<commit_before><?php

class Zigra_Router
{
    protected static $_controller;
    protected static $_action;
    protected static $_args;
    protected static $_defaults;
    protected static $_compiledRouteCollection;
    protected static $_routeCollection;
    private static $_instance;

    private function __construct()
    {
        self::$_routeCollection = new Zigra_Route_Collection();
        self::$_compiledRouteCollection = new Zigra_Route_Collection();
    }

    public static function singleton()
    {
        if (!isset(self::$_instance)) {
            $className = __CLASS__;
            self::$_instance = new $className;
        }
        return self::$_instance;
    }

    public static function route(Zigra_Request $request, $resetProperties = false)
    {
        self::singleton();

        $routefound = self::_Process($request, $resetProperties);
        if ($routefound) {
            $className = self::$_controller;
            $classFileName = '../app/controller/' . $className . 'Controller.php';
            try {
                if (file_exists($classFileName)) {
                    include_once $classFileName;

                    $fullClassName = ucfirst($className) . 'Controller';
                    $params = array_merge(self::$_defaults, self::$_args);
                    self::$_controller = new $fullClassName($request, $params);
                    if (is_callable(array(self::$_controller, self::$_action))) {
                        $registry = Zigra_Registry::getInstance();
                        $registry->set('controller', $className);
                        $registry->set('action', self::$_action);

                        call_user_func_array(array(self::$_controller, 'preExecute'), array($request));
                        call_user_func_array(array(self::$_controller, self::$_action), array($request));
                        call_user_func_array(array(self::$_controller, 'postExecute'), array($request));

                        return;
                    } else {
                        throw new Zigra_Exception('Impossibile richiamare il modulo: ' . $className . '->' . self::$_action);
                    }
                } else {
                    throw new Zigra_Exception('Impossibile trovare la classe ' . $className . ' (' . $classFileName . ')');

                    //self::route(new Zigra_Request('error'), true);
                }
            } catch (Zigra_Exception $e) {
                Zigra_Exception::renderError($e->getCode(), $e->getMessage());
            }
        } else {
            // 404 route not found
            // TODO: make this contruct indipendent from app
            $classFileName = '../app/controller/errorController.php';
            if (file_exists($classFileName)) {
                include_once $classFileName;
                $errorprocess = new errorController($request, self::$_args);

                $registry = Zigra_Registry::getInstance();
                $registry->set('controller', 'error');
                $registry->set('action', 'error404');

                call_user_func_array(array($errorprocess, 'error404'), array($request));
            } else {
                header('HTTP/1.0 404 Not Found');
                echo "<h1>404 Not Found</h1>";
                echo "The page that you have requested could not be found.";
                exit();
            }
        }
    }

    /**
     * Generate a short url for given arguments.
     *
     * @param Zigra_Request $request Request.
     * @param boolean $resetProperties If reset or not all singleton proprieties.
     *
     * @return boolean true when route is found or false if not found.
     */
    private static function _Process(Zigra_Request $request, $resetProperties = false)
    {
        $routes = array();
        $routes = self::$_compiledRouteCollection->routes;
        foreach ($routes as $route) {
            preg_match($route[0]['regex'], $request->getRequest(), $matches);
            //var_dump($route[0]['regex'], $request, $matches);

            if (count($matches)) {

                //array_shift($matches);
                //var_dump('matches', $matches);

                //rimuovi id numerici dall'array
                foreach ($matches as $k => &$v) {
                    if (is_numeric($k)) {
                        unset($matches[$k]);
                    }
                }
                $args = array();
                //var_dump('route 0', $route[0]['variables']);
                foreach ($route[0]['variables'] as $variable) {
                    $args[$variable] = array_shift($matches);
                }
                //var_dump('args', $args);
                self::$_controller = (!isset(self::$_controller) || $resetProperties) ? $route[0]['defaults']['controller'] : self::$_controller;
                self::$_action = (!isset(self::$_action) || $resetProperties) ? $route[0]['defaults']['action'] : self::$_action;
                self::$_args = (!isset(self::$_args) || $resetProperties) ? $args : self::$_args;
                self::$_defaults = (!isset(self::$_defaults) || $resetProperties) ? $route[0]['defaults'] : self::$_defaults;
                //var_dump(self::$_args);echo '<hr>';

                return true;
            }
        }
        self::$_controller = (!isset(self::$_controller) || $resetProperties) ? $request->getController() : self::$_controller;
        self::$_action = (!isset(self::$_action) || $resetProperties) ? $request->getAction() : self::$_action;
        self::$_args = (!isset(self::$_args) || $resetProperties) ? $request->getArgs() : self::$_args;
        //var_dump(self::$_args);echo '<hr>';

        return false;
    }
<commit_msg>Adding url query params (?param1=aaa&param2=bbbb) to args, taking precedence if an arg is already defined<commit_after><?php

class Zigra_Router
{
    protected static $_controller;
    protected static $_action;
    protected static $_args;
    protected static $_defaults;
    protected static $_compiledRouteCollection;
    protected static $_routeCollection;
    private static $_instance;

