1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Codeburner Framework. |
5
|
|
|
* |
6
|
|
|
* @author Alex Rohleder <[email protected]> |
7
|
|
|
* @copyright 2016 Alex Rohleder |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Codeburner\Router; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Group several routes and abstract operations applied to all. |
15
|
|
|
* |
16
|
|
|
* @author Alex Rohleder <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
class Group |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* All grouped route objects. |
24
|
|
|
* |
25
|
|
|
* @var Route[] |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
protected $routes; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set a new Route or merge an existing group of routes. |
32
|
|
|
* |
33
|
|
|
* @param Group|Route $route |
34
|
|
|
* @return self |
35
|
|
|
*/ |
36
|
|
|
|
37
|
28 |
|
public function set($route) |
38
|
|
|
{ |
39
|
28 |
|
if ($route instanceof Group) { |
40
|
28 |
|
foreach ($route->all() as $r) |
41
|
28 |
|
$this->routes[] = $r; |
42
|
28 |
|
} else $this->routes[] = $route; |
43
|
28 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A fast way to register a route into the group |
48
|
|
|
* |
49
|
|
|
* @param Route $route |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
|
53
|
53 |
|
public function setRoute(Route $route) |
54
|
|
|
{ |
55
|
53 |
|
$this->routes[] = $route; |
56
|
53 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return all grouped routes objects. |
61
|
|
|
* |
62
|
|
|
* @return Route[] |
63
|
|
|
*/ |
64
|
|
|
|
65
|
28 |
|
public function all() |
66
|
|
|
{ |
67
|
28 |
|
return $this->routes; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get a specific route of the group, routes receive a key based on |
72
|
|
|
* the order they are added to the group. |
73
|
|
|
* |
74
|
|
|
* @param int $number |
75
|
|
|
* @return Route |
76
|
|
|
*/ |
77
|
|
|
|
78
|
4 |
|
public function nth($number) |
79
|
|
|
{ |
80
|
4 |
|
return $this->routes[$number]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Forget the registration of all grouped routes on to collector. |
85
|
|
|
* After the forget the route object will still exist but will not |
86
|
|
|
* count for the matcher. |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
|
|
|
91
|
3 |
|
public function forget() |
92
|
|
|
{ |
93
|
3 |
|
foreach ($this->routes as $route) |
94
|
3 |
|
$route->forget(); |
95
|
3 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set one HTTP method to all grouped routes. |
100
|
|
|
* |
101
|
|
|
* @param string $method The HTTP Method |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
|
|
|
105
|
1 |
|
public function setMethod($method) |
106
|
|
|
{ |
107
|
1 |
|
foreach ($this->routes as $route) |
108
|
1 |
|
$route->setMethod($method); |
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set one action to all grouped routes. |
114
|
|
|
* |
115
|
|
|
* @param string $action |
116
|
|
|
* @return self |
117
|
|
|
*/ |
118
|
|
|
|
119
|
3 |
|
public function setAction($action) |
120
|
|
|
{ |
121
|
3 |
|
foreach ($this->routes as $route) |
122
|
3 |
|
$route->setAction($action); |
123
|
3 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set one namespace to all grouped routes. |
128
|
|
|
* |
129
|
|
|
* @param string $namespace |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
|
|
133
|
1 |
|
public function setNamespace($namespace) |
134
|
|
|
{ |
135
|
1 |
|
foreach ($this->routes as $route) |
136
|
1 |
|
$route->setNamespace($namespace); |
137
|
1 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add a prefix to all grouped routes pattern. |
142
|
|
|
* |
143
|
|
|
* @param string $prefix |
144
|
|
|
* @return self |
145
|
|
|
*/ |
146
|
|
|
|
147
|
3 |
|
public function setPrefix($prefix) |
148
|
|
|
{ |
149
|
3 |
|
$prefix = "/" . ltrim($prefix, "/"); |
150
|
3 |
|
$routes = []; |
151
|
3 |
|
foreach ($this->routes as $route) |
152
|
3 |
|
$routes[] = $route->setPattern(rtrim($prefix . $route->getPattern(), "/")); |
153
|
3 |
|
$this->routes = $routes; |
154
|
3 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set metadata to all grouped routes. |
159
|
|
|
* |
160
|
|
|
* @param string $key |
161
|
|
|
* @param string $value |
162
|
|
|
* |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
|
166
|
1 |
|
public function setMetadata($key, $value) |
167
|
|
|
{ |
168
|
1 |
|
foreach ($this->routes as $route) |
169
|
1 |
|
$route->setMetadata($key, $value); |
170
|
1 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set a bunch of metadata to all grouped routes. |
175
|
|
|
* |
176
|
|
|
* @param mixed[] $metadata |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
|
180
|
1 |
|
public function setMetadataArray(array $metadata) |
181
|
|
|
{ |
182
|
1 |
|
foreach ($this->routes as $route) |
183
|
1 |
|
$route->setMetadataArray($metadata); |
184
|
1 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set default parameters to all grouped routes. |
189
|
|
|
* |
190
|
|
|
* @param mixed[] $defaults |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
|
|
|
194
|
1 |
|
public function setDefaults(array $defaults) |
195
|
|
|
{ |
196
|
1 |
|
foreach ($this->routes as $route) |
197
|
1 |
|
$route->setDefaults($defaults); |
198
|
1 |
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set a default parameter to all grouped routes. |
203
|
|
|
* |
204
|
|
|
* @param string $key |
205
|
|
|
* @param mixed $value |
206
|
|
|
* |
207
|
|
|
* @return $this |
208
|
|
|
*/ |
209
|
|
|
|
210
|
1 |
|
public function setDefault($key, $value) |
211
|
|
|
{ |
212
|
1 |
|
foreach ($this->routes as $route) |
213
|
1 |
|
$route->setDefault($key, $value); |
214
|
1 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set one dispatch strategy to all grouped routes. |
219
|
|
|
* |
220
|
|
|
* @param string|Strategies\StrategyInterface $strategy |
221
|
|
|
* @return self |
222
|
|
|
*/ |
223
|
|
|
|
224
|
16 |
|
public function setStrategy($strategy) |
225
|
|
|
{ |
226
|
16 |
|
foreach ($this->routes as $route) |
227
|
16 |
|
$route->setStrategy($strategy); |
228
|
16 |
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Replace or define a constraint for all dynamic segments named by $name. |
233
|
|
|
* |
234
|
|
|
* @param string $name |
235
|
|
|
* @param string $regex |
236
|
|
|
* |
237
|
|
|
* @return self |
238
|
|
|
*/ |
239
|
|
|
|
240
|
1 |
|
public function setConstraint($name, $regex) |
241
|
|
|
{ |
242
|
1 |
|
foreach ($this->routes as $route) |
243
|
1 |
|
$route->setConstraint($name, $regex); |
244
|
1 |
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
} |
248
|
|
|
|