<commit_before><?php

namespace Rad\Routing;

use Rad\Config;
use Rad\Core\Bundles;

/**
 * RadPHP Router
 *
 * @package Rad\Routing
 */
class Router
{
    protected $uriSource = self::URI_SOURCE_SERVER_REQUEST_URI;
    protected $module;
    protected $action;
    protected $actionNamespace;
    protected $responderNamespace;
    protected $params;
    protected $language = null;
    protected $isMatched = false;

    const DEFAULT_ACTION = 'index';
    const URI_SOURCE_GET_URL = 'get_url_source';
    const URI_SOURCE_SERVER_REQUEST_URI = 'request_uri_source';

    const GEN_OPT_LANGUAGE = 'gen_opt_language';
    const GEN_OPT_WITH_PARAMS = 'gen_opt_with_params';

    protected $generateDefaultOption = [
        self::GEN_OPT_LANGUAGE => true,
        self::GEN_OPT_WITH_PARAMS => true,
    ];

    /**
     * Get rewrite info. This info is read from $_GET['_url'].
     * This returns '/' if the rewrite information cannot be read
     *
     * @return string
     */
    public function getRewriteUri()
    {
        if ($this->uriSource !== self::URI_SOURCE_SERVER_REQUEST_URI) {
            if (isset($_GET['_url']) && !empty($_GET['_url'])) {
                return $_GET['_url'];
            }
        } else {
            if (isset($_SERVER['REQUEST_URI'])) {
                $requestUri = explode('?', $_SERVER['REQUEST_URI']);
                if (!empty($requestUri[0])) {
                    return $requestUri[0];
                }
            }
        }

        return '/';
    }

