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