1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Import functions |
21
|
|
|
*/ |
22
|
|
|
use function array_merge; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* RouteCollection |
26
|
|
|
* |
27
|
|
|
* Use the {@see RouteCollectionFactory} factory to create this class. |
28
|
|
|
*/ |
29
|
|
|
class RouteCollection implements RouteCollectionInterface |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The collection routes |
34
|
|
|
* |
35
|
|
|
* @var RouteInterface[] |
36
|
|
|
*/ |
37
|
|
|
private $routes; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor of the class |
41
|
|
|
* |
42
|
|
|
* @param RouteInterface ...$routes |
43
|
|
|
*/ |
44
|
41 |
|
public function __construct(RouteInterface ...$routes) |
45
|
|
|
{ |
46
|
41 |
|
$this->routes = $routes; |
47
|
41 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
9 |
|
public function all() : array |
53
|
|
|
{ |
54
|
9 |
|
return $this->routes; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
23 |
|
public function get(string $name) : ?RouteInterface |
61
|
|
|
{ |
62
|
23 |
|
foreach ($this->routes as $route) { |
63
|
23 |
|
if ($name === $route->getName()) { |
64
|
23 |
|
return $route; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
22 |
|
public function has(string $name) : bool |
75
|
|
|
{ |
76
|
22 |
|
return $this->get($name) instanceof RouteInterface; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
16 |
|
public function add(RouteInterface ...$routes) : RouteCollectionInterface |
83
|
|
|
{ |
84
|
16 |
|
foreach ($routes as $route) { |
85
|
16 |
|
$this->routes[] = $route; |
86
|
|
|
} |
87
|
|
|
|
88
|
16 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function setHost(string $host) : RouteCollectionInterface |
95
|
|
|
{ |
96
|
1 |
|
foreach ($this->routes as $route) { |
97
|
1 |
|
$route->setHost($host); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function addPrefix(string $prefix) : RouteCollectionInterface |
107
|
|
|
{ |
108
|
1 |
|
foreach ($this->routes as $route) { |
109
|
1 |
|
$route->addPrefix($prefix); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function addSuffix(string $suffix) : RouteCollectionInterface |
119
|
|
|
{ |
120
|
1 |
|
foreach ($this->routes as $route) { |
121
|
1 |
|
$route->addSuffix($suffix); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
1 |
|
public function addMethod(string ...$methods) : RouteCollectionInterface |
131
|
|
|
{ |
132
|
1 |
|
foreach ($this->routes as $route) { |
133
|
1 |
|
$route->addMethod(...$methods); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
2 |
|
public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
143
|
|
|
{ |
144
|
2 |
|
foreach ($this->routes as $route) { |
145
|
2 |
|
$route->addMiddleware(...$middlewares); |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Alias to the addMiddleware method |
153
|
|
|
* |
154
|
|
|
* @param MiddlewareInterface ...$middlewares |
155
|
|
|
* |
156
|
|
|
* @return RouteCollectionInterface |
157
|
|
|
*/ |
158
|
1 |
|
public function appendMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
159
|
|
|
{ |
160
|
1 |
|
return $this->addMiddleware(...$middlewares); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
3 |
|
public function prependMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
167
|
|
|
{ |
168
|
3 |
|
foreach ($this->routes as $route) { |
169
|
3 |
|
$route->setMiddlewares(...array_merge($middlewares, $route->getMiddlewares())); |
170
|
|
|
} |
171
|
|
|
|
172
|
3 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @deprecated 2.10.0 Use the prependMiddleware method. |
177
|
|
|
*/ |
178
|
1 |
|
public function unshiftMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
179
|
|
|
{ |
180
|
1 |
|
return $this->prependMiddleware(...$middlewares); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|