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