31 lines
734 B
PHP
31 lines
734 B
PHP
<?php
|
|
|
|
namespace DistributingCarriers\Infrastructure;
|
|
|
|
class Router
|
|
{
|
|
private $routes = [];
|
|
|
|
public function add(string $method, string $path, callable $handler): void
|
|
{
|
|
$this->routes[] = [
|
|
'method' => $method,
|
|
'path' => $path,
|
|
'handler' => $handler
|
|
];
|
|
}
|
|
|
|
public function dispatch(string $method, string $uri)
|
|
{
|
|
foreach ($this->routes as $route) {
|
|
if ($route['method'] === $method && $route['path'] === $uri) {
|
|
return \call_user_func($route['handler']);
|
|
}
|
|
}
|
|
|
|
http_response_code(404);
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['error' => 'Not Found']);
|
|
}
|
|
}
|