<commit_before><?php
/**
 * xenFramework (http://xenframework.com/)
 *
 * This file is part of the xenframework package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * @link        http://github.com/xenframework for the canonical source repository
 * @copyright   Copyright (c) xenFramework. (http://xenframework.com)
 * @license     MIT License - http://en.wikipedia.org/wiki/MIT_License
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace xen\application;

class Router
{
    private $_url;
    private $_routes;
    private $_controller;
    private $_action;
    private $_params;

    public function __construct($_url)
    {
        $this->_cleanUrl($_url);
        $this->_routes = require str_replace('/', DIRECTORY_SEPARATOR, 'application/configs/routes.php');
        $this->_parseRoutes();
        $this->_params = array();
    }

    private function _cleanUrl($url)
    {
        $this->_url = ($url === null) ? '/' : '/' . filter_var($url, FILTER_SANITIZE_URL);
    }

    public function route($role)
    {
        $found = false;

        foreach ($this->_parseRoutes() as $route => $value) {

            if (preg_match('!^' . $route . '$!', $this->_url, $results) == 1) {

                $found = true;

                if (empty($value['allow']) || in_array($role, $value['allow'])) {

                    $params = array();

                    foreach ($value['params'] as $param) {

                        $params[$param] = $results[$param];
                    }

                    $this->_controller  = ucfirst($value['controller']);
                    $this->_action      = $value['action'];
                    $this->_params      = $params;

                } else {

                    $this->_controller  = 'Error';
                    $this->_action      = 'forbidden';
                    $this->_params      = array(
                        'controller'    => $value['controller'],
                        'action'        => $value['action'],
                    );<commit_msg>phpdoc<commit_after><?php
/**
 * xenFramework (http://xenframework.com/)
 *
 * This file is part of the xenframework package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * @link        http://github.com/xenframework for the canonical source repository
 * @copyright   Copyright (c) xenFramework. (http://xenframework.com)
 * @license     MIT License - http://en.wikipedia.org/wiki/MIT_License
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace xen\application;

/**
 * Class Router
 *
 * The router has 3 functions:
 *
 *      1. Route            => returns the Controller, the Action and the Params for the current Request
 *      2. ACL              => The router checks if a Role can access to the current route
 *      3. Url generator    => The router generates urls from a given Controller, Action and Params
 *
 * @package    xenframework
 * @subpackage xen\application
 * @author     <NAME> <<EMAIL>>
 * @copyright  Copyright (c) xenFramework. (http://xenframework.com)
 * @license    MIT License - http://en.wikipedia.org/wiki/MIT_License
 * @link       https://github.com/xenframework/xen
 * @since      Class available since Release 1.0.0
 */
class Router
{
    /**
     * @var string The url of the current Request
     */
    private $_url;

    /**
     * @var array The defined routes in 'application/configs/routes.php'
     */
    private $_routes;

    /**
     * @var array The parsed routes with constraints as a regular expressions
     */
    private $_parsedRoutes;

    /**
     * @var Controller The controller for manage the Request
     */
    private $_controller;

    /**
     * @var string The action for manage the Request
     */
    private $_action;

    /**
     * @var array The params of the current Request
     */
    private $_params;

    public function __construct($_url)
    {
        $this->_cleanUrl($_url);

        $this->_routes          = require str_replace('/', DIRECTORY_SEPARATOR, 'application/configs/routes.php');
        $this->_parsedRoutes    = $this->_parseRoutes();
        $this->_params          = array();
    }

    private function _cleanUrl($url)
    {
        $this->_url = ($url === null) ? '/' : '/' . filter_var($url, FILTER_SANITIZE_URL);
    }

    public function route($role)
    {
        $found = false;

        foreach ($this->_parsedRoutes as $route => $value) {

            if (preg_match('!^' . $route . '$!', $this->_url, $results) == 1) {

                $found = true;

                if (empty($value['allow']) || in_array($role, $value['allow'])) {

                    $params = array();

                    foreach ($value['params'] as $param) {

                        $params[$param] = $results[$param];
                    }

                    $this->_controller  = ucfirst($value['controller']);
                    $this->_action      = $value['action'];
                    $this->_params      = $params;

                } else {

                    $this->_controller  = 'Error';
                    $this->_action      = 'forbidden';
                    $this->_params      = array(
                        'controller'    => $value['controller'],
                        'action'        => $value['action'],
                    );
{'fraction_non_alphanumeric': 0.13781636800295585, 'fraction_numerical': 0.001662663957140218, 'mean_word_length': 1.8645502645502645, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 48, 'https://': 1, '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': '496778', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1439, 'n_words': 386}