parent
22127b8702
commit
4d1ef981d2
@ -1,11 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Silex\Router\Router;
|
||||||
|
|
||||||
require __DIR__ . '/../src/Silex/Config/SplClassLoader.php';
|
require __DIR__ . '/../src/Silex/Config/SplClassLoader.php';
|
||||||
require __DIR__ . '/../src/Silex/Config/Config.php';
|
require __DIR__ . '/../src/Silex/Config/Config.php';
|
||||||
|
|
||||||
$loader = new SplClassLoader('Silex', __DIR__ . '/../src');
|
$loader = new SplClassLoader('Silex', __DIR__ . '/../src');
|
||||||
$loader->register();
|
$loader->register();
|
||||||
|
|
||||||
// TODO router
|
|
||||||
$controller = new \Silex\Controller\UserController();
|
$controller = new \Silex\Controller\UserController();
|
||||||
$controller->index(new \Silex\DI\DI())->render(__DIR__ . '/../' . VIEW_PATH);
|
$router = new Router($_SERVER['REQUEST_URI']);
|
||||||
|
$router->get('/^$/', [$controller, 'index']);
|
||||||
|
//$router->get('/^inscription$/', [$controller, 'connexion']);
|
||||||
|
$router->run(new \Silex\DI\DI())->render(__DIR__ . '/../' . VIEW_PATH);
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Router;
|
||||||
|
|
||||||
|
use Silex\DI\DI;
|
||||||
|
use Silex\Http\HttpResponse;
|
||||||
|
|
||||||
|
class Route
|
||||||
|
{
|
||||||
|
|
||||||
|
private string $path;
|
||||||
|
|
||||||
|
private $callable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
private array $matches = [];
|
||||||
|
|
||||||
|
public function __construct(string $path, callable $callable)
|
||||||
|
{
|
||||||
|
$this->path = $path;
|
||||||
|
$this->callable = $callable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function matches(string $url): bool
|
||||||
|
{
|
||||||
|
return preg_match($this->path, $url, $this->matches) === 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function call(DI $di): HttpResponse
|
||||||
|
{
|
||||||
|
return call_user_func_array($this->callable, [$di, $this->matches]);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Router;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lorsqu'aucune route ne correspond à l'url demandée.
|
||||||
|
*/
|
||||||
|
class RouteNotFoundException extends Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct(string $message, int $code = 0, ?Throwable $previous = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Router;
|
||||||
|
|
||||||
|
use Silex\Http\HttpResponse;
|
||||||
|
use Silex\DI\DI;
|
||||||
|
|
||||||
|
class Router
|
||||||
|
{
|
||||||
|
|
||||||
|
private string $url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Route[]
|
||||||
|
*/
|
||||||
|
private array $routes = [];
|
||||||
|
|
||||||
|
public function __construct(string $url)
|
||||||
|
{
|
||||||
|
$this->url = trim($url, '/');;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(string $path, callable $callable): self
|
||||||
|
{
|
||||||
|
return $this->addRoute('GET', $path, $callable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addRoute(string $method, string $path, $callable): self
|
||||||
|
{
|
||||||
|
$route = new Route($path, $callable);
|
||||||
|
$this->routes[$method][] = $route;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run(DI $di): HttpResponse
|
||||||
|
{
|
||||||
|
if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) {
|
||||||
|
throw new RouteNotFoundException('Unknown HTTP method');
|
||||||
|
}
|
||||||
|
foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) {
|
||||||
|
if ($route->matches($this->url)) {
|
||||||
|
return $route->call($di);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new RouteNotFoundException('No matching routes');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue