<commit_before><?php
namespace Bolt\Controllers;

use Doctrine\Common\Collections\ArrayCollection;
use Silex;
use Silex\ControllerProviderInterface;

/**
 * Configurable routes controller
 *
 * Add routes from a configuration file.
 */
class Routing implements ControllerProviderInterface
{
    /** @var Silex\Application */
    protected $app;

    /**
     * Connect this controller to the application
     *
     * @param Silex\Application $app
     *
     * @return Silex\ControllerCollection
     */
    public function connect(Silex\Application $app)
    {
        $this->app = $app;

        $routes = $app['config']->get('routing');
        $routes = is_array($routes) ? $routes : array();

        $ctr = $this->addRoutes($routes);

        return $ctr;
    }

    /**
     * Add routes based on the parsed array
     *
     * @param array $routes
     *
     * @return Silex\ControllerCollection
     */
    protected function addRoutes(array $routes)
    {
        /** @var $ctr Silex\ControllerCollection */
        $ctr = $this->app['controllers_factory'];

        foreach ($routes as $name => $config) {
            $config = new ArrayCollection($config);

            if (!$path = $config['path']) {
                continue;
            }
            if (!$defaults = $config['defaults']) {
                continue;
            }
            $defaults = new ArrayCollection($defaults);

            if (!$to = $defaults->remove('_controller')) {
                continue;
            }
            if (strpos($to, '::') > 0) {
                $to = explode('::', $to);
            }
            $route = $ctr->match($path, $to);

            $before = $defaults->remove('_before') ?: '::before';
            if (substr($before, 0, 2) === '::' && is_array($to)) {
                $before = array($to[0], substr($before, 2));
            }
            $route->before($before);

            $after = $defaults->remove('_after') ?: '::after';
            if (substr($after, 0, 2) === '::' && is_array($to)) {
                $after = array($to[0], substr($after, 2));
            }
            $route->after($after);

            foreach ($defaults as $key => $value) {
                $route->value($key, $value);
            }

            foreach ($config['requirements'] ?: array() as $variable => $regexp) {
                $properRegexp = $this->getProperRegexp($regexp);
                $route->assert($variable, $properRegexp);
            }

            if ($host = $config['host']) {
                $route->setHost($host);
            }

            $route->bind($name);
        }

        return $ctr;
    }

    /**<commit_msg>Extracted addRoute() out of addRoutes(). This will make it easier to extend.<commit_after><?php
namespace Bolt\Controllers;

use Doctrine\Common\Collections\ArrayCollection;
use Silex;
use Silex\ControllerProviderInterface;

/**
 * Configurable routes controller
 *
 * Add routes from a configuration file.
 */
class Routing implements ControllerProviderInterface
{
    /** @var Silex\Application */
    protected $app;

    /**
     * Connect this controller to the application
     *
     * @param Silex\Application $app
     *
     * @return Silex\ControllerCollection
     */
    public function connect(Silex\Application $app)
    {
        $this->app = $app;

        $routes = $app['config']->get('routing');
        $routes = is_array($routes) ? $routes : array();

        $ctr = $this->addRoutes($routes);

        return $ctr;
    }

    /**
     * Add routes based on the parsed array
     *
     * @param array $routes
     *
     * @return Silex\ControllerCollection
     */
    protected function addRoutes(array $routes)
    {
        /** @var $ctr Silex\ControllerCollection */
        $ctr = $this->app['controllers_factory'];

        foreach ($routes as $name => $config) {
            $this->addRoute($ctr, $name, $config);
        }

        return $ctr;
    }

    protected function addRoute(Silex\ControllerCollection $ctr, $name, array $config)
    {
        $config = new ArrayCollection($config);

        if (!$path = $config['path']) {
            return;
        }
        if (!$defaults = $config['defaults']) {
            return;
        }
        $defaults = new ArrayCollection($defaults);

        if (!$to = $defaults->remove('_controller')) {
            return;
        }
        if (strpos($to, '::') > 0) {
            $to = explode('::', $to);
        }
        $route = $ctr->match($path, $to);

        $before = $defaults->remove('_before') ?: '::before';
        if (substr($before, 0, 2) === '::' && is_array($to)) {
            $before = array($to[0], substr($before, 2));
        }
        $route->before($before);

        $after = $defaults->remove('_after') ?: '::after';
        if (substr($after, 0, 2) === '::' && is_array($to)) {
            $after = array($to[0], substr($after, 2));
        }
        $route->after($after);

        foreach ($defaults as $key => $value) {
            $route->value($key, $value);
        }

        foreach ($config['requirements'] ?: array() as $variable => $regexp) {
            $properRegexp = $this->getProperRegexp($regexp);
            $route->assert($variable, $properRegexp);
        }

        if ($host = $config['host']) {
            $route->setHost($host);
        }

        $route->bind($name);
    }

    /**
{'fraction_non_alphanumeric': 0.16399851079672376, 'fraction_numerical': 0.0033507073715562173, 'mean_word_length': 1.957072096862961, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, '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': '213522', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1556, 'n_words': 333}