<commit_before><?php
namespace Nf;

use Nf\Registry;
use Nf\Front;

class Router extends Singleton
{
    
    protected static $_instance=null;
    
    // pour le routeur
    private $routingPreferences = array();

    private $routesDirectories = array();

    private $rootRoutesDirectories = array();

    private $allRoutesByVersionAndLocale = array();

    private $activeRoute = array();

    private $allVersionsUrls = array();

    const rootRouteFilename = '_root.php';

    const defaultRequestType = 'default';

    const defaultRouteName = 'default';
    
    // routes
    public function addAllRoutes()
    {
                
        if (Registry::get('environment')!='dev' && file_exists(Registry::get('applicationPath') . '/cache/routes.all.php')) {
            $this->allRoutesByVersionAndLocale = require(Registry::get('applicationPath') . '/cache/routes.all.php');
        } else {
            $routesDirectory = realpath(Registry::get('applicationPath') . '/routes');
            $directory = new \RecursiveDirectoryIterator($routesDirectory);
            $files = new \RecursiveIteratorIterator($directory);
            $allRouteFiles = array();
            foreach ($files as $file) {
                $pathname = ltrim(str_replace($routesDirectory, '', $file->getPathname()), '/');
                // if it's not a folder or anything other than a .php
                if (substr($pathname, - 1, 1) != '.' && substr($pathname, -4)=='.php') {
                    $allRouteFiles[] = $pathname;
                }
            }
            // sort allRouteFiles by their depth to allow inheriting a route from all versions and or locales
            usort($allRouteFiles, function ($a, $b) {
                return substr_count($a, '/') > substr_count($b, '/');
            });
            foreach ($allRouteFiles as $file) {
                $pathname = ltrim(str_replace($routesDirectory, '', $file), '/');
            
                $arrPath = explode('/', $pathname);
            
                // routes are sorted by version and locale
                if (count($arrPath) == 3) {
                    $version = $arrPath[0];
                    $locale = $arrPath[1];
                } elseif (count($arrPath) == 2) {
                    $version = $arrPath[0];
                    $locale = '*';
                } elseif (count($arrPath) == 1) {
                    $version = '*';
                    $locale = '*';
                }
                // add the route to allRoutes, sorted by version and locale
                // all your routes are belong to us
                if (! isset($this->allRoutesByVersionAndLocale[$version])) {
                    $this->allRoutesByVersionAndLocale[$version] = array();
                }
                if (! isset($this->allRoutesByVersionAndLocale[$version][$locale])) {
                    $this->allRoutesByVersionAndLocale[$version][$locale] = array();
                }
                if (basename($file) != self::rootRouteFilename) {
                    $subPath = str_replace('.php', '', basename($file));
                } else {
                    $subPath = '';
                }
                $newRoutes = require $routesDirectory . '/' . $pathname;
                // the file doesn't contain an array, or contains nothing => we ignore it
                if (is_array($newRoutes)) {
                    foreach ($newRoutes as &$newRoute) {
                        if (isset($newRoute['type']) && $newRoute['type']=='inherit') {
                            // go up one level until we find the route to inherit from
                            if (isset($this->allRoutesByVersionAndLocale[$version]['*'][$newRoute['from']])) {
                                $routeToAdd = $this->allRoutesByVersionAndLocale[$version]['*'][$newRoute['from']];
                                $routeToAdd['regexp'] = $routeToAdd['inheritableRegexp'];
                                $routeToAdd['regexp'] = ltrim($routeToAdd['regexp'], '/');
                                $routeToAdd['regexp'] = rtrim(ltrim($subPath . '/' . $routeToAdd['regexp'], '/'), '/');
                            } elseif (isset($this->allRoutesByVersionAndLocale['*']['*'][$newRoute['from']])) {
                                $routeToAdd = $this->allRoutesByVersionAndLocale['*']['*'][$newRoute['from']];
                                $routeToAdd['regexp'] = $routeToAdd['inheritableRegexp'];
                                $routeToAdd['regexp'] = ltrim($routeToAdd['regexp'], '/');
                                $routeToAdd['regexp'] = rtrim(ltrim($subPath . '/' . $routeToAdd['regexp'], '/'), '/');
                            }
                            $this->allRoutesByVersionAndLocale[$version][$locale][$routeToAdd['name']] = $routeToAdd;
                        } else {
                            if (isset($newRoute['regexp'])) {
                                $newRoute['regexp'] = ltrim($newRoute['regexp'], '/');
                                $newRoute['inheritableRegexp'] = $newRoute['regexp'];
                                $newRoute['regexp'] = rtrim(ltrim($subPath . '/' . $newRoute['regexp'], '/'), '/');
                            }
                            if (isset($newRoute['name'])) {
                                $this->allRoutesByVersionAndLocale[$version][$locale][$newRoute['name']] = $newRoute;
                            } else {
                                $this->allRoutesByVersionAndLocale[$version][$locale][] = $newRoute;
                            }
                        }
                    }
                }
            }
        }
    }

    public function setRoutesFromFiles()
    {
        $this->routingPreferences[] = 'files';
    }

    public function setStructuredRoutes()
    {
        $this->routingPreferences[] = 'structured';
    }
<commit_msg>fixed routes without parameters<commit_after><?php
namespace Nf;

use Nf\Registry;
use Nf\Front;

class Router extends Singleton
{
    
    protected static $_instance=null;
    
    // pour le routeur
    private $routingPreferences = array();

    private $routesDirectories = array();

