comparison vendor/symfony-cmf/routing/ChainRouteCollection.php @ 0:c75dbcec494b

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