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 factory to create this class. |
28
|
|
|
*/ |
29
|
|
|
class RouteCollection implements RouteCollectionInterface |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The collection routes |
34
|
50 |
|
* |
35
|
|
|
* @var RouteInterface[] |
36
|
50 |
|
*/ |
37
|
50 |
|
private $routes; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor of the class |
41
|
|
|
* |
42
|
36 |
|
* @param RouteInterface ...$routes |
43
|
|
|
*/ |
44
|
36 |
|
public function __construct(RouteInterface ...$routes) |
45
|
36 |
|
{ |
46
|
|
|
$this->routes = $routes; |
47
|
36 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
22 |
|
public function add(RouteInterface ...$routes) : void |
53
|
|
|
{ |
54
|
22 |
|
foreach ($routes as $route) { |
55
|
|
|
$this->routes[] = $route; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function all() : array |
63
|
|
|
{ |
64
|
|
|
return $this->routes; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
* |
70
|
|
|
* @since 2.9.0 |
71
|
|
|
*/ |
72
|
|
|
public function setHost(string $host) : RouteCollectionInterface |
73
|
|
|
{ |
74
|
|
|
foreach ($this->routes as $route) { |
75
|
|
|
$route->setHost($host); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
* |
84
|
|
|
* @since 2.9.0 |
85
|
|
|
*/ |
86
|
|
|
public function addPrefix(string $prefix) : RouteCollectionInterface |
87
|
|
|
{ |
88
|
|
|
foreach ($this->routes as $route) { |
89
|
|
|
$route->addPrefix($prefix); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
* |
98
|
|
|
* @since 2.9.0 |
99
|
|
|
*/ |
100
|
|
|
public function addSuffix(string $suffix) : RouteCollectionInterface |
101
|
|
|
{ |
102
|
|
|
foreach ($this->routes as $route) { |
103
|
|
|
$route->addSuffix($suffix); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
* |
112
|
|
|
* @since 2.9.0 |
113
|
|
|
*/ |
114
|
|
|
public function addMethod(string ...$methods) : RouteCollectionInterface |
115
|
|
|
{ |
116
|
|
|
foreach ($this->routes as $route) { |
117
|
|
|
$route->addMethod(...$methods); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
* |
126
|
|
|
* @since 2.9.0 |
127
|
|
|
*/ |
128
|
|
|
public function appendMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
129
|
|
|
{ |
130
|
|
|
foreach ($this->routes as $route) { |
131
|
|
|
$route->addMiddleware(...$middlewares); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
* |
140
|
|
|
* @since 2.9.0 |
141
|
|
|
*/ |
142
|
|
|
public function prependMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
143
|
|
|
{ |
144
|
|
|
foreach ($this->routes as $route) { |
145
|
|
|
$route->setMiddlewares(...array_merge($middlewares, $route->getMiddlewares())); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* BC (backward compatibility) for version less than 2.9.0 |
153
|
|
|
* |
154
|
|
|
* @see appendMiddleware |
155
|
|
|
* |
156
|
|
|
* @deprecated 2.9.0 Use the `appendMiddleware` method. |
157
|
|
|
*/ |
158
|
|
|
public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
159
|
|
|
{ |
160
|
|
|
$this->appendMiddleware(...$middlewares); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* BC (backward compatibility) for version less than 2.9.0 |
167
|
|
|
* |
168
|
|
|
* @see prependMiddleware |
169
|
|
|
* |
170
|
|
|
* @deprecated 2.9.0 Use the `prependMiddleware` method. |
171
|
|
|
*/ |
172
|
|
|
public function unshiftMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
173
|
|
|
{ |
174
|
|
|
$this->prependMiddleware(...$middlewares); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|