<?php
if (php_sapi_name() !== 'cli') exit('It\'s no cli!');

$config = dirname(dirname(dirname(__DIR__))).'/Config/Router.php';

if(is_file($config)) return "\n--- Router configuration file already exists!";
if(!is_dir(dirname($config))) return "\n\n--- Configuration file for Router not instaled!\n\n";

$data = <<<'EOD'
<?php
/**
 * Config\Router
 * PHP version 7
 *
 * @category  Access
 * @package   Config
 * @author    Bill Rocha <prbr@ymail.com>
 * @copyright 2016 Bill Rocha <http://google.com/+BillRocha>
 * @license   <https://opensource.org/licenses/MIT> MIT
 * @version   GIT: 0.0.2
 * @link      http://paulorocha.tk/github/devbr
 */

namespace Config;

/**
 * Config\Router Class
 *
 * @category Access
 * @package  Config
 * @author   Bill Rocha <prbr@ymail.com>
 * @license  <https://opensource.org/licenses/MIT> MIT
 * @link     http://paulorocha.tk/github/devbr
 */
class Router
{
    function routers(&$router)
    {
        $router->respond('get', '/', 'Site\Front::page')
               /*
                  Considere a url:
                    http://localhost/loja/903/Camisa Polo Marca XXX/ldfnld-n0p/+=dlknferçlkm/dsfdfdsjd//

                    Captura:
                        ['id'] => 903
                        ['produto'] => Camisa Polo MArca XXX

                    Ignora tudo mais...

                    Veja a função "teste" da classe em "/.php/Site/Front.php"
                    Segue a configuração da rota, abaixo:
                */
               ->respond('get', '/loja/(?<id>.*?)/(?<produto>[^/]*).*', 'Site\Front::test')
               
                /*
                    A mesma configuração, porém com parametros (índice) NUMÉRICOS:
                    http://localhost/loja2/903/Camisa Polo Marca XXX/ldfnld-n0p/+=dlknferçlkm/dsfdfdsjd//
                */
                
               ->respond('get', '/loja2/(.*?)/([^/]*).*', 'Site\Front::test')

               /*
                    Usando uma função anônima diretamente na configuração do Router
                    http://localhost/fac/categoria/pergunta
                */
               ->respond('get', '/fac/(.*?)/([^/]*).*',

                            function ($type, $user) {
                                echo '<h1>Função anônima</h1>
                                      <p><b>Request URI:</b> '.$type.'<br>
                                      <b>Parametros:</b><pre>'.print_r($user, true).'</pre></p>';
                            })

               /*
                    Usando uma função anônima para mostrar uma página HTML ESTÁTICA
                    http://localhost/about                    
                */
               ->respond('get', '/about',

                            function () {
                                include _HTML.'Static/about.html';
                            });
    }
}

/* Description:

        $router->respond( <type>, <request>, <controller>, [<action>]);
        
            <type>:       A string with the following methods: "all", "get", "post", "delete", "put", "patch". 
                          Or specify a specific group: "get|post|delete".
                          
            <request>:    String of the requested URI - ex.: "about/me" ==> http://site.com/about/me
            
            <controller>: Class (object) to manage the request. 
                          Name must be a complete string, with NAMESPACE + CLASSNAME. Ex.: "Lib\User".
                          Alternatively you can use the following format: "controller::action" - ex.: "Lib\User::login".                          
                          The Controller can also be an anonymous function that receives (or not) 
                          parameters of the regular expression in <request>. 
                          Ex.: $router->respond('get', '/(*)', function($path){exit($path);});
                           -- if request "http://site.com/test/me", print in screen "test/me".
            
            <action>:     Optional form to indicate an action. Ex.: "login"
*/
EOD;

file_put_contents($config, $data);

return "\n--- Router instaled!";