    private function __construct()
    {
        self::$_routeCollection = new Zigra_Route_Collection();
        self::$_compiledRouteCollection = new Zigra_Route_Collection();
    }

    public static function singleton()
    {
        if (!isset(self::$_instance)) {
            $className = __CLASS__;
            self::$_instance = new $className;
        }

        return self::$_instance;
    }

    public static function route(Zigra_Request $request, $resetProperties = false)
    {
        self::singleton();

        $routefound = self::_Process($request, $resetProperties);
        if ($routefound) {
            $className = self::$_controller;
            $classFileName = '../app/controller/' . $className . 'Controller.php';
            try {
                if (file_exists($classFileName)) {
                    include_once $classFileName;

                    $fullClassName = ucfirst($className) . 'Controller';
                    $params = array_merge(self::$_defaults, self::$_args);
                    self::$_controller = new $fullClassName($request, $params);
                    if (is_callable(array(self::$_controller, self::$_action))) {
                        $registry = Zigra_Registry::getInstance();
                        $registry->set('controller', $className);
                        $registry->set('action', self::$_action);

                        call_user_func_array(array(self::$_controller, 'preExecute'), array($request));
                        call_user_func_array(array(self::$_controller, self::$_action), array($request));
                        call_user_func_array(array(self::$_controller, 'postExecute'), array($request));

                        return;
                    } else {
                        throw new Zigra_Exception('Impossibile richiamare il modulo: ' . $className . '->' . self::$_action);
                    }
                } else {
                    throw new Zigra_Exception('Impossibile trovare la classe ' . $className . ' (' . $classFileName . ')');

                    //self::route(new Zigra_Request('error'), true);
                }
            } catch (Zigra_Exception $e) {
                Zigra_Exception::renderError($e->getCode(), $e->getMessage());
            }
        } else {
            // 404 route not found
            // TODO: make this contruct indipendent from app
            $classFileName = '../app/controller/errorController.php';
            if (file_exists($classFileName)) {
                include_once $classFileName;
                $errorprocess = new errorController($request, self::$_args);

                $registry = Zigra_Registry::getInstance();
                $registry->set('controller', 'error');
                $registry->set('action', 'error404');

                call_user_func_array(array($errorprocess, 'error404'), array($request));
            } else {
                header('HTTP/1.0 404 Not Found');
                echo "<h1>404 Not Found</h1>";
                echo "The page that you have requested could not be found.";
                exit();
            }
        }
    }

    /**
     * Generate a short url for given arguments.
     *
     * @param Zigra_Request $request Request.
     * @param boolean $resetProperties If reset or not all singleton proprieties.
     *
     * @return boolean true when route is found or false if not found.
     */
    private static function _Process(Zigra_Request $request, $resetProperties = false)
    {
        $routes = array();
        $routes = self::$_compiledRouteCollection->routes;
        foreach ($routes as $route) {
            $request_parts = parse_url($request->getRequest());
            preg_match($route[0]['regex'], $request_parts['path'], $matches);
            //preg_match($route[0]['regex'], $request->getRequest(), $matches);

            if (count($matches)) {
                //array_shift($matches);
                //var_dump('matches', $matches);

                //rimuovi id numerici dall'array
                foreach ($matches as $k => &$v) {
                    if (is_numeric($k)) {
                        unset($matches[$k]);
                    }
                }
                $args = array();
                //var_dump('route 0', $route[0]['variables']);
                foreach ($route[0]['variables'] as $variable) {
                    $args[$variable] = array_shift($matches);
                }
                //var_dump('args', $args);
                self::$_controller = (!isset(self::$_controller) || $resetProperties) ? $route[0]['defaults']['controller'] : self::$_controller;
                self::$_action = (!isset(self::$_action) || $resetProperties) ? $route[0]['defaults']['action'] : self::$_action;
                self::$_args = (!isset(self::$_args) || $resetProperties) ? $args : self::$_args;
                self::$_defaults = (!isset(self::$_defaults) || $resetProperties) ? $route[0]['defaults'] : self::$_defaults;

                if (isset($request_parts['query'])) {
                    parse_str($request_parts['query'], $query_args);
                    self::$_args = array_merge(self::$_args, $query_args);
                }
                //var_dump(self::$_args);echo '<hr>';
                //var_dump(self::$_defaults);echo '<hr>';

                return true;
            }
        }
        self::$_controller = (!isset(self::$_controller) || $resetProperties)
            ? $request->getController()
            : self::$_controller;
        self::$_action = (!isset(self::$_action) || $resetProperties) ? $request->getAction() : self::$_action;
        self::$_args = (!isset(self::$_args) || $resetProperties) ? $request->getArgs() : self::$_args;

        //var_dump(self::$_args);echo '<hr>';

        return false;
    }

{'fraction_non_alphanumeric': 0.14351541554959785, 'fraction_numerical': 0.004691689008042895, 'mean_word_length': 2.0, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 39, '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': '5860443', 'n_tokens_mistral': 3149, 'n_tokens_neox': 2976, 'n_words': 633}