|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Router; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Router\Internal\MiddlewareFilter; |
|
8
|
|
|
|
|
9
|
|
|
final class Group |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Group[]|RoutableInterface[]|Route[] |
|
13
|
|
|
*/ |
|
14
|
|
|
private array $routes = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array[]|callable[]|string[] |
|
18
|
|
|
* @psalm-var list<array|callable|string> |
|
19
|
|
|
*/ |
|
20
|
|
|
private array $middlewares = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private array $hosts = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @psalm-var list<array|callable|string>|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private ?array $enabledMiddlewaresCache = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array|callable|string|null Middleware definition for CORS requests. |
|
34
|
|
|
*/ |
|
35
|
|
|
private $corsMiddleware = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled. |
|
39
|
|
|
* It is useful to avoid invoking one of the parent group middleware for |
|
40
|
|
|
* a certain route. |
|
41
|
|
|
*/ |
|
42
|
42 |
|
public function __construct( |
|
43
|
|
|
private ?string $prefix = null, |
|
44
|
|
|
private ?string $namePrefix = null, |
|
45
|
|
|
array $routes = [], |
|
46
|
|
|
array $middlewares = [], |
|
47
|
|
|
array $hosts = [], |
|
48
|
|
|
private array $disabledMiddlewares = [], |
|
49
|
|
|
array|callable|string|null $corsMiddleware = null |
|
50
|
|
|
) { |
|
51
|
42 |
|
$this->setRoutes($routes); |
|
52
|
42 |
|
$this->setMiddlewares($middlewares); |
|
53
|
41 |
|
$this->setHosts($hosts); |
|
54
|
40 |
|
$this->corsMiddleware = $corsMiddleware; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return Group[]|RoutableInterface[]|Route[] |
|
59
|
|
|
*/ |
|
60
|
24 |
|
public function getRoutes(): array |
|
61
|
|
|
{ |
|
62
|
24 |
|
return $this->routes; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
23 |
|
public function getMiddlewares(): array |
|
66
|
|
|
{ |
|
67
|
23 |
|
return $this->middlewares; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
26 |
|
public function getHosts(): array |
|
71
|
|
|
{ |
|
72
|
26 |
|
return $this->hosts; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
23 |
|
public function getCorsMiddleware(): callable|array|string|null |
|
76
|
|
|
{ |
|
77
|
23 |
|
return $this->corsMiddleware; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
24 |
|
public function getPrefix(): ?string |
|
81
|
|
|
{ |
|
82
|
24 |
|
return $this->prefix; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
24 |
|
public function getNamePrefix(): ?string |
|
86
|
|
|
{ |
|
87
|
24 |
|
return $this->namePrefix; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function getDisabledMiddlewares(): array |
|
91
|
|
|
{ |
|
92
|
1 |
|
return $this->disabledMiddlewares; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
42 |
|
public function setRoutes(array $routes): self |
|
96
|
|
|
{ |
|
97
|
42 |
|
$this->assertRoutes($routes); |
|
98
|
42 |
|
$this->routes = $routes; |
|
99
|
42 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
42 |
|
public function setMiddlewares(array $middlewares): self |
|
103
|
|
|
{ |
|
104
|
42 |
|
$this->assertMiddlewares($middlewares); |
|
105
|
41 |
|
$this->middlewares = $middlewares; |
|
106
|
41 |
|
$this->enabledMiddlewaresCache = null; |
|
107
|
41 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
41 |
|
public function setHosts(array $hosts): self |
|
111
|
|
|
{ |
|
112
|
41 |
|
foreach ($hosts as $host) { |
|
113
|
6 |
|
if (!is_string($host)) { |
|
114
|
1 |
|
throw new \InvalidArgumentException('Invalid $hosts provided, list of string expected.'); |
|
115
|
|
|
} |
|
116
|
6 |
|
$host = rtrim($host, '/'); |
|
117
|
|
|
|
|
118
|
6 |
|
if ($host !== '' && !in_array($host, $this->hosts, true)) { |
|
119
|
6 |
|
$this->hosts[] = $host; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
40 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
3 |
|
public function setCorsMiddleware(callable|array|string|null $corsMiddleware): self |
|
127
|
|
|
{ |
|
128
|
3 |
|
$this->corsMiddleware = $corsMiddleware; |
|
129
|
3 |
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
public function setPrefix(?string $prefix): self |
|
133
|
|
|
{ |
|
134
|
1 |
|
$this->prefix = $prefix; |
|
135
|
1 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
public function setNamePrefix(?string $namePrefix): self |
|
139
|
|
|
{ |
|
140
|
1 |
|
$this->namePrefix = $namePrefix; |
|
141
|
1 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
4 |
|
public function setDisabledMiddlewares(array $disabledMiddlewares): self |
|
145
|
|
|
{ |
|
146
|
4 |
|
$this->disabledMiddlewares = $disabledMiddlewares; |
|
147
|
4 |
|
$this->enabledMiddlewaresCache = null; |
|
148
|
4 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return array[]|callable[]|string[] |
|
153
|
|
|
* @psalm-return list<array|callable|string> |
|
154
|
|
|
*/ |
|
155
|
24 |
|
public function getEnabledMiddlewares(): array |
|
156
|
|
|
{ |
|
157
|
24 |
|
if ($this->enabledMiddlewaresCache !== null) { |
|
158
|
13 |
|
return $this->enabledMiddlewaresCache; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
24 |
|
return $this->enabledMiddlewaresCache = MiddlewareFilter::filter($this->middlewares, $this->disabledMiddlewares); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @psalm-assert list<array|callable|string> $middlewares |
|
166
|
|
|
*/ |
|
167
|
42 |
|
private function assertMiddlewares(array $middlewares): void |
|
168
|
|
|
{ |
|
169
|
|
|
/** @var mixed $middleware */ |
|
170
|
42 |
|
foreach ($middlewares as $middleware) { |
|
171
|
15 |
|
if (is_string($middleware) || is_callable($middleware) || is_array($middleware)) { |
|
172
|
15 |
|
continue; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
1 |
|
throw new \InvalidArgumentException( |
|
176
|
1 |
|
'Invalid $middlewares provided, list of string or array or callable expected.' |
|
177
|
1 |
|
); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @psalm-assert array<Route|Group|RoutableInterface> $routes |
|
183
|
|
|
*/ |
|
184
|
42 |
|
private function assertRoutes(array $routes): void |
|
185
|
|
|
{ |
|
186
|
|
|
/** @var Group|RoutableInterface|Route $route */ |
|
187
|
42 |
|
foreach ($routes as $route) { |
|
188
|
25 |
|
if ($route instanceof Route || $route instanceof self || $route instanceof RoutableInterface) { |
|
189
|
25 |
|
continue; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
1 |
|
throw new \InvalidArgumentException( |
|
193
|
1 |
|
'Invalid $routes provided, array of `Route` or `Group` or `RoutableInterface` instance expected.' |
|
194
|
1 |
|
); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|