<commit_before><?php

class Toro
{
    public static function serve($routes)
    {
        ToroHook::fire('before_request', compact('routes'));
        
        // Determine action (and set default)
        if (empty($_SERVER['REQUEST_METHOD']))
            $_SERVER['REQUEST_METHOD'] = 'GET';
        $action = strtolower($_SERVER['REQUEST_METHOD']); // e.g. get, put, post, delete

        $path_info = '/';
        if (!empty($_SERVER['PATH_INFO'])) {
            $path_info = $_SERVER['PATH_INFO'];
        }
        else if (!empty($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] !== '/index.php') {
            $path_info = $_SERVER['ORIG_PATH_INFO'];
        }
        else {
            if (!empty($_SERVER['REQUEST_URI'])) {
                $path_info = (strpos($_SERVER['REQUEST_URI'], '?') > 0) ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'];
            }
        }
        
        $discovered_handler = null;
        $regex_matches = array();
        $arguments = array();

        if (isset($routes[$path_info])) {
            $discovered_handler = $routes[$path_info];
        }
        else if ($routes) {
            $tokens = array(
                ':string' => '([a-zA-Z]+)',
                ':number' => '([0-9]+)',
                ':alpha'  => '([a-zA-Z0-9-_]+)',
                ':action'  => '([a-zA-Z0-9-_/]+)'
            );

            // Search through routes and find first match
            foreach ($routes as $pattern => $handler_name) {
                $pattern = strtr($pattern, $tokens);
                if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) {
                    $discovered_handler = $handler_name;
                    $regex_matches = $matches;
                    $params = isset($regex_matches[1]) ? explode("/", $regex_matches[1]) : array();
                    // e.g. getActionName($regex_matches), User::getProfile($vars)

                    // Determine action and arguments
                    if(count($params) > 1)
                    {
                        // @todo add different parsers here...possibly pass route function in routes.json
                        $int = 1;
                        foreach($params as $param)
                        {
                            // if even param
                            if($int % 2 == 0)
                                $action .= ucfirst($param);
                            else
                                $arguments[] = $param;
                            $int++;
                        }
                    } else {
                        unset($regex_matches[0]);
                        $arguments = $regex_matches; // Toro compatible
                    }

                    // error_log("regex_matches: ".print_r($regex_matches, true));
                    // error_log("action: $action");
                    // error_log("arguments: ".print_r($arguments, true));

                    break;
                }
            }
        }

        $result = null;
        $handler_instance = null;

        if ($discovered_handler) {
            if (is_string($discovered_handler)) {<commit_msg>add N url params to controller actions<commit_after><?php

class Toro
{
    public static function serve($routes)
    {
        ToroHook::fire('before_request', compact('routes'));
        
        // Determine action (and set default)
        if (empty($_SERVER['REQUEST_METHOD']))
            $_SERVER['REQUEST_METHOD'] = 'GET';
        $action = strtolower($_SERVER['REQUEST_METHOD']); // e.g. get, put, post, delete

        $path_info = '/';
        if (!empty($_SERVER['PATH_INFO'])) {
            $path_info = $_SERVER['PATH_INFO'];
        }
        else if (!empty($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] !== '/index.php') {
            $path_info = $_SERVER['ORIG_PATH_INFO'];
        }
        else {
            if (!empty($_SERVER['REQUEST_URI'])) {
                $path_info = (strpos($_SERVER['REQUEST_URI'], '?') > 0) ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'];
            }
        }
        
        $discovered_handler = null;
        $regex_matches = array();
        $arguments = array();

        if (isset($routes[$path_info])) {
            $discovered_handler = $routes[$path_info];
        }
        else if ($routes) {
            $tokens = array(
                ':string' => '([a-zA-Z]+)',
                ':number' => '([0-9]+)',
                ':alpha'  => '([a-zA-Z0-9-_]+)',
                ':action'  => '([a-zA-Z0-9-_/]+)'
            );

            // Search through routes and find first match
            foreach ($routes as $pattern => $handler_name) {
                $pattern = strtr($pattern, $tokens);
                if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) {
                    $discovered_handler = $handler_name;
                    $regex_matches = $matches;
                    $params = isset($regex_matches[1]) ? explode("/", $regex_matches[1]) : array();

                    // Determine action and arguments
                    if(count($params) > 1)
                    {
                        // @todo add different parsers here...possibly pass route function in routes.json
                        $action .= ucfirst($params[0]);
                        unset($params[0]);
                        // $int = 1;

                        foreach($params as $param)
                        {
                            $arguments[] = $param;
                            /*
                            // if even param
                            if($int % 2 == 0)
                                $action .= ucfirst($param);
                            else
                                $arguments[] = $param;
                            $int++;
                            */
                        }
                    } else {
                        unset($regex_matches[0]);
                        $arguments = $regex_matches; // Toro compatible
                    }

                    // error_log("regex_matches: ".print_r($regex_matches, true));
                    // error_log("action: $action");
                    // error_log("arguments: ".print_r($arguments, true));

                    break;
                }
            }
        }

        $result = null;
        $handler_instance = null;

        if ($discovered_handler) {
            if (is_string($discovered_handler)) {
{'fraction_non_alphanumeric': 0.1486030089038993, 'fraction_numerical': 0.00460546515198035, 'mean_word_length': 1.2724101848622253, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '7349300', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1662, 'n_words': 322}