<commit_before><?php
/**
 * OSRouter is a simple HTTP router for PHP.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright 2015 <NAME>, Onesimus Systems
 *
 * @license BSD 3-Clause
 */
namespace Onesimus\Router;

class Router
{
    private static $instance;
    private static $routes = [];

    private static $filters = [];

    private function __construct() {}

    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     *  Register route for a GET request
     */
    public static function get($url, $options)
    {
        self::register('GET', $url, $options);
        return;
    }

    /**
     *  Register a route for a POST request
     */
    public static function post($url, $options)
    {
        self::register('POST', $url, $options);
        return;

    }

    /**
     *  Register a route for any type of HTTP request
     */
    public static function any($url, $options)
    {
        self::register('ANY', $url, $options);
        return;

    }

    /**
     * Register a group of routes
     */
    public static function group(array $properties, array $routes)
    {
        if (isset($properties['filter'])) {
            if (!is_array($properties['filter'])) {
                $properties['filter'] = [$properties['filter']];
            }
        } else {
            $properties['filter'] = [];
        }

        $baseProperties = ['prefix' => '', 'rprefix' => ''];
        $properties = array_merge($baseProperties, $properties);

        // $routes: [0] = HTTP method, [1] = pattern, [2] = controller/method route
        foreach ($routes as $route) {
            $httpmethod = $route[0];

            if (!method_exists(__CLASS__, $httpmethod)) {
                continue;
            }

            $pattern = $properties['prefix'].$route[1];
            $controller = $route[2];
            $options = [
                'use' => $properties['rprefix'].$controller,
                'filter' => $properties['filter']
            ];
            self::$httpmethod($pattern, $options);
        }
    }

    /**
     *  Common register function, adds route to $routeList
     */
    private static function register($method, $url, $options)
    {
        $key = $method.'@'.$url;
        self::$routes[$key] = new Route($method, $url, $options);
    }

    /**
     * Get array of current routes
     */
    public static function getRoutes()
    {
        return self::$routes;
    }

    /**
     *  Initiate the routing for the given URL
     */
    public static function route(Http\Request $request)
    {
        $path = str_replace(rtrim($request->get('SERVER_NAME'), '/'), '', $request->FULL_URI);
        $key = $request->getMethod().'@'.$path;
        $keyAny = 'ANY@'.$path;

        $matchedRoute = null;
        $matchedScore = 0;

        if (isset(self::$routes[$key])) {
            $matchedRoute = self::$routes[$key];
        } elseif (isset(self::$routes[$keyAny])) {
            $matchedRoute = self::$routes[$keyAny];
        } else {
            foreach (self::$routes as $key2 => $route) {
                if ($route->getMethod() != 'ANY' && $route->getMethod() != $request->getMethod()) {
                    continue;
                }

                $score = $route->getScore($path);
                if ($score > $matchedScore) {
                    $matchedRoute = $route;
                    $matchedScore = $score;
                }
            }
        }

        if ($matchedRoute) {
            $matchedRoute->setUrl($path);
        }
        return $matchedRoute;
    }

    public static function filter($name, \Closure $callback)
    {
        self::$filters[$name] = $callback;
    }

    /**
     *  Perform any before actions on the route
     */
    public static function handleFilter($action = '')
    {
        if (!$action) {
            return true;<commit_msg>Forgot to give getScore the HTTP method<commit_after><?php
/**
 * OSRouter is a simple HTTP router for PHP.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright 2015 <NAME>, Onesimus Systems
 *
 * @license BSD 3-Clause
 */
namespace Onesimus\Router;

class Router
{
    private static $instance;
    private static $routes = [];

    private static $filters = [];

    private function __construct() {}

    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     *  Register route for a GET request
     */
    public static function get($url, $options)
    {
        self::register('GET', $url, $options);
        return;
    }

    /**
     *  Register a route for a POST request
     */
    public static function post($url, $options)
    {
        self::register('POST', $url, $options);
        return;

    }

    /**
     *  Register a route for any type of HTTP request
     */
    public static function any($url, $options)
    {
        self::register('ANY', $url, $options);
        return;

    }

    /**
     * Register a group of routes
     */
    public static function group(array $properties, array $routes)
    {
        if (isset($properties['filter'])) {
            if (!is_array($properties['filter'])) {
                $properties['filter'] = [$properties['filter']];
            }
        } else {
            $properties['filter'] = [];
        }

        $baseProperties = ['prefix' => '', 'rprefix' => ''];
        $properties = array_merge($baseProperties, $properties);

        // $routes: [0] = HTTP method, [1] = pattern, [2] = controller/method route
        foreach ($routes as $route) {
            $httpmethod = $route[0];

            if (!method_exists(__CLASS__, $httpmethod)) {
                continue;
            }

            $pattern = $properties['prefix'].$route[1];
            $controller = $route[2];
            $options = [
                'use' => $properties['rprefix'].$controller,
                'filter' => $properties['filter']
            ];
            self::$httpmethod($pattern, $options);
        }
    }

    /**
     *  Common register function, adds route to $routeList
     */
    private static function register($method, $url, $options)
    {
        $key = $method.'@'.$url;
        self::$routes[$key] = new Route($method, $url, $options);
    }

    /**
     * Get array of current routes
     */
    public static function getRoutes()
    {
        return self::$routes;
    }

    /**
     *  Initiate the routing for the given URL
     */
    public static function route(Http\Request $request)
    {
        $path = str_replace(rtrim($request->get('SERVER_NAME'), '/'), '', $request->FULL_URI);
        $key = $request->getMethod().'@'.$path;
        $keyAny = 'ANY@'.$path;

        $matchedRoute = null;
        $matchedScore = 0;

        if (isset(self::$routes[$key])) {
            $matchedRoute = self::$routes[$key];
        } elseif (isset(self::$routes[$keyAny])) {
            $matchedRoute = self::$routes[$keyAny];
        } else {
            foreach (self::$routes as $key2 => $route) {
                if ($route->getMethod() != 'ANY' && $route->getMethod() != $request->getMethod()) {
                    continue;
                }

                $score = $route->getScore($path, $request->getMethod());
                if ($score > $matchedScore) {
                    $matchedRoute = $route;
                    $matchedScore = $score;
                }
            }
        }

        if ($matchedRoute) {
            $matchedRoute->setUrl($path);
        }
        return $matchedRoute;
    }

    public static function filter($name, \Closure $callback)
    {
        self::$filters[$name] = $callback;
    }

    /**
     *  Perform any before actions on the route
     */
    public static function handleFilter($action = '')
    {
        if (!$action) {
            return true;
{'fraction_non_alphanumeric': 0.1521875391751285, 'fraction_numerical': 0.003259370690735866, 'mean_word_length': 1.8320908768193114, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 40, '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': '1821714', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2213, 'n_words': 532}