<commit_before><?php

namespace Symfony\Cmf\Bundle\ChainRoutingBundle\Routing;

use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;

/**
 * ChainRouter
 *
 * Allows access to a lot of different routers.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ChainRouter implements RouterInterface, WarmableInterface
{
    private $context;
    private $logger;

    public function __construct(LoggerInterface $logger = null)
    {
        $this->logger = $logger;
    }

    public function getContext()
    {
        return $this->context;
    }

    /**
     * @var array
     */
    private $routers = array();

    /**
     * Add a Router to the index
     *
     * @param RouterInterface $router
     * @param integer $priority
     */
    public function add(RouterInterface $router, $priority = 0)
    {
        if (empty($this->routers[$priority])) {
            $this->routers[$priority] = array();
        }

        $this->routers[$priority][] = $router;
    }

    /**
     * Sorts the routers and flattens them.
     *
     * @return array
     */
    public function all()
    {
        krsort($this->routers);
        $routers = array();

        foreach ($this->routers as $all) {
            $routers = array_merge($routers, $all);
        }

        return $routers;
    }

    /**
     * Loops through all routes and tries to match the passed url.
     *
     * @param string $url
     * @throws ResourceNotFoundException $e
     * @throws MethodNotAllowedException $e
     * @return array
     */
    public function match($url)
    {
        $methodNotAllowed = null;

        foreach ($this->all() as $router) {
            try {
                return $router->match($url);
            } catch (ResourceNotFoundException $e) {
                if ($this->logger) {
                    $this->logger->addError($e);
                }
                // Needs special care
            } catch (MethodNotAllowedException $e) {
                if ($this->logger) {
                    $this->logger->addError($e);
                }
                $methodNotAllowed = $e;
            }
        }

        throw $methodNotAllowed ?: new ResourceNotFoundException("None of the routers in the chain matched '$url'");
    }

    /**
     * Loops through all registered routers and returns a router if one is found.
     * It will always return the first route generated.
     *
     * @param string $name
     * @param array $parameters
     * @param Boolean $absolute
     * @throws RouteNotFoundException
     * @return string
     */
    public function generate($name, $parameters = array(), $absolute = false)
    {
        foreach ($this->all() as $router) {
            try {
                return $router->generate($name, $parameters, $absolute);
            } catch (RouteNotFoundException $e) {
                if ($this->logger) {
                    $this->logger->addError($e);
                }
            }
        }

        throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
    }

    /**
     * Sets the Request Context
     *
     * @param RouterContext $context
     */
    public function setContext(RequestContext $context)
    {
        $this->context = $context;

        foreach ($this->all() as $router) {
            if ($router instanceof RequestContextAwareInterface) {<commit_msg>do not log unmatched routes as an error<commit_after><?php

namespace Symfony\Cmf\Bundle\ChainRoutingBundle\Routing;

use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;

/**
 * ChainRouter
 *
 * Allows access to a lot of different routers.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ChainRouter implements RouterInterface, WarmableInterface
{
    private $context;
    private $logger;

    public function __construct(LoggerInterface $logger = null)
    {
        $this->logger = $logger;
    }

    public function getContext()
    {
        return $this->context;
    }

    /**
     * @var array
     */
    private $routers = array();

    /**
     * Add a Router to the index
     *
     * @param RouterInterface $router
     * @param integer $priority
     */
    public function add(RouterInterface $router, $priority = 0)
    {
        if (empty($this->routers[$priority])) {
            $this->routers[$priority] = array();
        }

        $this->routers[$priority][] = $router;
    }

    /**
     * Sorts the routers and flattens them.
     *
     * @return array
     */
    public function all()
    {
        krsort($this->routers);
        $routers = array();

        foreach ($this->routers as $all) {
            $routers = array_merge($routers, $all);
        }

        return $routers;
    }

    /**
     * Loops through all routes and tries to match the passed url.
     *
     * @param string $url
     * @throws ResourceNotFoundException $e
     * @throws MethodNotAllowedException $e
     * @return array
     */
    public function match($url)
    {
        $methodNotAllowed = null;

        foreach ($this->all() as $router) {
            try {
                return $router->match($url);
            } catch (ResourceNotFoundException $e) {
                if ($this->logger) {
                    $this->logger->addInfo($e->getMessage());
                }
                // Needs special care
            } catch (MethodNotAllowedException $e) {
                if ($this->logger) {
                    $this->logger->addInfo($e->getMessage());
                }
                $methodNotAllowed = $e;
            }
        }

        throw $methodNotAllowed ?: new ResourceNotFoundException("None of the routers in the chain matched '$url'");
    }

    /**
     * Loops through all registered routers and returns a router if one is found.
     * It will always return the first route generated.
     *
     * @param string $name
     * @param array $parameters
     * @param Boolean $absolute
     * @throws RouteNotFoundException
     * @return string
     */
    public function generate($name, $parameters = array(), $absolute = false)
    {
        foreach ($this->all() as $router) {
            try {
                return $router->generate($name, $parameters, $absolute);
            } catch (RouteNotFoundException $e) {
                if ($this->logger) {
                    $this->logger->addInfo($e->getMessage());
                }
            }
        }

        throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
    }

    /**
     * Sets the Request Context
     *
     * @param RouterContext $context
     */
    public function setContext(RequestContext $context)
    {
        $this->context = $context;

        foreach ($this->all() as $router) {
            if ($router instanceof RequestContextAwareInterface) {
{'fraction_non_alphanumeric': 0.1161256950730635, 'fraction_numerical': 0.000258631837579206, 'mean_word_length': 2.1451809678731193, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5158071', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2079, 'n_words': 521}