|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Router; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
9
|
|
|
use Spiral\Core\Container\Autowire; |
|
10
|
|
|
use Spiral\Core\CoreInterface; |
|
11
|
|
|
use Spiral\Core\FactoryInterface; |
|
12
|
|
|
use Spiral\Router\Target\AbstractTarget; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* RouteGroup provides the ability to configure multiple routes to controller/actions using same presets. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class RouteGroup |
|
18
|
|
|
{ |
|
19
|
|
|
private string $prefix = ''; |
|
20
|
|
|
private string $namePrefix = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array<non-empty-string, Route> */ |
|
|
|
|
|
|
23
|
|
|
private array $routes = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var array<class-string<MiddlewareInterface>|MiddlewareInterface|Autowire> */ |
|
|
|
|
|
|
26
|
|
|
private array $middleware = []; |
|
27
|
|
|
|
|
28
|
|
|
private Autowire|CoreInterface|string|null $core = null; |
|
29
|
|
|
|
|
30
|
285 |
|
public function __construct( |
|
31
|
|
|
/** @deprecated since v3.3.0 */ |
|
32
|
|
|
private readonly ?ContainerInterface $container = null, |
|
33
|
|
|
/** @deprecated since v3.3.0 */ |
|
34
|
|
|
private readonly ?RouterInterface $router = null, |
|
35
|
|
|
/** @deprecated since v3.3.0 */ |
|
36
|
|
|
private readonly ?UriHandler $handler = null |
|
37
|
|
|
) { |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Check if group has a route with given name |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function hasRoute(string $name): bool |
|
44
|
|
|
{ |
|
45
|
4 |
|
return \array_key_exists($name, $this->routes); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Prefix added to all the routes. |
|
50
|
|
|
*/ |
|
51
|
267 |
|
public function setPrefix(string $prefix): self |
|
52
|
|
|
{ |
|
53
|
267 |
|
$this->prefix = $prefix; |
|
54
|
|
|
|
|
55
|
267 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Route name prefix added to all routes. |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function setNamePrefix(string $prefix): self |
|
62
|
|
|
{ |
|
63
|
1 |
|
$this->namePrefix = $prefix; |
|
64
|
|
|
|
|
65
|
1 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
public function setCore(Autowire|CoreInterface|string $core): self |
|
69
|
|
|
{ |
|
70
|
2 |
|
$this->core = $core; |
|
71
|
|
|
|
|
72
|
2 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param MiddlewareInterface|Autowire|class-string<MiddlewareInterface>|non-empty-string $middleware |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
271 |
|
public function addMiddleware(MiddlewareInterface|Autowire|string $middleware): self |
|
79
|
|
|
{ |
|
80
|
271 |
|
$this->middleware[] = $middleware; |
|
81
|
|
|
|
|
82
|
271 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Push routes to router. |
|
87
|
|
|
* |
|
88
|
|
|
* @internal |
|
89
|
|
|
*/ |
|
90
|
282 |
|
public function register(RouterInterface $router, FactoryInterface $factory): void |
|
91
|
|
|
{ |
|
92
|
282 |
|
foreach ($this->routes as $name => $route) { |
|
93
|
282 |
|
if ($this->core !== null) { |
|
94
|
2 |
|
if (!$this->core instanceof CoreInterface) { |
|
95
|
1 |
|
$this->core = $factory->make($this->core); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
$target = $route->getTarget(); |
|
99
|
2 |
|
if ($target instanceof AbstractTarget) { |
|
100
|
2 |
|
$route = $route->withTarget($target->withCore($this->core)); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
try { |
|
105
|
282 |
|
$uriHandler = $route->getUriHandler(); |
|
106
|
282 |
|
} catch (\Throwable) { |
|
107
|
282 |
|
$uriHandler = $factory->make(UriHandler::class); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
282 |
|
$router->setRoute( |
|
111
|
|
|
$name, |
|
112
|
282 |
|
$route->withUriHandler($uriHandler->withPrefix($this->prefix))->withMiddleware(...$this->middleware) |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Add a route to a route group. |
|
119
|
|
|
*/ |
|
120
|
284 |
|
public function addRoute(string $name, Route $route): self |
|
121
|
|
|
{ |
|
122
|
284 |
|
$this->routes[$this->namePrefix . $name] = $route; |
|
123
|
|
|
|
|
124
|
284 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|