<commit_before><?php

namespace Core;

use Core\Exceptions\MethodNotAllowedException;
use Core\Exceptions\RouteNotFoundException;

class Router
{

    protected $path;

    protected $routes = [];

    protected $methods = [];

    public function setPath($path = '/')
    {
        $this->path = $path;
    }

    public function addRoute($uri , $handler, $methods = ['GET'])
    {
        if ($this->hasParameters($uri)) {
            $parseRequestUri = parseUrl($_SERVER['REQUEST_URI']);
            $parseRegisterUri = parseUrl($uri);

            // Checking if parameters exists on request uri
            if (count($parseRequestUri) > 1) {
                //Update registered route
                $uri = "/{$parseRegisterUri[0]}/{$parseRequestUri[1]}";

                //Save params
                if (is_array($handler))  {
                    $handler[] = $parseRequestUri[1];
                }
            }
        }

        $projectPath = getenv('PROJECT_PATH');
        if ($projectPath != '') {
            $this->routes['/'.$projectPath.$uri]  = $handler;
            $this->methods['/'.$projectPath.$uri] = $methods;
            return;
        }

        $this->routes[$uri]  = $handler;
        $this->methods[$uri] = $methods;
    }

    public function hasParameters($uri)
    {
        return strpos($uri, '{') !== false || strpos($uri, '}') !== false;
    }


    public function getResponse()
    {
        if (!isset($this->routes[$this->path])) {
            throw new RouteNotFoundException('No route registered for ' . $this->path);
        }

        if (!in_array($_SERVER['REQUEST_METHOD'], $this->methods[$this->path])) {
            throw new MethodNotAllowedException;
        }
        return $this->routes[$this->path];
    }
}
<commit_msg>Added suppport to pass parameters to method when the project path is not empty<commit_after><?php

namespace Core;

use Core\Exceptions\MethodNotAllowedException;
use Core\Exceptions\RouteNotFoundException;

class Router
{

    protected $path;

    protected $routes = [];

    protected $methods = [];

    public function setPath($path = '/')
    {
        $this->path = $path;
    }

    public function addRoute($uri , $handler, $methods = ['GET'])
    {
        $projectPath = getenv('PROJECT_PATH');

        if ($this->hasParameters($uri)) {
            $parseRequestUri = parseUrl($_SERVER['REQUEST_URI']);
            $parseRegisterUri = parseUrl($uri);

            //Delete project path from request uri
            if ($projectPath !== '') {
                $projectPathCollection = parseUrl($projectPath);
                $collectionUri = collect($parseRequestUri);
                $parseRequestUri = $collectionUri->diff($projectPathCollection)->values()->toArray();
            }

            // Checking if parameters exists on request uri
            if (count($parseRequestUri) > 1) {
                //Update registered route
                $uri = "/{$parseRegisterUri[0]}/{$parseRequestUri[1]}";

                //Save params
                if (is_array($handler))  {
                    $handler[] = $parseRequestUri[1];
                }
            }
        }

        if ($projectPath != '') {
            $this->routes['/'.$projectPath.$uri]  = $handler;
            $this->methods['/'.$projectPath.$uri] = $methods;
            return;
        }

        $this->routes[$uri]  = $handler;
        $this->methods[$uri] = $methods;

    }

    public function hasParameters($uri)
    {
        return strpos($uri, '{') !== false || strpos($uri, '}') !== false;
    }


    public function getResponse()
    {
        if (!isset($this->routes[$this->path])) {
            throw new RouteNotFoundException('No route registered for ' . $this->path);
        }

        if (!in_array($_SERVER['REQUEST_METHOD'], $this->methods[$this->path])) {
            throw new MethodNotAllowedException;
        }
        return $this->routes[$this->path];
    }
}

{'fraction_non_alphanumeric': 0.15137730603992924, 'fraction_numerical': 0.0020217336365933787, 'mean_word_length': 2.0329501915708814, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, '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': '569214', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1066, 'n_words': 215}