<commit_before><?php

/*!
 * SmoothPHP
 * This file is part of the SmoothPHP project.
 * * * *
 * Copyright (C) 2016 <NAME>
 * License: https://github.com/Ikkerens/SmoothPHP/blob/master/License.md
 * * * *
 * RouteDatabase.php
 * Class responsible for parsing new routes, indexing them and then decoding requests.
 */

namespace SmoothPHP\Framework\Flow\Routing;

use SmoothPHP\Framework\Flow\Requests\Request;
use SmoothPHP\Framework\Flow\Responses\PlainTextResponse;

class RouteDatabase {
    const HANDLER = '-EOP-';
    const WILDCARD_INPUT = '%';
    const VARARGS_INPUT = '...';

    private $defaults;
    private $routes;
    private $resolveCache;

    public function __construct() {
        $this->routes = array();
        $this->resolveCache = array();

        $this->defaults = array(
            'method' => 'GET',
            'domain' => self::WILDCARD_INPUT,
            'content-type' => PlainTextResponse::class
        );
    }

    public function register(array $routeOptions) {
        $routeOpts = array_merge($this->defaults, $routeOptions);

        foreach(((array) $routeOpts['method']) as $method) {
            $path = explode('/', $routeOptions['path']);
            $path = array_merge(array($method, $routeOpts['domain']), $path);
            $path = array_values(array_filter($path, 'strlen'));

            $currentRecursive = &$this->resolveCache;
            for ($i = 0; $i < count($path); $i++) {
                $pathPart = $path[$i];

                if ($pathPart == self::VARARGS_INPUT && $i < (count($path) - 1))
                    throw new \LogicException("Route varargs can only be used at the end of the path");

                if (!isset($currentRecursive[$pathPart]))
                    $currentRecursive[$pathPart] = array();

                $currentRecursive = &$currentRecursive[$pathPart];
            }

            $currentRecursive[self::HANDLER] = &$routeOpts;
        }

        $this->routes[$routeOpts['name']] = &$routeOpts;

        $routeOpts['controllercall'] = new ControllerCall($routeOpts['controller'], $routeOpts['call']);
    }

    public function initializeControllers() {
        foreach($this->routes as $route) {
            $route['controllercall']->initializeController();
        }
    }
<commit_msg>HTTP Method is now always uppercased<commit_after><?php

/*!
 * SmoothPHP
 * This file is part of the SmoothPHP project.
 * * * *
 * Copyright (C) 2016 <NAME>
 * License: https://github.com/Ikkerens/SmoothPHP/blob/master/License.md
 * * * *
 * RouteDatabase.php
 * Class responsible for parsing new routes, indexing them and then decoding requests.
 */

namespace SmoothPHP\Framework\Flow\Routing;

use SmoothPHP\Framework\Flow\Requests\Request;
use SmoothPHP\Framework\Flow\Responses\PlainTextResponse;

class RouteDatabase {
    const HANDLER = '-EOP-';
    const WILDCARD_INPUT = '%';
    const VARARGS_INPUT = '...';

    private $defaults;
    private $routes;
    private $resolveCache;

    public function __construct() {
        $this->routes = array();
        $this->resolveCache = array();

        $this->defaults = array(
            'method' => 'GET',
            'domain' => self::WILDCARD_INPUT,
            'content-type' => PlainTextResponse::class
        );
    }

    public function register(array $routeOptions) {
        $routeOpts = array_merge($this->defaults, $routeOptions);

        foreach(((array) $routeOpts['method']) as $method) {
            $path = explode('/', $routeOptions['path']);
            $path = array_merge(array(strtoupper($method), $routeOpts['domain']), $path);
            $path = array_values(array_filter($path, 'strlen'));

            $currentRecursive = &$this->resolveCache;
            for ($i = 0; $i < count($path); $i++) {
                $pathPart = $path[$i];

                if ($pathPart == self::VARARGS_INPUT && $i < (count($path) - 1))
                    throw new \LogicException("Route varargs can only be used at the end of the path");

                if (!isset($currentRecursive[$pathPart]))
                    $currentRecursive[$pathPart] = array();

                $currentRecursive = &$currentRecursive[$pathPart];
            }

            $currentRecursive[self::HANDLER] = &$routeOpts;
        }

        $this->routes[$routeOpts['name']] = &$routeOpts;

        $routeOpts['controllercall'] = new ControllerCall($routeOpts['controller'], $routeOpts['call']);
    }

    public function initializeControllers() {
        foreach($this->routes as $route) {
            $route['controllercall']->initializeController();
        }
    }

{'fraction_non_alphanumeric': 0.1485342019543974, 'fraction_numerical': 0.0026058631921824105, 'mean_word_length': 2.609717868338558, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 2, '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': '1070091', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1324, 'n_words': 279}