    private $rootRoutesDirectories = array();

    private $allRoutesByVersionAndLocale = array();

    private $activeRoute = array();

    private $allVersionsUrls = array();

    const rootRouteFilename = '_root.php';

    const defaultRequestType = 'default';

    const defaultRouteName = 'default';
    
    // routes
    public function addAllRoutes()
    {
                
        if (Registry::get('environment')!='dev' && file_exists(Registry::get('applicationPath') . '/cache/routes.all.php')) {
            $this->allRoutesByVersionAndLocale = require(Registry::get('applicationPath') . '/cache/routes.all.php');
        } else {
            $routesDirectory = realpath(Registry::get('applicationPath') . '/routes');
            $directory = new \RecursiveDirectoryIterator($routesDirectory);
            $files = new \RecursiveIteratorIterator($directory);
            $allRouteFiles = array();
            foreach ($files as $file) {
                $pathname = ltrim(str_replace($routesDirectory, '', $file->getPathname()), '/');
                // if it's not a folder or anything other than a .php
                if (substr($pathname, - 1, 1) != '.' && substr($pathname, -4)=='.php') {
                    $allRouteFiles[] = $pathname;
                }
            }
            // sort allRouteFiles by their depth to allow inheriting a route from all versions and or locales
            usort($allRouteFiles, function ($a, $b) {
                return substr_count($a, '/') > substr_count($b, '/');
            });
            foreach ($allRouteFiles as $file) {
                $pathname = ltrim(str_replace($routesDirectory, '', $file), '/');
            
                $arrPath = explode('/', $pathname);
            
                // routes are sorted by version and locale
                if (count($arrPath) == 3) {
                    $version = $arrPath[0];
                    $locale = $arrPath[1];
                } elseif (count($arrPath) == 2) {
                    $version = $arrPath[0];
                    $locale = '*';
                } elseif (count($arrPath) == 1) {
                    $version = '*';
                    $locale = '*';
                }
                // add the route to allRoutes, sorted by version and locale
                // all your routes are belong to us
                if (! isset($this->allRoutesByVersionAndLocale[$version])) {
                    $this->allRoutesByVersionAndLocale[$version] = array();
                }
                if (! isset($this->allRoutesByVersionAndLocale[$version][$locale])) {
                    $this->allRoutesByVersionAndLocale[$version][$locale] = array();
                }
                if (basename($file) != self::rootRouteFilename) {
                    $subPath = str_replace('.php', '', basename($file));
                } else {
                    $subPath = '';
                }
                $newRoutes = require $routesDirectory . '/' . $pathname;
                // the file doesn't contain an array, or contains nothing => we ignore it
                if (is_array($newRoutes)) {
                    foreach ($newRoutes as &$newRoute) {
                        if (isset($newRoute['type']) && $newRoute['type']=='inherit') {
                            // go up one level until we find the route to inherit from
                            if (isset($this->allRoutesByVersionAndLocale[$version]['*'][$newRoute['from']])) {
                                $routeToAdd = $this->allRoutesByVersionAndLocale[$version]['*'][$newRoute['from']];
                                $routeToAdd['regexp'] = $routeToAdd['inheritableRegexp'];
                                $routeToAdd['regexp'] = ltrim($routeToAdd['regexp'], '/');
                                $routeToAdd['regexp'] = rtrim(ltrim($subPath . '/' . $routeToAdd['regexp'], '/'), '/');
                            } elseif (isset($this->allRoutesByVersionAndLocale['*']['*'][$newRoute['from']])) {
                                $routeToAdd = $this->allRoutesByVersionAndLocale['*']['*'][$newRoute['from']];
                                $routeToAdd['regexp'] = $routeToAdd['inheritableRegexp'];
                                $routeToAdd['regexp'] = ltrim($routeToAdd['regexp'], '/');
                                $routeToAdd['regexp'] = rtrim(ltrim($subPath . '/' . $routeToAdd['regexp'], '/'), '/');
                            }
                            $this->allRoutesByVersionAndLocale[$version][$locale][$routeToAdd['name']] = $routeToAdd;
                        } else {
                            if (isset($newRoute['regexp'])) {
                                $newRoute['regexp'] = ltrim($newRoute['regexp'], '/');
                                $newRoute['inheritableRegexp'] = $newRoute['regexp'];
                                // special case for $ for answering to /something$ (setting something$ and not /something/$ as the regexp)
                                if($newRoute['regexp'] == '$') {
                                    $newRoute['regexp'] = rtrim(ltrim($subPath . $newRoute['regexp'], '/'), '/');
                                }
                                else {
                                    $newRoute['regexp'] = rtrim(ltrim($subPath . '/' . $newRoute['regexp'], '/'), '/');
                                }
                            }
                            if (isset($newRoute['name'])) {
                                $this->allRoutesByVersionAndLocale[$version][$locale][$newRoute['name']] = $newRoute;
                            } else {
                                $this->allRoutesByVersionAndLocale[$version][$locale][] = $newRoute;
                            }
                        }
                    }
                }
            }
        }
    }

    public function setRoutesFromFiles()
    {
        $this->routingPreferences[] = 'files';
    }

    public function setStructuredRoutes()
    {
        $this->routingPreferences[] = 'structured';
    }

{'fraction_non_alphanumeric': 0.1461796740643682, 'fraction_numerical': 0.0014740807468675784, 'mean_word_length': 1.5484140233722872, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '859498', 'n_tokens_mistral': 3191, 'n_tokens_neox': 3066, 'n_words': 567}