1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Router; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Exception; |
9
|
|
|
use Gacela\Container\Container; |
10
|
|
|
use Gacela\Router\Entities\Route; |
11
|
|
|
use Gacela\Router\Exceptions\NotFound404Exception; |
12
|
|
|
use ReflectionFunction; |
13
|
|
|
|
14
|
|
|
use function get_class; |
15
|
|
|
use function is_callable; |
16
|
|
|
|
17
|
|
|
final class Router |
18
|
|
|
{ |
19
|
92 |
|
public function __construct( |
20
|
|
|
private Routes $routes, |
21
|
|
|
private Bindings $bindings, |
22
|
|
|
private Handlers $handlers, |
23
|
|
|
) { |
24
|
92 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Shortcut to create, add and run all routes at once. |
28
|
|
|
*/ |
29
|
90 |
|
public static function configure(Closure $fn): void |
30
|
|
|
{ |
31
|
90 |
|
$self = self::create(); |
32
|
90 |
|
$self->addRoutes($fn); |
33
|
89 |
|
$self->run(); |
34
|
|
|
} |
35
|
|
|
|
36
|
92 |
|
public static function create(): self |
37
|
|
|
{ |
38
|
92 |
|
return new self(new Routes(), new Bindings(), new Handlers()); |
39
|
|
|
} |
40
|
|
|
|
41
|
92 |
|
public function addRoutes(Closure $fn): self |
42
|
|
|
{ |
43
|
92 |
|
$params = array_map(fn ($param) => match ((string)$param->getType()) { |
44
|
92 |
|
Routes::class => $this->routes, |
45
|
92 |
|
Bindings::class => $this->bindings, |
46
|
92 |
|
Handlers::class => $this->handlers, |
47
|
92 |
|
default => null, |
48
|
92 |
|
}, (new ReflectionFunction($fn))->getParameters()); |
49
|
|
|
|
50
|
92 |
|
$fn(...$params); |
51
|
|
|
|
52
|
91 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
91 |
|
public function run(): void |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
91 |
|
echo self::findRoute($this->routes)->run($this->bindings); |
59
|
15 |
|
} catch (Exception $exception) { |
60
|
15 |
|
echo self::handleException($this->handlers, $exception); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
91 |
|
private static function findRoute(Routes $routes): Route |
65
|
|
|
{ |
66
|
91 |
|
foreach ($routes->getAllRoutes() as $route) { |
67
|
89 |
|
if ($route->requestMatches()) { |
68
|
84 |
|
return $route; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
7 |
|
throw new NotFound404Exception(); |
73
|
|
|
} |
74
|
|
|
|
75
|
15 |
|
private static function handleException(Handlers $handlers, Exception $exception): string |
76
|
|
|
{ |
77
|
15 |
|
$handler = self::findHandler($handlers, $exception); |
78
|
|
|
|
79
|
15 |
|
if (is_callable($handler)) { |
80
|
8 |
|
return $handler($exception); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @var mixed $instance */ |
84
|
7 |
|
$instance = Container::create($handler); |
85
|
|
|
|
86
|
7 |
|
if (is_callable($instance)) { |
87
|
7 |
|
return $instance($exception); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return ''; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return callable|class-string |
|
|
|
|
95
|
|
|
*/ |
96
|
15 |
|
private static function findHandler(Handlers $handlers, Exception $exception): string|callable |
97
|
|
|
{ |
98
|
15 |
|
return $handlers->getAllHandlers()[get_class($exception)] |
99
|
15 |
|
?? $handlers->getAllHandlers()[Exception::class]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|