<commit_before><?php

namespace ANDS\Registry\API;

class Router
{
    private $routes = [
        'get' => [], 'post' => [], 'put' => [], 'delete' => [], 'patch' => []
    ];

    private $prefix = "";
    private $controllerNamespace = "ANDS\Registry\API\Controller\\";

    function __construct($prefix = "") {
        $this->prefix = $prefix;
    }
    function __clone() {}

    public function route($methods, $pattern, $callback) {
        $methods = array_map('strtolower', $methods);
        $pattern = $this->prefix.$pattern;
        $pattern = rtrim($pattern, '/').'/';
        $pattern = '/^' . str_replace('/', '\/', $pattern) . '$/';
        foreach ($methods as $method) {
            $this->routes[$method][] = [$pattern => $callback];
        }
    }

    public function get($pattern, $callback) {
        $this->route(['get'], $pattern, $callback);
    }

    public function post($pattern, $callback)
    {
        $this->route(['get'], $pattern, $callback);
    }

    public function resource($resources, $controller) {
        $resources = explode('.', $resources);
        if (count($resources) ==  1) {
            $this->addResource($resources[0], $controller);
        }
        if (count($resources) == 2) {
            $this->addResource($resources[0].'/(\w+)/'.$resources[1], $controller);
        }
    }

    public function addResource($resource, $controller)
    {
        $this->route(['get'], "$resource/", "$controller@index");
        $this->route(['get'], "$resource/(\\w+)/", "$controller@show");
        $this->route(['post'], "$resource/", "$controller@add");
        $this->route(['put', 'patch'], "$resource/(\\w+)/", "$controller@update");
        $this->route(['delete'], "$resource/(\\w+)/", "$controller@destroy");
    }

    public function execute($url = null, $requestMethod = null) {
        if (!$url) {
            $url = $_SERVER['REQUEST_URI'];
        }

        if (!$requestMethod) {
            $requestMethod = $_SERVER['REQUEST_METHOD'];
        }

        if ($match = $this->getMatch($url, $requestMethod)) {
            return $this->callAction($match['callback'], $match['params']);
        }

        // no match
        throw new \Exception("No route match $url");
    }

    public function getMatch($url = null, $requestMethod = null)
    {
        if (!$url) {
            $url = $_SERVER['REQUEST_URI'];
        }

        $parsedUrl = parse_url(baseUrl());
        $path = array_key_exists('path', $parsedUrl) ? $parsedUrl['path'] : null;
        if ($path) {
            $url = str_replace($path, "", $url);
            $url = str_pad($url, strlen($url) + 1, "/", STR_PAD_LEFT);
        }
        

        if (!$requestMethod) {
            $requestMethod = $_SERVER['REQUEST_METHOD'];
        }

        foreach ($this->routes as $method => $matches) {
            $requestMethod = strtolower($requestMethod);
            if ($requestMethod != $method) {
                continue;
            }
            foreach ($matches as $match) {
                $pattern = array_keys($match)[0];
                $callback = $match[$pattern];
<commit_msg>Registry. Fixed an issue with Router url parsing leaving / as the base path<commit_after><?php

namespace ANDS\Registry\API;

class Router
{
    private $routes = [
        'get' => [], 'post' => [], 'put' => [], 'delete' => [], 'patch' => []
    ];

    private $prefix = "";
    private $controllerNamespace = "ANDS\Registry\API\Controller\\";

    function __construct($prefix = "") {
        $this->prefix = $prefix;
    }
    function __clone() {}

    public function route($methods, $pattern, $callback) {
        $methods = array_map('strtolower', $methods);
        $pattern = $this->prefix.$pattern;
        $pattern = rtrim($pattern, '/').'/';
        $pattern = '/^' . str_replace('/', '\/', $pattern) . '$/';
        foreach ($methods as $method) {
            $this->routes[$method][] = [$pattern => $callback];
        }
    }

    public function get($pattern, $callback) {
        $this->route(['get'], $pattern, $callback);
    }

    public function post($pattern, $callback)
    {
        $this->route(['get'], $pattern, $callback);
    }

    public function resource($resources, $controller) {
        $resources = explode('.', $resources);
        if (count($resources) ==  1) {
            $this->addResource($resources[0], $controller);
        }
        if (count($resources) == 2) {
            $this->addResource($resources[0].'/(\w+)/'.$resources[1], $controller);
        }
    }

    public function addResource($resource, $controller)
    {
        $this->route(['get'], "$resource/", "$controller@index");
        $this->route(['get'], "$resource/(\\w+)/", "$controller@show");
        $this->route(['post'], "$resource/", "$controller@add");
        $this->route(['put', 'patch'], "$resource/(\\w+)/", "$controller@update");
        $this->route(['delete'], "$resource/(\\w+)/", "$controller@destroy");
    }

    public function execute($url = null, $requestMethod = null) {
        if (!$url) {
            $url = $_SERVER['REQUEST_URI'];
        }

        if (!$requestMethod) {
            $requestMethod = $_SERVER['REQUEST_METHOD'];
        }

        if ($match = $this->getMatch($url, $requestMethod)) {
            return $this->callAction($match['callback'], $match['params']);
        }

        // no match
        throw new \Exception("No route match $url");
    }

    public function getMatch($url = null, $requestMethod = null)
    {
        if (!$url) {
            $url = $_SERVER['REQUEST_URI'];
        }

        $parsedUrl = parse_url(baseUrl());
        $path = array_key_exists('path', $parsedUrl) ? $parsedUrl['path'] : null;
        if ($path && $path !== "/") {
            $url = str_replace($path, "", $url);
            $url = str_pad($url, strlen($url) + 1, "/", STR_PAD_LEFT);
        }

        if (!$requestMethod) {
            $requestMethod = $_SERVER['REQUEST_METHOD'];
        }

        foreach ($this->routes as $method => $matches) {
            $requestMethod = strtolower($requestMethod);
            if ($requestMethod != $method) {
                continue;
            }
            foreach ($matches as $match) {
                $pattern = array_keys($match)[0];
                $callback = $match[$pattern];

{'fraction_non_alphanumeric': 0.1984478935698448, 'fraction_numerical': 0.0022172949002217295, 'mean_word_length': 2.268633540372671, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '1446514', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1802, 'n_words': 363}