Commit 873cf802 authored by maed's avatar maed
Browse files

router

parent 22b93ea3
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -2,15 +2,19 @@

require_once __DIR__ . './vendor/autoload.php';

use Inifap\Biblioteca\Router;
const VIEW_PATH = __DIR__ . '/src/views';

use Inifap\Biblioteca\App;
use Inifap\Biblioteca\RouterManager;

$router = new Router();
$router = new RouterManager();
$router->addRoute("/biblioteca")->get(function () {
    include __DIR__ . './src/views/home/index.php';
    include VIEW_PATH . '/home/index.php';
    // echo "Hola mundo";
});
$router->addRoute("/biblioteca/users/:id")->get(function () {
    echo "Hola mundo 3";
$router->addRoute("/biblioteca/users/:id/:author")->get(function (array $params) {
    // echo "Hola mundo 3" . $params['id'] . " " . $params['author'];
    // include VIEW_PATH . '/home/index.php';
});


+4 −17
Original line number Diff line number Diff line
@@ -5,30 +5,17 @@ namespace Inifap\Biblioteca;
class App
{

    private Router $router;
    private RouterManager $routerManager;

    public function __construct(Router $router)
    public function __construct(RouterManager $routerManager)
    {
        $this->router = $router;
        $this->routerManager = $routerManager;
    }

    public function run(): void
    {
        $path = $_SERVER['REQUEST_URI'] ?? '/';
        $method = $_SERVER['REQUEST_METHOD'] ?? 'GET';

        $route = $this->router->getRoute($path);

        if ($route) {
            $callback = $route->getMethods()[$method];

            if ($callback) {
                $callback();
            } else {
                echo "Method not allowed";
            }
        } else {
            echo "Not found";
        }
        $this->routerManager->resolve($path, $method);
    }
}
+28 −0
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@ class Route
{
    private string $path;
    private array $methods = [];
    private array $paramsPosition = [];
    private array $params = [];

    public function __construct(string $path)
    {
@@ -14,6 +16,14 @@ class Route
        }

        $this->path = $path;

        preg_match_all('/:(\w+)/', $path, $matches, PREG_SET_ORDER);

        foreach ($matches as $match) {
            $path = str_replace($match[0], '(\w+)', $path);
            $this->paramsPosition[] = $match[1];
            $this->params[$match[1]] = null;
        }
    }

    public function post(callable $callback): self
@@ -40,6 +50,24 @@ class Route
        return $this;
    }

    public function hasMethod(string $method): bool
    {
        $method = strtoupper($method);
        return isset($this->methods[$method]);
    }

    public function setParams(array $params): void
    {
        foreach ($params as $key => $value) {
            $this->params[$this->paramsPosition[$key]] = $value;
        }
    }

    public function getParams(): array
    {
        return $this->params;
    }

    public function getPath(): string
    {
        return $this->path;

biblioteca/src/Router.php

deleted100644 → 0
+0 −30
Original line number Diff line number Diff line
<?php

namespace Inifap\Biblioteca;

class Router
{

    /** @var Route[] */
    private array $routes = [];

    public function addRoute(string $path): Route
    {
        if (!isset($this->routes[$path])) {
            $this->routes[$path] = new Route($path);
        }
        return $this->routes[$path];
    }

    public function getRoute(string $path): ?Route
    {
        if (substr($path, -1) === '/') {
            $path = rtrim($path, '/');
        }

        if (!isset($this->routes[$path])) {
            return null;
        }
        return $this->routes[$path];
    }
}
+58 −0
Original line number Diff line number Diff line
<?php

namespace Inifap\Biblioteca;

class RouterManager
{

    /** @var Route[] */
    private array $routes = [];

    public function addRoute(string $path): Route
    {
        if (!isset($this->routes[$path])) {
            $this->routes[$path] = new Route($path);
        }
        return $this->routes[$path];
    }

    public function resolve(string $path, string $method): void
    {
        if (substr($path, -1) === '/') {
            $path = rtrim($path, '/');
        }

        foreach ($this->routes as $route) {
            if (preg_match_all('/:(\w+)/', $route->getPath(), $matches, PREG_SET_ORDER)) {
                $regex = str_replace('/', '\/', $route->getPath());
                foreach ($matches as $match) {
                    $regex = str_replace($match[0], '(\w+)', $regex);
                }
                if (preg_match('/^' . $regex . '$/', $path, $params) && $route->hasMethod($method)) {
                    array_shift($params);
                    $route->setParams($params);
                    $this->callback($route, $method);
                    return;
                }
            } else {
                if ($route->getPath() === $path) {
                    $this->callback($route, $method);
                }
            }
        }

        // Not found
        include VIEW_PATH . '/errors/404.php';
    }

    private function callback(Route $route, string $method): void
    {
        $callback = $route->getMethods()[$method];
        if (is_callable($callback)) {
            $callback($route->getParams());
            return;
        } else {
            echo "Method not allowed";
        }
    }
}
Loading