forked from symfony-cmf/Routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChainRouteCollection.php
272 lines (245 loc) · 7.44 KB
/
ChainRouteCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Routing;
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ChainRouteCollection extends RouteCollection
{
/**
* @var RouteCollection[]
*/
private $routeCollections = array();
/**
* @var RouteCollection
*/
private $routeCollection;
public function __clone()
{
foreach ($this->routeCollections as $routeCollection) {
$this->routeCollections[] = clone $routeCollection;
}
}
/**
* Gets the current RouteCollection as an Iterator that includes all routes.
*
* It implements \IteratorAggregate.
*
* @see all()
*
* @return \ArrayIterator An \ArrayIterator object for iterating over routes
*/
public function getIterator()
{
return new \ArrayIterator($this->all());
}
/**
* Gets the number of Routes in this collection.
*
* @return int The number of routes
*/
public function count()
{
$count = 0;
foreach ($this->routeCollections as $routeCollection) {
$count += $routeCollection->count();
}
return $count;
}
/**
* Adds a route.
*
* @param string $name The route name
* @param Route $route A Route instance
*/
public function add($name, Route $route)
{
$this->createInternalCollection();
$this->routeCollection->add($name, $route);
}
/**
* Returns all routes in this collection.
*
* @return Route[] An array of routes
*/
public function all()
{
$routeCollectionAll = new RouteCollection();
foreach ($this->routeCollections as $routeCollection) {
$routeCollectionAll->addCollection($routeCollection);
}
return $routeCollectionAll->all();
}
/**
* Gets a route by name.
*
* @param string $name The route name
*
* @return Route|null A Route instance or null when not found
*/
public function get($name)
{
foreach ($this->routeCollections as $routeCollection) {
$route = $routeCollection->get($name);
if (null !== $route) {
return $route;
}
}
return;
}
/**
* Removes a route or an array of routes by name from the collection.
*
* @param string|array $name The route name or an array of route names
*/
public function remove($name)
{
foreach ($this->routeCollections as $routeCollection) {
$route = $routeCollection->get($name);
if (null !== $route) {
$routeCollection->remove($name);
}
}
}
/**
* Adds a route collection at the end of the current set by appending all
* routes of the added collection.
*
* @param RouteCollection $collection A RouteCollection instance
*/
public function addCollection(RouteCollection $collection)
{
$this->routeCollections[] = $collection;
}
/**
* Adds a prefix to the path of all child routes.
*
* @param string $prefix An optional prefix to add before each pattern of the route collection
* @param array $defaults An array of default values
* @param array $requirements An array of requirements
*/
public function addPrefix($prefix, array $defaults = array(), array $requirements = array())
{
$this->createInternalCollection();
foreach ($this->routeCollections as $routeCollection) {
$routeCollection->addPrefix($prefix, $defaults, $requirements);
}
}
/**
* Sets the host pattern on all routes.
*
* @param string $pattern The pattern
* @param array $defaults An array of default values
* @param array $requirements An array of requirements
*/
public function setHost($pattern, array $defaults = array(), array $requirements = array())
{
$this->createInternalCollection();
foreach ($this->routeCollections as $routeCollection) {
$routeCollection->setHost($pattern, $defaults, $requirements);
}
}
/**
* Adds defaults to all routes.
*
* An existing default value under the same name in a route will be overridden.
*
* @param array $defaults An array of default values
*/
public function addDefaults(array $defaults)
{
$this->createInternalCollection();
foreach ($this->routeCollections as $routeCollection) {
$routeCollection->addDefaults($defaults);
}
}
/**
* Adds requirements to all routes.
*
* An existing requirement under the same name in a route will be overridden.
*
* @param array $requirements An array of requirements
*/
public function addRequirements(array $requirements)
{
$this->createInternalCollection();
foreach ($this->routeCollections as $routeCollection) {
$routeCollection->addRequirements($requirements);
}
}
/**
* Adds options to all routes.
*
* An existing option value under the same name in a route will be overridden.
*
* @param array $options An array of options
*/
public function addOptions(array $options)
{
$this->createInternalCollection();
foreach ($this->routeCollections as $routeCollection) {
$routeCollection->addOptions($options);
}
}
/**
* Sets the schemes (e.g. 'https') all child routes are restricted to.
*
* @param string|array $schemes The scheme or an array of schemes
*/
public function setSchemes($schemes)
{
$this->createInternalCollection();
foreach ($this->routeCollections as $routeCollection) {
$routeCollection->setSchemes($schemes);
}
}
/**
* Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
*
* @param string|array $methods The method or an array of methods
*/
public function setMethods($methods)
{
$this->createInternalCollection();
foreach ($this->routeCollections as $routeCollection) {
$routeCollection->setMethods($methods);
}
}
/**
* Returns an array of resources loaded to build this collection.
*
* @return ResourceInterface[] An array of resources
*/
public function getResources()
{
$resources = array();
foreach ($this->routeCollections as $routeCollection) {
$resources = array_merge($resources, $routeCollection->getResources());
}
return array_unique($resources);
}
/**
* Adds a resource for this collection.
*
* @param ResourceInterface $resource A resource instance
*/
public function addResource(ResourceInterface $resource)
{
$this->createInternalCollection();
$this->routeCollection->addResource($resource);
}
private function createInternalCollection()
{
if (!$this->routeCollection instanceof RouteCollection) {
$this->routeCollection = new RouteCollection();
$this->routeCollections[] = $this->routeCollection;
}
}
}