[
  {
    "path": "CONTRIBUTING.md",
    "content": "# Contributing\n\nContributions are **welcome** and will be fully **credited**.\n\nWe accept contributions via Pull Requests on [Github](https://github.com/robsonvleite/uploader).\n\n## Pull Requests\n\n- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).\n- **Document any change in behaviour** - Make sure the README and any other relevant documentation are kept up-to-date.\n- **Create topic branches** - Don't ask us to pull from your master branch.\n- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.\n- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.\n\n**Happy Coffee**!\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2018 Robson V. Leite @CoffeeCode\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE."
  },
  {
    "path": "README.md",
    "content": "# Router @CoffeeCode\n\n[![Maintainer](http://img.shields.io/badge/maintainer-@robsonvleite-blue.svg?style=flat-square)](https://twitter.com/robsonvleite)\n[![Source Code](http://img.shields.io/badge/source-coffeecode/router-blue.svg?style=flat-square)](https://github.com/robsonvleite/router)\n[![PHP from Packagist](https://img.shields.io/packagist/php-v/coffeecode/router.svg?style=flat-square)](https://packagist.org/packages/coffeecode/router)\n[![Latest Version](https://img.shields.io/github/release/robsonvleite/router.svg?style=flat-square)](https://github.com/robsonvleite/router/releases)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)\n[![Quality Score](https://img.shields.io/scrutinizer/g/robsonvleite/router.svg?style=flat-square)](https://scrutinizer-ci.com/g/robsonvleite/router)\n[![Total Downloads](https://img.shields.io/packagist/dt/coffeecode/router.svg?style=flat-square)](https://packagist.org/packages/coffeecode/router)\n\n###### Small, simple and uncomplicated. The router is a PHP route components with abstraction for MVC. Prepared with RESTfull verbs (GET, POST, PUT, PATCH and DELETE), works on its own layer in isolation and can be integrated without secrets to your application.\n\nPequeno, simples e descomplicado. O router é um componentes de rotas PHP com abstração para MVC. Preparado com verbos\nRESTfull (GET, POST, PUT, PATCH e DELETE), trabalha em sua própria camada de forma isolada e pode ser integrado sem\nsegredos a sua aplicação.\n\n## About CoffeeCode\n\n###### CoffeeCode is a set of small and optimized PHP components for common tasks. Held by Robson V. Leite and the UpInside team. With them you perform routine tasks with fewer lines, writing less and doing much more.\n\nCoffeeCode é um conjunto de pequenos e otimizados componentes PHP para tarefas comuns. Mantido por Robson V. Leite e a\nequipe UpInside. Com eles você executa tarefas rotineiras com poucas linhas, escrevendo menos e fazendo muito mais.\n\n### Highlights\n\n- Router class with all RESTful verbs (Classe router com todos os verbos RESTful)\n- Optimized dispatch with total decision control (Despacho otimizado com controle total de decisões)\n- Requesting Spoofing for Local Verbalization (Falsificador (Spoofing) de requisição para verbalização local)\n- It's very simple to create routes for your application or API (É muito simples criar rotas para sua aplicação ou API)\n- Trigger and data carrier for the controller (Gatilho e transportador de dados para o controloador)\n- Composer ready and PSR-2 compliant (Pronto para o composer e compatível com PSR-2)\n\n## Installation\n\nRouter is available via Composer:\n\n```bash\n\"coffeecode/router\": \"2.0.*\"\n```\n\nor run\n\n```bash\ncomposer require coffeecode/router\n```\n\n## Documentation\n\n###### For details on how to use the router, see the sample folder with details in the component directory. To use the router you need to redirect your route routing navigation (index.php) where all traffic must be handled. The example below shows how:\n\nPara mais detalhes sobre como usar o router, veja a pasta de exemplo com detalhes no diretório do componente. Para usar\no router é preciso redirecionar sua navegação para o arquivo raiz de rotas (index.php) onde todo o tráfego deve ser\ntratado. O exemplo abaixo mostra como:\n\n#### Apache\n\n```apacheconfig\nRewriteEngine On\n#Options All -Indexes\n\n## ROUTER WWW Redirect.\n#RewriteCond %{HTTP_HOST} !^www\\. [NC]\n#RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\n\n## ROUTER HTTPS Redirect\n#RewriteCond %{HTTP:X-Forwarded-Proto} !https\n#RewriteCond %{HTTPS} off\n#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\n\n# ROUTER URL Rewrite\nRewriteCond %{SCRIPT_FILENAME} !-f\nRewriteCond %{SCRIPT_FILENAME} !-d\nRewriteRule ^(.*)$ index.php?route=/$1 [L,QSA]\n```\n\n#### Nginx\n\n````nginxconfig\nlocation / {\n  if ($script_filename !~ \"-f\"){\n    rewrite ^(.*)$ /index.php?route=/$1 break;\n  }\n}\n````\n\n##### Routes\n\n```php\n<?php\n\nuse CoffeeCode\\Router\\Router;\n\n$router = new Router(\"https://www.youdomain.com\");\n\n/**\n * routes\n * The controller must be in the namespace Test\\Controller\n * this produces routes for route, route/$id, route/{$id}/profile, etc.\n */\n$router->namespace(\"Test\");\n\n$router->get(\"/route\", \"Controller:method\");\n$router->post(\"/route/{id}\", \"Controller:method\");\n$router->put(\"/route/{id}/profile\", \"Controller:method\");\n$router->patch(\"/route/{id}/profile/{photo}\", \"Controller:method\");\n$router->delete(\"/route/{id}\", \"Controller:method\");\n\n/**\n * group by routes and namespace\n * this produces routes for /admin/route and /admin/route/$id\n * The controller must be in the namespace Dash\\Controller\n */\n$router->group(\"admin\")->namespace(\"Dash\");\n\n$router->get(\"/route\", \"Controller:method\");\n$router->post(\"/route/{id}\", \"Controller:method\");\n\n/**\n * sub group\n */\n$router->group(\"admin/support\");\n\n$router->get(\"/tickets\", \"Controller:method\");\n$router->post(\"/ticket/{id}\", \"Controller:method\");\n\n/**\n * Group Error\n * This monitors all Router errors. Are they: 400 Bad Request, 404 Not Found, 405 Method Not Allowed and 501 Not Implemented\n */\n$router->group(\"error\")->namespace(\"Test\");\n$router->get(\"/{errcode}\", \"Coffee:notFound\");\n\n/**\n * This method executes the routes\n */\n$router->dispatch();\n\n/*\n * Redirect all errors\n */\nif ($router->error()) {\n    $router->redirect(\"/error/{$router->error()}\");\n}\n```\n\n##### Named\n\n```php\n<?php\n\nuse CoffeeCode\\Router\\Router;\n\n$router = new Router(\"https://www.youdomain.com\");\n\n/**\n * routes\n * The controller must be in the namespace Test\\Controller\n */\n$router->namespace(\"Test\")->group(\"name\");\n\n$router->get(\"/\", \"Name:home\", \"name.home\");\n$router->get(\"/hello\", \"Name:hello\", \"name.hello\");\n$router->get(\"/redirect\", \"Name:redirect\", \"name.redirect\");\n\n/**\n * This method executes the routes\n */\n$router->dispatch();\n\n/*\n * Redirect all errors\n */\nif ($router->error()) {\n    $router->redirect(\"name.hello\");\n}\n```\n\n###### Named Controller Example\n\n```php\n<?php\n\nclass Name\n{\n    public function __construct($router)\n    {\n        $this->router = $router;\n    }\n\n    public function home(): void\n    {\n        echo \"<h1>Home</h1>\";\n        echo \"<p>\", $this->router->route(\"name.home\"), \"</p>\";\n        echo \"<p>\", $this->router->route(\"name.hello\"), \"</p>\";\n        echo \"<p>\", $this->router->route(\"name.redirect\"), \"</p>\";\n    }\n\n    public function redirect(): void\n    {\n        $this->router->redirect(\"name.hello\");\n    }\n}\n```\n\n###### Named Params\n\n````php\n<?php\n\nuse CoffeeCode\\Router\\Router;\n\n$router = new Router(\"https://www.youdomain.com\");\n\n$this->router->route(\"name.params\", [\n    \"category\" => 22,\n    \"page\" => 2\n]);\n\n//result\n//https://www.youdomain.com/name/params/22/page/2\n\n$this->router->route(\"name.params\", [\n    \"category\" => 22,\n    \"page\" => 2,\n    \"argument1\" => \"most filter\",\n    \"argument2\" => \"most search\"\n]);\n\n//result\n//https://www.youdomain.com/name/params/22/page/2?argument1=most+filter&argument2=most+search\n````\n\n##### Callable\n\n```php\n<?php\n\nuse CoffeeCode\\Router\\Router;\n\n$router = new Router(\"https://www.youdomain.com\");\n\n/**\n * GET httpMethod\n */\n$router->get(\"/\", function ($data) {\n    $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n    echo \"<h1>GET :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n});\n\n/**\n * GET httpMethod and Route\n */\n $router->get(\"/\", function ($data, Router $route) {\n    $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n    echo \"<h1>GET :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n    var_dump($route->current());\n});\n\n/**\n * POST httpMethod\n */\n$router->post(\"/\", function ($data) {\n    $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n    echo \"<h1>POST :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n});\n\n/**\n * PUT spoofing and httpMethod\n */\n$router->put(\"/\", function ($data) {\n    $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n    echo \"<h1>PUT :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n});\n\n/**\n * PATCH spoofing and httpMethod\n */\n$router->patch(\"/\", function ($data) {\n    $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n    echo \"<h1>PATCH :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n});\n\n/**\n * DELETE spoofing and httpMethod\n */\n$router->delete(\"/\", function ($data) {\n    $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n    echo \"<h1>DELETE :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n});\n\n$router->dispatch();\n```\n\n##### Simple Middleware\n\n```php\n<?php\n\nuse CoffeeCode\\Router\\Router;\n\n$router = new Router(\"https://www.youdomain.com\");\n\n//simple\n$router->get(\"/edit/{id}\", \"Coffee:edit\", middleware: \\Http\\Guest::class);\n$router->get(\"/denied\", \"Coffee:denied\", \"coffe.denied\", \\Http\\Group::class);\n\n//multiple\n$router->get(\"/logado\", \"Coffee:logged\", middleware: [\\Http\\Guest::class, \\Http\\Group::class]);\n\n//callable\n$router->get(\"/call\", function ($data, Router $router){\n    //code here\n}, middleware: \\Http\\Guest::class);\n```\n\n##### Simple Middleware Group\n\n```php\n<?php\n\nuse CoffeeCode\\Router\\Router;\n\n$router = new Router(\"https://www.youdomain.com\");\n\n//group single or multiple\n$router->group(\"name\", \\Http\\Guest::class);\n$router->get(\"/\", \"Name:home\", \"name.home\");\n$router->get(\"/hello\", \"Name:hello\", \"name.hello\");\n$router->get(\"/redirect\", \"Name:redirect\", \"name.redirect\");\n```\n\n##### Simple Middleware Class Example\n\n```php\n<?php\n\nnamespace Http;\n\nuse CoffeeCode\\Router\\Router;\n\nclass User\n{\n    public function handle(Router $router): bool\n    {\n        $user = true;\n        if ($user) {\n            var_dump($router->current());\n            return true;\n        }\n        return false;\n    }\n}\n```\n\n##### Form Spoofing\n\n###### This example shows how to access the routes (PUT, PATCH, DELETE) from the application. You can see more details in the sample folder. From an attention to the _method field, it can be of the hidden type.\n\nEsse exemplo mostra como acessar as rotas (PUT, PATCH, DELETE) a partir da aplicação. Você pode ver mais detalhes na\npasta de exemplo. De uma atenção para o campo _method, ele pode ser do tipo hidden.\n\n```html\n\n<form action=\"\" method=\"POST\">\n    <select name=\"_method\">\n        <option value=\"POST\">POST</option>\n        <option value=\"PUT\">PUT</option>\n        <option value=\"PATCH\">PATCH</option>\n        <option value=\"DELETE\">DELETE</option>\n    </select>\n\n    <input type=\"text\" name=\"first_name\" value=\"Robson\"/>\n    <input type=\"text\" name=\"last_name\" value=\"Leite\"/>\n    <input type=\"text\" name=\"email\" value=\"cursos@upinside.com.br\"/>\n\n    <button>CoffeeCode</button>\n</form>\n```\n\n##### PHP cURL example\n\n```php\n<?php\n\n$curl = curl_init();\n\ncurl_setopt_array($curl, array(\n  CURLOPT_URL => \"http://localhost/coffeecode/router/example/spoofing/\",\n  CURLOPT_RETURNTRANSFER => true,\n  CURLOPT_ENCODING => \"\",\n  CURLOPT_MAXREDIRS => 10,\n  CURLOPT_TIMEOUT => 30,\n  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,\n  CURLOPT_CUSTOMREQUEST => \"PUT\",\n  CURLOPT_POSTFIELDS => \"first_name=Robson&last_name=Leite&email=cursos%40upinside.com.br\",\n  CURLOPT_HTTPHEADER => array(\n    \"Cache-Control: no-cache\",\n    \"Content-Type: application/x-www-form-urlencoded\"\n  ),\n));\n\n$response = curl_exec($curl);\n$err = curl_error($curl);\n\ncurl_close($curl);\n\nif ($err) {\n  echo \"cURL Error #:\" . $err;\n} else {\n  echo $response;\n}\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/robsonvleite/router/blob/master/CONTRIBUTING.md) for details.\n\n## Support\n\n###### Security: If you discover any security related issues, please email cursos@upinside.com.br instead of using the issue tracker.\n\nSe você descobrir algum problema relacionado à segurança, envie um e-mail para cursos@upinside.com.br em vez de usar o\nrastreador de problemas.\n\nThank you\n\n## Credits\n\n- [Robson V. Leite](https://github.com/robsonvleite) (Developer)\n- [UpInside Treinamentos](https://github.com/upinside) (Team)\n- [All Contributors](https://github.com/robsonvleite/router/contributors) (This Rock)\n\n## License\n\nThe MIT License (MIT). Please see [License File](https://github.com/robsonvleite/router/blob/master/LICENSE) for more\ninformation."
  },
  {
    "path": "composer.json",
    "content": "{\n  \"name\": \"coffeecode/router\",\n  \"description\": \"A classic CoffeeCode Router is easy, fast and extremely uncomplicated. Create and manage your routes in minutes!\",\n  \"keywords\": [\n    \"CoffeeCode\",\n    \"UpInside\",\n    \"Router\",\n    \"Route\",\n    \"Routes\"\n  ],\n  \"homepage\": \"http://www.upinside.com.br\",\n  \"license\": \"MIT\",\n  \"authors\": [\n    {\n      \"name\": \"Robson V. Leite\",\n      \"email\": \"cursos@upinside.com.br\",\n      \"role\": \"Developer\"\n    }\n  ],\n  \"require\": {\n    \"php\": \">=8.0\"\n  },\n  \"autoload\": {\n    \"psr-4\": {\n      \"CoffeeCode\\\\Router\\\\\": \"src\"\n    }\n  }\n}\n"
  },
  {
    "path": "exemple/controller/.htaccess",
    "content": "RewriteEngine On\nOptions All -Indexes\n\n# ROUTER WWW Redirect.\nRewriteCond %{HTTP_HOST} !^www\\. [NC]\nRewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\n\n# ROUTER HTTPS Redirect\nRewriteCond %{HTTP:X-Forwarded-Proto} !https\nRewriteCond %{HTTPS} off\nRewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\n\n# ROUTER URL Rewrite\nRewriteCond %{SCRIPT_FILENAME} !-f\nRewriteCond %{SCRIPT_FILENAME} !-d\nRewriteRule ^(.*)$ index.php?route=/$1 [L,QSA]"
  },
  {
    "path": "exemple/controller/Http/Group.php",
    "content": "<?php\n\nnamespace Http;\n\nclass Group\n{\n    public function handle(): bool\n    {\n        echo \"<p><i>O middleware <b>Group</b> foi executado!</i></p>\";\n\n        $group = true;\n        if ($group) {\n            return true;\n        }\n        return false;\n    }\n}"
  },
  {
    "path": "exemple/controller/Http/Guest.php",
    "content": "<?php\n\nnamespace Http;\n\nclass Guest\n{\n    public function handle(): bool\n    {\n        echo \"<p><i>O middleware <b>Guest</b> foi executado!</i></p>\";\n\n        $guest = true;\n        if ($guest) {\n            return true;\n        }\n        return false;\n    }\n}\n"
  },
  {
    "path": "exemple/controller/Http/Middlewares.php",
    "content": "<?php\n\nnamespace Http;\n\nclass Middlewares\n{\n    const GUEST = Guest::class;\n    const USER = User::class;\n    CONST GROUP = Group::class;\n}"
  },
  {
    "path": "exemple/controller/Http/User.php",
    "content": "<?php\n\nnamespace Http;\n\nuse CoffeeCode\\Router\\Router;\n\nclass User\n{\n    public function handle(Router $router): bool\n    {\n        echo \"<p><i>O middleware <b>User</b> foi executado!</i></p>\";\n\n        $user = filter_input(INPUT_GET, \"user\", FILTER_VALIDATE_BOOL);\n        if ($user) {\n            return true;\n        }\n\n        echo \"<h1>Acces Denied!</h1>\";\n        echo \"<a href='{$router->route(\"coffe.denied\", [\"user\" => true])}'>Simular Usuário</a>\";\n        return false;\n    }\n}"
  },
  {
    "path": "exemple/controller/Test/Coffee.php",
    "content": "<?php\n\nnamespace Test;\n\nuse CoffeeCode\\Router\\Router;\n\n/**\n * Class Coffee MVC :: CONTROLLER\n * @package Test\n */\nclass Coffee\n{\n    /**\n     * Coffee constructor.\n     */\n    public function __construct(Router $router)\n    {\n        $url = BASE;\n        $rand = rand(44, 244);\n\n        echo \"<h1>Router @CoffeeCode</h1>\";\n        echo \"<p>Normal routes:</p>\";\n        echo \"<nav>\n            <a href='{$url}'>Home</a> | \n            <a href='{$url}/edit/{$rand}'>Edit</a> | \n            <a href='{$url}/logado/?user=true'>Logado</a> | \n            <a href='{$router->route(\"coffe.denied\")}'>Negado</a> | \n            <a href='{$url}/error/'>Error</a>\n        </nav>\";\n\n        echo \"<p>Group routes:</p>\";\n        echo \"<nav>\n            <a href='{$url}/admin'>Admin</a> | \n            <a href='{$url}/admin/user/{$rand}'>Edit User</a> | \n            <a href='{$url}/admin/user/{$rand}/profile'>Perfil</a> | \n            <a href='{$url}/admin/user/{$rand}/profile/imagem-{$rand}.jpg'>Photo</a> \n        </nav>\";\n\n        echo \"<p>Named and call routes:</p>\";\n        echo \"<nav>\n            <a href='{$url}/name'>Named</a> | \n            <a href='{$url}/call'>Call Current</a> | \n            <a href='{$url}/call/coffecode'>Call Current + App</a>\n        </nav>\";\n    }\n\n    /**\n     * @param array $data\n     */\n    public function home(array $data): void\n    {\n        echo \"<h3>\", __METHOD__, \"::\", $_SERVER[\"REQUEST_METHOD\"], \"</h3><hr>\";\n        echo \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n\n    /**\n     * @param array $data\n     */\n    public function edit(array $data): void\n    {\n        echo \"<h3>\", __METHOD__, \"::\", $_SERVER[\"REQUEST_METHOD\"], \"</h3><hr>\";\n\n        echo \"<form name='coffeecode' method='post' enctype='multipart/form-data'>\n            <input name=\\\"first_name\\\" value=\\\"Robson\\\">\n            <input name=\\\"last_name\\\" value=\\\"V. Leite\\\">\n            <input name=\\\"email\\\" value=\\\"cursos@upinside.com.br\\\">\n            <button>@CoffeeCode</button>\n        </form>\";\n\n        echo \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n\n    /**\n     * @param array $data\n     */\n    public function notfound(array $data): void\n    {\n        echo \"<h3>Whoops!</h3>\", \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n\n    /**\n     * @param array $data\n     */\n    public function admin(array $data): void\n    {\n        echo \"<h3>Admin Group:</h3>\", \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n\n    /**\n     * @param array $data\n     */\n    public function logged(array $data)\n    {\n        echo \"<h3>Logged</h3>\", \"<p>Essa tela simula execução de múltiplos middlewares</p>\", \"<pre>\", print_r(\n            $data,\n            true\n        ), \"</pre>\";\n    }\n\n    /**\n     * @param array $data\n     */\n    public function denied(array $data)\n    {\n        echo \"<h3>Acessou com sucesso: (Access Denied)</h3>\", \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n}"
  },
  {
    "path": "exemple/controller/Test/Name.php",
    "content": "<?php\n\nnamespace Test;\n\nuse CoffeeCode\\Router\\Router;\n\nclass Name\n{\n    /** @var Router */\n    private Router $router;\n\n    public function __construct($router)\n    {\n        $this->router = $router;\n        $params = [\"category\" => 23213, \"page\" => 2];\n\n        echo \"<p>Named routes:</p>\";\n        echo \"<nav>\n            <a href='{$this->router->route(\"name.home\")}'>Home</a> | \n            <a href='{$this->router->route(\"name.hello\")}'>Hello</a> | \n            <a href='{$this->router->route(\"name.params\", $params)}'>Params</a> | \n            <a href='{$this->router->route(\"name.redirect\")}'>Redirect Back</a>\n        </nav>\";\n    }\n\n    public function home(): void\n    {\n        echo \"<h3>Home</h3>\";\n        echo \"<p>\", $this->router->route(\"name.home\"), \"</p>\";\n        echo \"<p>\", $this->router->route(\"name.hello\"), \"</p>\";\n        echo \"<p>\", $this->router->route(\"name.redirect\"), \"</p>\";\n    }\n\n    public function hello(): void\n    {\n        echo \"<h3>Hello World</h3>\";\n        echo \"<a href='{$this->router->route(\"name.params\", [\"category\" => 6, \"page\" => 1])}'>Route Params</a>\";\n    }\n\n    public function params(array $data): void\n    {\n        echo \"<h3>Params</h3>\";\n        var_dump($data, $this->router->current());\n    }\n\n    public function redirect(array $data): void\n    {\n        if ($data) {\n            $this->router->redirect(\"name.params\", $data);\n        }\n\n        $this->router->redirect(BASE);\n    }\n}"
  },
  {
    "path": "exemple/controller/index.php",
    "content": "<?php\n\nrequire dirname(__DIR__, 2) . \"/vendor/autoload.php\";\nrequire __DIR__ . \"/Test/Coffee.php\";\nrequire __DIR__ . \"/Test/Name.php\";\n\n/*\n * Middleware example classes\n */\nrequire __DIR__ . \"/Http/Middlewares.php\";\nrequire __DIR__ . \"/Http/Guest.php\";\nrequire __DIR__ . \"/Http/User.php\";\nrequire __DIR__ . \"/Http/Group.php\";\n\nuse CoffeeCode\\Router\\Router;\nuse Http\\Middlewares as Middleware;\n\nconst BASE = \"https://www.localhost/coffeecode/router/exemple/controller\";\n$router = new Router(BASE);\n\n/**\n * routes\n */\n$router->namespace(\"Test\");\n\n$router->get(\"/\", \"Coffee:home\");\n$router->get(\"/edit/{id}\", \"Coffee:edit\", middleware: Middleware::GUEST);\n$router->post(\"/edit/{id}\", \"Coffee:edit\");\n$router->get(\"/logado\", \"Coffee:logged\", middleware: [\\Http\\Guest::class, \\Http\\User::class]);\n$router->get(\"/negado\", \"Coffee:denied\", \"coffe.denied\", Middleware::USER);\n\n/**\n * group by routes and namespace\n */\n$router->group(\"admin\", \\Http\\Group::class);\n\n$router->get(\"/\", \"Coffee:admin\");\n$router->get(\"/user/{id}\", \"Coffee:admin\");\n$router->get(\"/user/{id}/profile\", \"Coffee:admin\", \\Http\\Guest::class);\n$router->get(\"/user/{id}/profile/{photo}\", \"Coffee:admin\");\n\n/**\n * named routes and middlewares\n */\n$router->group(\"name\");\n\n$router->get(\"/\", \"Name:home\", \"name.home\");\n$router->get(\"/hello\", \"Name:hello\", \"name.hello\", \\Http\\Guest::class);\n$router->get(\"/params/{category}/page/{page}\", \"name:params\", \"name.params\");\n$router->get(\"/redirect\", \"Name:redirect\", \"name.redirect\", Middleware::GUEST);\n$router->get(\"/redirect/{category}/{page}\", \"name:redirect\", \"name.redirect.params\");\n\n/**\n * call route and group middleware\n */\n$router->group(\"call\", Middleware::GUEST);\n$router->get(\n    \"/\",\n    function ($data, Router $route) {\n        var_dump($data, $route->current());\n\n        echo \"<a href='{$route->home()}' title='voltar'>voltar</a>\";\n    }\n);\n$router->get(\n    \"/{app}/\",\n    function ($data, Router $route) {\n        var_dump($data, $route->current());\n\n        echo \"<a href='{$route->home()}' title='voltar'>voltar</a>\";\n    }\n);\n\n/**\n * Group Error\n */\n$router->namespace(\"Test\")->group(\"error\");\n$router->get(\"/{errcode}\", \"Coffee:notFound\");\n\n/**\n * execute\n */\n$router->dispatch();\n\nif ($router->error()) {\n    //var_dump($router->error());\n    $router->redirect(\"/error/{$router->error()}\");\n}"
  },
  {
    "path": "exemple/spoofing/.htaccess",
    "content": "RewriteEngine On\nOptions All -Indexes\n\n# ROUTER URL Rewrite\nRewriteCond %{SCRIPT_FILENAME} !-f\nRewriteCond %{SCRIPT_FILENAME} !-d\nRewriteRule ^(.*)$ index.php?route=/$1 [L,QSA]"
  },
  {
    "path": "exemple/spoofing/index.php",
    "content": "<?php\n\nrequire dirname(__DIR__, 2) . \"/vendor/autoload.php\";\n\nuse CoffeeCode\\Router\\Router;\n\ndefine(\"BASE\", \"https://www.localhost/coffeecode/router/exemple/controller\");\n$router = new Router(BASE);\n\n/**\n * GET httpMethod\n */\n$router->get(\n    \"/\",\n    function ($data) {\n        $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n        echo \"<h1>GET :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n);\n\n/**\n * POST httpMethod\n */\n$router->post(\n    \"/\",\n    function ($data) {\n        $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n        echo \"<h1>POST :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n);\n\n/**\n * PUT spoofing and httpMethod\n */\n$router->put(\n    \"/\",\n    function ($data) {\n        $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n        echo \"<h1>PUT :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n);\n\n/**\n * PATCH spoofing and httpMethod\n */\n$router->patch(\n    \"/\",\n    function ($data) {\n        $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n        echo \"<h1>PATCH :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n);\n\n/**\n * DELETE spoofing and httpMethod\n */\n$router->delete(\n    \"/\",\n    function ($data) {\n        $data = [\"realHttp\" => $_SERVER[\"REQUEST_METHOD\"]] + $data;\n        echo \"<h1>DELETE :: Spoofing</h1>\", \"<pre>\", print_r($data, true), \"</pre>\";\n    }\n);\n\n$router->dispatch();\n?>\n\n<form action=\"\" method=\"POST\">\n    <select name=\"_method\">\n        <option value=\"POST\">POST</option>\n        <option value=\"PUT\">PUT</option>\n        <option value=\"PATCH\">PATCH</option>\n        <option value=\"DELETE\">DELETE</option>\n    </select>\n\n    <input type=\"text\" name=\"first_name\" value=\"Robson\"/>\n    <input type=\"text\" name=\"last_name\" value=\"Leite\"/>\n    <input type=\"text\" name=\"email\" value=\"cursos@upinside.com.br\"/>\n\n    <button>CoffeeCode</button>\n</form>"
  },
  {
    "path": "src/Dispatch.php",
    "content": "<?php\n\nnamespace CoffeeCode\\Router;\n\n/**\n * Class CoffeeCode Dispatch\n *\n * @author Robson V. Leite <https://github.com/robsonvleite>\n * @package CoffeeCode\\Router\n */\nabstract class Dispatch\n{\n    use RouterTrait;\n\n    /** @var string */\n    protected string $projectUrl;\n\n    /** @var string */\n    protected string $httpMethod;\n\n    /** @var string */\n    protected string $path;\n\n    /** @var array|null */\n    protected ?array $route = null;\n\n    /** @var array */\n    protected array $routes;\n\n    /** @var string */\n    protected string $separator;\n\n    /** @var string|null */\n    protected ?string $namespace = null;\n\n    /** @var string|null */\n    protected ?string $group = null;\n\n    /** @var array|null */\n    protected ?array $middleware = null;\n\n    /** @var array|null */\n    protected ?array $data = null;\n\n    /** @var int */\n    protected ?int $error = null;\n\n    /** @const int Bad Request */\n    public const BAD_REQUEST = 400;\n\n    /** @const int Not Found */\n    public const NOT_FOUND = 404;\n\n    /** @const int Method Not Allowed */\n    public const METHOD_NOT_ALLOWED = 405;\n\n    /** @const int Not Implemented */\n    public const NOT_IMPLEMENTED = 501;\n\n    /**\n     * Dispatch constructor.\n     *\n     * @param string $projectUrl\n     * @param null|string $separator\n     */\n    public function __construct(string $projectUrl, ?string $separator = \":\")\n    {\n        $this->projectUrl = (substr($projectUrl, \"-1\") == \"/\" ? substr($projectUrl, 0, -1) : $projectUrl);\n        $this->path = rtrim((filter_input(INPUT_GET, \"route\", FILTER_DEFAULT) ?? \"/\"), \"/\");\n        $this->separator = ($separator ?? \":\");\n        $this->httpMethod = $_SERVER['REQUEST_METHOD'];\n    }\n\n    /**\n     * @return array\n     */\n    public function __debugInfo()\n    {\n        return $this->routes;\n    }\n\n    /**\n     * @param string $name\n     * @param array|null $data\n     * @return string|null\n     */\n    public function route(string $name, array $data = null): ?string\n    {\n        foreach ($this->routes as $http_verb) {\n            foreach ($http_verb as $route_item) {\n                if (!empty($route_item[\"name\"]) && $route_item[\"name\"] == $name) {\n                    return $this->treat($route_item, $data);\n                }\n            }\n        }\n        return null;\n    }\n\n    /**\n     * @param null|string $namespace\n     * @return Dispatch\n     */\n    public function namespace(?string $namespace): Dispatch\n    {\n        $this->namespace = ($namespace ? ucwords($namespace) : null);\n        return $this;\n    }\n\n    /**\n     * @param null|string $group\n     * @return Dispatch\n     */\n    public function group(?string $group, array|string $middleware = null): Dispatch\n    {\n        $this->group = ($group ? trim($group, \"/\") : null);\n        $this->middleware = $middleware ? [$this->group => $middleware] : null;\n        return $this;\n    }\n\n    /**\n     * @return null|array\n     */\n    public function data(): ?array\n    {\n        return $this->data;\n    }\n\n    /**\n     * @return object|null\n     */\n    public function current(): ?object\n    {\n        return (object)array_merge(\n            [\n                \"namespace\" => $this->namespace,\n                \"group\" => $this->group,\n                \"path\" => $this->path\n            ],\n            $this->route ?? []\n        );\n    }\n\n    /**\n     * @return string\n     */\n    public function home(): string\n    {\n        return $this->projectUrl;\n    }\n\n    /**\n     * @param string $route\n     * @param array|null $data\n     */\n    public function redirect(string $route, array $data = null): void\n    {\n        if ($name = $this->route($route, $data)) {\n            header(\"Location: {$name}\");\n            exit;\n        }\n\n        if (filter_var($route, FILTER_VALIDATE_URL)) {\n            header(\"Location: {$route}\");\n            exit;\n        }\n\n        $route = (substr($route, 0, 1) == \"/\" ? $route : \"/{$route}\");\n        header(\"Location: {$this->projectUrl}{$route}\");\n        exit;\n    }\n\n    /**\n     * @return null|int\n     */\n    public function error(): ?int\n    {\n        return $this->error;\n    }\n\n    /**\n     * @return bool\n     */\n    public function dispatch(): bool\n    {\n        if (empty($this->routes) || empty($this->routes[$this->httpMethod])) {\n            $this->error = self::NOT_IMPLEMENTED;\n            return false;\n        }\n\n        $this->route = null;\n        foreach ($this->routes[$this->httpMethod] as $key => $route) {\n            if (preg_match(\"~^\" . $key . \"$~\", $this->path, $found)) {\n                $this->route = $route;\n            }\n        }\n\n        return $this->execute();\n    }\n}"
  },
  {
    "path": "src/Router.php",
    "content": "<?php\n\nnamespace CoffeeCode\\Router;\n\n/**\n * Class CoffeeCode Router\n *\n * @author Robson V. Leite <https://github.com/robsonvleite>\n * @package CoffeeCode\\Router\n */\nclass Router extends Dispatch\n{\n    /**\n     * Router constructor.\n     *\n     * @param string $projectUrl\n     * @param null|string $separator\n     */\n    public function __construct(string $projectUrl, ?string $separator = \":\")\n    {\n        parent::__construct($projectUrl, $separator);\n    }\n\n    /**\n     * @param string $route\n     * @param callable|string $handler\n     * @param string|null $name\n     * @param array|string|null $middleware\n     */\n    public function get(\n        string $route,\n        callable|string $handler,\n        string $name = null,\n        array|string $middleware = null\n    ): void {\n        $this->addRoute(\"GET\", $route, $handler, $name, $middleware);\n    }\n\n    /**\n     * @param string $route\n     * @param callable|string $handler\n     * @param string|null $name\n     * @param array|string|null $middleware\n     */\n    public function post(\n        string $route,\n        callable|string $handler,\n        string $name = null,\n        array|string $middleware = null\n    ): void {\n        $this->addRoute(\"POST\", $route, $handler, $name, $middleware);\n    }\n\n    /**\n     * @param string $route\n     * @param callable|string $handler\n     * @param string|null $name\n     * @param array|string|null $middleware\n     */\n    public function put(\n        string $route,\n        callable|string $handler,\n        string $name = null,\n        array|string $middleware = null\n    ): void {\n        $this->addRoute(\"PUT\", $route, $handler, $name, $middleware);\n    }\n\n    /**\n     * @param string $route\n     * @param callable|string $handler\n     * @param string|null $name\n     * @param array|string|null $middleware\n     */\n    public function patch(\n        string $route,\n        callable|string $handler,\n        string $name = null,\n        array|string $middleware = null\n    ): void {\n        $this->addRoute(\"PATCH\", $route, $handler, $name, $middleware);\n    }\n\n    /**\n     * @param string $route\n     * @param callable|string $handler\n     * @param string|null $name\n     * @param array|string|null $middleware\n     */\n    public function delete(\n        string $route,\n        callable|string $handler,\n        string $name = null,\n        array|string $middleware = null\n    ): void {\n        $this->addRoute(\"DELETE\", $route, $handler, $name, $middleware);\n    }\n}"
  },
  {
    "path": "src/RouterTrait.php",
    "content": "<?php\n\nnamespace CoffeeCode\\Router;\n\n/**\n * Trait RouterTrait\n * @package CoffeeCode\\Router\n */\ntrait RouterTrait\n{\n    /**\n     * @param string $method\n     * @param string $route\n     * @param callable|string $handler\n     * @param string|null $name\n     * @param array|string|null $middleware\n     */\n    protected function addRoute(\n        string $method,\n        string $route,\n        callable|string $handler,\n        string $name = null,\n        array|string $middleware = null\n    ): void {\n        $route = rtrim($route, \"/\");\n\n        $removeGroupFromPath = $this->group ? str_replace($this->group, \"\", $this->path) : $this->path;\n        $pathAssoc = trim($removeGroupFromPath, \"/\");\n        $routeAssoc = trim($route, \"/\");\n\n        preg_match_all(\"~\\{\\s* ([a-zA-Z_][a-zA-Z0-9_-]*) \\}~x\", $routeAssoc, $keys, PREG_SET_ORDER);\n        $routeDiff = array_values(array_diff_assoc(explode(\"/\", $pathAssoc), explode(\"/\", $routeAssoc)));\n\n        $this->formSpoofing();\n        $offset = 0;\n        foreach ($keys as $key) {\n            $this->data[$key[1]] = ($routeDiff[$offset++] ?? null);\n        }\n\n        $route = (!$this->group ? $route : \"/{$this->group}{$route}\");\n        $data = $this->data;\n        $namespace = $this->namespace;\n        $middleware = $middleware ?? (!empty($this->middleware[$this->group]) ? $this->middleware[$this->group] : null);\n        $router = function () use ($method, $handler, $data, $route, $name, $namespace, $middleware) {\n            return [\n                \"route\" => $route,\n                \"name\" => $name,\n                \"method\" => $method,\n                \"middlewares\" => $middleware,\n                \"handler\" => $this->handler($handler, $namespace),\n                \"action\" => $this->action($handler),\n                \"data\" => $data\n            ];\n        };\n\n        $route = preg_replace('~{([^}]*)}~', \"([^/]+)\", $route);\n        $this->routes[$method][$route] = $router();\n    }\n\n    /**\n     * httpMethod form spoofing\n     */\n    protected function formSpoofing(): void\n    {\n        $post = filter_input_array(INPUT_POST, FILTER_DEFAULT);\n\n        if (!empty($post['_method']) && in_array($post['_method'], [\"PUT\", \"PATCH\", \"DELETE\"])) {\n            $this->httpMethod = $post['_method'];\n            $this->data = $post;\n\n            unset($this->data[\"_method\"]);\n            return;\n        }\n\n        if ($this->httpMethod == \"POST\") {\n            $this->data = filter_input_array(INPUT_POST, FILTER_DEFAULT);\n\n            unset($this->data[\"_method\"]);\n            return;\n        }\n\n        if (in_array($this->httpMethod, [\"PUT\", \"PATCH\", \"DELETE\"]) && !empty($_SERVER['CONTENT_LENGTH'])) {\n            parse_str(file_get_contents('php://input', false, null, 0, $_SERVER['CONTENT_LENGTH']), $putPatch);\n            $this->data = $putPatch;\n\n            unset($this->data[\"_method\"]);\n            return;\n        }\n\n        $this->data = [];\n    }\n\n    /**\n     * @return bool\n     */\n    private function execute(): bool\n    {\n        if ($this->route) {\n            if (!$this->middleware()) {\n                return false;\n            }\n\n            if (is_callable($this->route['handler'])) {\n                call_user_func($this->route['handler'], ($this->route['data'] ?? []), $this);\n                return true;\n            }\n\n            $controller = $this->route['handler'];\n            $method = $this->route['action'];\n\n            if (class_exists($controller)) {\n                $newController = new $controller($this);\n                if (method_exists($controller, $method)) {\n                    $newController->$method(($this->route['data'] ?? []));\n                    return true;\n                }\n\n                $this->error = self::METHOD_NOT_ALLOWED;\n                return false;\n            }\n\n            $this->error = self::BAD_REQUEST;\n            return false;\n        }\n\n        $this->error = self::NOT_FOUND;\n        return false;\n    }\n\n    /**\n     * @return bool\n     */\n    private function middleware(): bool\n    {\n        if (empty($this->route[\"middlewares\"])) {\n            return true;\n        }\n\n        $middlewares = is_array(\n            $this->route[\"middlewares\"]\n        ) ? $this->route[\"middlewares\"] : [$this->route[\"middlewares\"]];\n\n        foreach ($middlewares as $middleware) {\n            if (class_exists($middleware)) {\n                $newMiddleware = new $middleware;\n                if (method_exists($newMiddleware, \"handle\")) {\n                    if (!$newMiddleware->handle($this)) {\n                        return false;\n                    }\n                } else {\n                    $this->error = self::METHOD_NOT_ALLOWED;\n                    return false;\n                }\n            } else {\n                $this->error = self::NOT_IMPLEMENTED;\n                return false;\n            }\n        }\n\n        return true;\n    }\n\n    /**\n     * @param callable|string $handler\n     * @param string|null $namespace\n     * @return callable|string\n     */\n    private function handler(callable|string $handler, ?string $namespace): callable|string\n    {\n        return (!is_string($handler) ? $handler : \"{$namespace}\\\\\" . explode($this->separator, $handler)[0]);\n    }\n\n    /**\n     * @param callable|string $handler\n     * @return string|null\n     */\n    private function action(callable|string $handler): ?string\n    {\n        return (!is_string($handler) ?: (explode($this->separator, $handler)[1] ?? null));\n    }\n\n    /**\n     * @param array $route_item\n     * @param array|null $data\n     * @return string|null\n     */\n    private function treat(array $route_item, array $data = null): ?string\n    {\n        $route = $route_item[\"route\"];\n        if (!empty($data)) {\n            $arguments = [];\n            $params = [];\n            foreach ($data as $key => $value) {\n                if (!strstr($route, \"{{$key}}\")) {\n                    $params[$key] = $value;\n                }\n                $arguments[\"{{$key}}\"] = $value;\n            }\n            $route = $this->process($route, $arguments, $params);\n        }\n\n        return \"{$this->projectUrl}{$route}\";\n    }\n\n    /**\n     * @param string $route\n     * @param array $arguments\n     * @param array|null $params\n     * @return string\n     */\n    private function process(string $route, array $arguments, array $params = null): string\n    {\n        $params = (!empty($params) ? \"?\" . http_build_query($params) : null);\n        return str_replace(array_keys($arguments), array_values($arguments), $route) . \"{$params}\";\n    }\n}"
  }
]