<commit_before><?php

namespace Bolt\Controllers;

use Silex;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Routes implements ControllerProviderInterface
{
    public function connect(Silex\Application $app)
    {
        $ctr = false;

        $filename = BOLT_CONFIG_DIR.'/routes.yml';
        if (is_readable($filename)) {
            $yamlparser = new \Symfony\Component\Yaml\Parser();
            $routes = $yamlparser->parse(file_get_contents($filename) . "\n");

            if (is_array($routes)) {
                $ctr = $this->addRoutes($app, $routes);
            }
        }

        if ($ctr === false) {
            $ctr = $app['controllers_factory'];
        }

        return $ctr;
    }

    private function addRoutes(Silex\Application $app, array $routes)
    {
        $ctr = $app['controllers_factory'];

        foreach($routes as $binding => $route) {
            $path         = false;
            $to           = false;
            $controller   = false;
            $_controller  = false;
            $_before      = false;
            $_after       = false;
            $requirements = array();
            if (isset($route['path'])) {
                $path = $route['path'];
            }
            if (isset($route['defaults'])) {
                $default = $route['defaults'];
                if (isset($default['_controller'])) {
                    $to = $default['_controller'];
                    if (strpos($to, '::') > 0) {
                        $_controller = explode('::', $default['_controller']);
                    }
                    /*
                    $_controller = explode('::', $default['_controller']);
                    if (class_exists($_controller[0])) {
                        $instance = new $_controller[0];
                        $to       = array($instance, $_controller[1]);
                    }
                    //*/
                }
                if (isset($default['_before'])) {
                    if ((substr($default['_before'] ,0, 2) == '::') && (is_array($_controller))) {
                        $_before = $_controller[0].$default['_before'];
                    }
                    else {
                        $_before = $default['_before'];
                    }
                }
                if (isset($default['_after'])) {
                    if ((substr($default['_after'] ,0, 2) == '::') && (is_array($_controller))) {
                        $_after = $_controller[0].$default['_after'];
                    }
                    else {
                        $_after = $default['_after'];
                    }
                }
            }
            if (isset($route['requirements']) && (is_array($route['requirements']))) {
                $requirements = $route['requirements'];
            }

            if (($path !== false) && ($to !== false)) {
                $controller = $ctr->match($path, $to);
            }
            if ($controller !== false) {
                if ($_before !== false) {
                    $controller->before($_before);
                }
                if ($_after !== false) {
                    $controller->after($_after);
                }
            
                foreach($requirements as $variable => $regexp) {
                    $controller->assert($variable, $regexp);
                }
<commit_msg>Added more YAML to Route features.<commit_after><?php
namespace Bolt\Controllers;

use Silex;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


/**
 * Configurable routes controller
 *
 * Read and add routes based on a routes.yml file.
 */
class Routes implements ControllerProviderInterface
{
    /**
     * Connect this controller to the application
     */
    public function connect(Silex\Application $app)
    {
        $ctr = false;

        $filename = BOLT_CONFIG_DIR.'/routes.yml.dist';
        if (is_readable($filename)) {
            $yamlparser = new \Symfony\Component\Yaml\Parser();
            $routes = $yamlparser->parse(file_get_contents($filename) . "\n");

            if (is_array($routes)) {
                $ctr = $this->addRoutes($app, $routes);
            }
        }

        if ($ctr === false) {
            $ctr = $app['controllers_factory'];
        }

        return $ctr;
    }

    /**
     * Add routes based on the parsed array
     */
    private function addRoutes(Silex\Application $app, array $routes)
    {
        $ctr = $app['controllers_factory'];

        foreach($routes as $binding => $route) {
            $path         = false;
            $to           = false;
            $controller   = false;
            $host         = false;
            $_controller  = false;
            $_before      = false;
            $_after       = false;
            $defaults     = array();
            $requirements = array();


            // parse YAML structure

            if (isset($route['path'])) {
                $path = $route['path'];
            }
            if (isset($route['defaults'])) {
                $defaults = $route['defaults'];
                if (isset($defaults['_controller'])) {
                    $to = $defaults['_controller'];
                    if (strpos($to, '::') > 0) {
                        $_controller = explode('::', $defaults['_controller']);

                        if (class_exists($_controller[0])) {
                            $instance = new $_controller[0];

                            $to = array($instance, $_controller[1]);
                        }
                    }
                    unset($defaults['_controller']);
                }
                if (isset($defaults['_before'])) {
                    if ((substr($defaults['_before'] ,0, 2) == '::') && (is_array($to))) {
                        //$_before = $_controller[0].$defaults['_before'];
                        $_before = array($to[0], substr($defaults['_before'], 2));
                    }
                    else {
                        $_before = $defaults['_before'];
                    }
                    unset($defaults['_before']);
                }
                if (isset($defaults['_after'])) {
                    if ((substr($defaults['_after'] ,0, 2) == '::') && (is_array($to))) {
                        //$_after = $_controller[0].$defaults['_after'];
                        $_after = array($to[0], substr($defaults['_after'], 2));
                    }
                    else {
                        $_after = $defaults['_after'];
                    }
                    unset($defaults['_after']);
                }
            }
            if (isset($route['requirements']) && (is_array($route['requirements']))) {
                $requirements = $route['requirements'];
            }
            if (isset($route['host'])) {
                $host = $route['host'];
            }


            // add Route

            if (($path !== false) && ($to !== false)) {
                $controller = $ctr->match($path, $to);
            }
            if ($controller !== false) {
                if ($_before !== false) {
                    $controller->before($_before);
                }
                if ($_after !== false) {
                    $controller->after($_after);
                }
            
                foreach($requirements as $variable => $regexp) {
                    $controller->assert($variable, $regexp);
                }
                foreach($defaults as $variable => $default) {
                    $controller->value($variable, $default);
                }

{'fraction_non_alphanumeric': 0.14811942613416051, 'fraction_numerical': 0.0031019775106630476, 'mean_word_length': 1.315380011968881, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '505203', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1887, 'n_words': 361}