1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Router; |
13
|
|
|
|
14
|
|
|
use Closure; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Collecter tous les itinéraires définis pour affichage. |
18
|
|
|
*/ |
19
|
|
|
final class DefinedRouteCollector |
20
|
|
|
{ |
21
|
|
|
private RouteCollection $routeCollection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Routes deja collectees (pour eviter de faire la meme chose plusieurs fois) |
25
|
|
|
*/ |
26
|
|
|
private array $cachedRoutes = []; |
27
|
|
|
|
28
|
|
|
public function __construct(RouteCollection $routes) |
29
|
|
|
{ |
30
|
2 |
|
$this->routeCollection = $routes; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Collecte les routes enregistrees |
35
|
|
|
*/ |
36
|
|
|
public function collect(bool $reset = true): array |
37
|
|
|
{ |
38
|
|
|
if (! $reset && $this->cachedRoutes !== []) { |
39
|
2 |
|
return $this->cachedRoutes; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
$methods = Router::HTTP_METHODS; |
43
|
|
|
|
44
|
2 |
|
$definedRoutes = []; |
45
|
|
|
|
46
|
|
|
foreach ($methods as $method) { |
47
|
2 |
|
$routes = $this->routeCollection->getRoutes($method); |
48
|
|
|
|
49
|
|
|
foreach ($routes as $route => $handler) { |
50
|
|
|
if (is_string($handler) || $handler instanceof Closure) { |
51
|
|
|
if ($handler instanceof Closure) { |
52
|
2 |
|
$view = $this->routeCollection->getRoutesOptions($route, $method)['view'] ?? false; |
53
|
|
|
|
54
|
2 |
|
$handler = $view ? '(View) ' . $view : '(Closure)'; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$routeName = $this->routeCollection->getRoutesOptions($route, $method)['as'] ?? $route; |
58
|
|
|
|
59
|
|
|
$definedRoutes[] = [ |
60
|
|
|
'method' => $method, |
61
|
|
|
'route' => $route, |
62
|
|
|
'name' => $routeName, |
63
|
|
|
'handler' => $handler, |
64
|
2 |
|
]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
return $this->cachedRoutes = $definedRoutes; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|