    /**
     * Handles routing information received from the rewrite engine
     *
     * @param string $uri
     */
    public function handle($uri = null)
    {
        if (!$uri) {
            $uri = $this->getRewriteUri();
        }

        $uri = trim($uri, '/');

        // remove empty cells
        $parts = [];
        foreach (explode('/', $uri) as $p) {
            if (trim($p) !== '') {
                $parts[] = $p;
            }
        }

        // check language
        if (in_array($parts[0], Config::get('languages.possible', ['en']))) {
            // We found the language, so set it as current language
            $this->language = array_shift($parts);
        } else {
            $this->language = Config::get('languages.default', 'en');
        }

        // Cleaning route parts & Rebase array keys
        $camelizedParts = $parts;
        // I really need to add index to both of them separately! Because of "lazy copy" feature of PHP
        $camelizedParts[] = self::DEFAULT_ACTION;
        $parts[] = self::DEFAULT_ACTION;

        array_values(array_filter($camelizedParts, [$this, 'camelize']));
        $module = reset($parts);
        $this->camelize($module);
        $bundles = array_intersect(['App', $module], Bundles::getLoaded());

        $matchedRoute = null;
        foreach ($bundles as $bundleName) {
            // reset manipulation parameters
            $dummyCamelizedParts = $camelizedParts;
            $dummyParts = $parts;

            if ($bundleName === 'App') {
                array_unshift($dummyParts, $bundleName);
                array_unshift($dummyCamelizedParts, $bundleName);
            } else {
                // get bundle namespace instead of its name
                array_shift($dummyCamelizedParts);
                array_unshift($dummyCamelizedParts, trim(Bundles::getNamespace($bundleName), '\\'));
            }

            // add "Action" to array as second param
            array_splice($dummyCamelizedParts, 1, 0, 'Action');

            // Continue searching till you found any matching
            // Or you have at least three elements in array (Bundle, "Action", Action)
            for ($i = 0; count($dummyCamelizedParts) >= 3; $i++) {
                $actionNamespace = implode('\\', $dummyCamelizedParts) . 'Action';

                if (class_exists($actionNamespace)) {
                    array_splice($dummyCamelizedParts, 1, 1, 'Responder');
                    $responderNamespace = implode('\\', $dummyCamelizedParts) . 'Responder';

                    $matchedRoute = [
                        'namespace' => $actionNamespace,
                        'responder' => $responderNamespace,
                        'action' => $dummyParts[count($dummyCamelizedParts) - 2],
                        'module' => strtolower($bundleName),
                        'params' => array_slice($dummyParts, count($dummyCamelizedParts) - 1, -1)
                    ];
<commit_msg>fix a small issue in router<commit_after><?php

namespace Rad\Routing;

use Rad\Config;
use Rad\Core\Bundles;

/**
 * RadPHP Router
 *
 * @package Rad\Routing
 */
class Router
{
    protected $uriSource = self::URI_SOURCE_SERVER_REQUEST_URI;
    protected $module;
    protected $action;
    protected $actionNamespace;
    protected $responderNamespace;
    protected $params;
    protected $language = null;
    protected $isMatched = false;

    const DEFAULT_ACTION = 'index';
    const URI_SOURCE_GET_URL = 'get_url_source';
    const URI_SOURCE_SERVER_REQUEST_URI = 'request_uri_source';

    const GEN_OPT_LANGUAGE = 'gen_opt_language';
    const GEN_OPT_WITH_PARAMS = 'gen_opt_with_params';

    protected $generateDefaultOption = [
        self::GEN_OPT_LANGUAGE => true,
        self::GEN_OPT_WITH_PARAMS => true,
    ];

    /**
     * Get rewrite info. This info is read from $_GET['_url'].
     * This returns '/' if the rewrite information cannot be read
     *
     * @return string
     */
    public function getRewriteUri()
    {
        if ($this->uriSource !== self::URI_SOURCE_SERVER_REQUEST_URI) {
            if (isset($_GET['_url']) && !empty($_GET['_url'])) {
                return $_GET['_url'];
            }
        } else {
            if (isset($_SERVER['REQUEST_URI'])) {
                $requestUri = explode('?', $_SERVER['REQUEST_URI']);
                if (!empty($requestUri[0])) {
                    return $requestUri[0];
                }
            }
        }

        return '/';
    }

    /**
     * Handles routing information received from the rewrite engine
     *
     * @param string $uri
     */
    public function handle($uri = null)
    {
        if (!$uri) {
            $uri = $this->getRewriteUri();
        }

        $uri = trim($uri, '/');

        // remove empty cells
        $parts = [];
        foreach (explode('/', $uri) as $p) {
            if (trim($p) !== '') {
                $parts[] = $p;
            }
        }

        // check language
        if (in_array($parts[0], Config::get('languages.possible', ['en']))) {
            // We found the language, so set it as current language
            $this->language = array_shift($parts);
        } else {
            $this->language = Config::get('languages.default', 'en');
        }

        // Cleaning route parts & Rebase array keys
        $camelizedParts = $parts;
        // I really need to add index to both of them separately! Because of "lazy copy" feature of PHP
        $camelizedParts[] = self::DEFAULT_ACTION;
        $parts[] = self::DEFAULT_ACTION;

        array_values(array_filter($camelizedParts, [$this, 'camelize']));
        $module = reset($parts);
        $this->camelize($module);
        $bundles = array_intersect(['App', $module], Bundles::getLoaded());

        $matchedRoute = null;
        foreach ($bundles as $bundleName) {
            // reset manipulation parameters
            $dummyCamelizedParts = $camelizedParts;
            $dummyParts = $parts;

            if ($bundleName === 'App' && $dummyCamelizedParts[0] != 'App') {
                array_unshift($dummyParts, $bundleName);
                array_unshift($dummyCamelizedParts, $bundleName);
            } else {
                // get bundle namespace instead of its name
                array_shift($dummyCamelizedParts);
                array_unshift($dummyCamelizedParts, trim(Bundles::getNamespace($bundleName), '\\'));
            }

            // add "Action" to array as second param
            array_splice($dummyCamelizedParts, 1, 0, 'Action');

            // Continue searching till you found any matching
            // Or you have at least three elements in array (Bundle, "Action", Action)
            for ($i = 0; count($dummyCamelizedParts) >= 3; $i++) {
                $actionNamespace = implode('\\', $dummyCamelizedParts) . 'Action';

                if (class_exists($actionNamespace)) {
                    array_splice($dummyCamelizedParts, 1, 1, 'Responder');
                    $responderNamespace = implode('\\', $dummyCamelizedParts) . 'Responder';

                    $matchedRoute = [
                        'namespace' => $actionNamespace,
                        'responder' => $responderNamespace,
                        'action' => $dummyParts[count($dummyCamelizedParts) - 2],
                        'module' => strtolower($bundleName),
                        'params' => array_slice($dummyParts, count($dummyCamelizedParts) - 1, -1)
                    ];

{'fraction_non_alphanumeric': 0.1298888888888889, 'fraction_numerical': 0.002777777777777778, 'mean_word_length': 1.9903654485049833, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '3063163', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2441, 'n_words': 583}