|
0
|
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 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
20 |
* |
|
|
21 |
* @api |
|
|
22 |
*/ |
|
|
23 |
class RouteCollection implements \IteratorAggregate |
|
|
24 |
{ |
|
|
25 |
private $routes; |
|
|
26 |
private $resources; |
|
|
27 |
private $prefix; |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* Constructor. |
|
|
31 |
* |
|
|
32 |
* @api |
|
|
33 |
*/ |
|
|
34 |
public function __construct() |
|
|
35 |
{ |
|
|
36 |
$this->routes = array(); |
|
|
37 |
$this->resources = array(); |
|
|
38 |
$this->prefix = ''; |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
public function getIterator() |
|
|
42 |
{ |
|
|
43 |
return new \ArrayIterator($this->routes); |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
/** |
|
|
47 |
* Adds a route. |
|
|
48 |
* |
|
|
49 |
* @param string $name The route name |
|
|
50 |
* @param Route $route A Route instance |
|
|
51 |
* |
|
|
52 |
* @throws \InvalidArgumentException When route name contains non valid characters |
|
|
53 |
* |
|
|
54 |
* @api |
|
|
55 |
*/ |
|
|
56 |
public function add($name, Route $route) |
|
|
57 |
{ |
|
|
58 |
if (!preg_match('/^[a-z0-9A-Z_.]+$/', $name)) { |
|
|
59 |
throw new \InvalidArgumentException(sprintf('Name "%s" contains non valid characters for a route name.', $name)); |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
$this->routes[$name] = $route; |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
/** |
|
|
66 |
* Returns the array of routes. |
|
|
67 |
* |
|
|
68 |
* @return array An array of routes |
|
|
69 |
*/ |
|
|
70 |
public function all() |
|
|
71 |
{ |
|
|
72 |
$routes = array(); |
|
|
73 |
foreach ($this->routes as $name => $route) { |
|
|
74 |
if ($route instanceof RouteCollection) { |
|
|
75 |
$routes = array_merge($routes, $route->all()); |
|
|
76 |
} else { |
|
|
77 |
$routes[$name] = $route; |
|
|
78 |
} |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
return $routes; |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
/** |
|
|
85 |
* Gets a route by name. |
|
|
86 |
* |
|
|
87 |
* @param string $name The route name |
|
|
88 |
* |
|
|
89 |
* @return Route $route A Route instance |
|
|
90 |
*/ |
|
|
91 |
public function get($name) |
|
|
92 |
{ |
|
|
93 |
// get the latest defined route |
|
|
94 |
foreach (array_reverse($this->routes) as $routes) { |
|
|
95 |
if (!$routes instanceof RouteCollection) { |
|
|
96 |
continue; |
|
|
97 |
} |
|
|
98 |
|
|
|
99 |
if (null !== $route = $routes->get($name)) { |
|
|
100 |
return $route; |
|
|
101 |
} |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
if (isset($this->routes[$name])) { |
|
|
105 |
return $this->routes[$name]; |
|
|
106 |
} |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
/** |
|
|
110 |
* Adds a route collection to the current set of routes (at the end of the current set). |
|
|
111 |
* |
|
|
112 |
* @param RouteCollection $collection A RouteCollection instance |
|
|
113 |
* @param string $prefix An optional prefix to add before each pattern of the route collection |
|
|
114 |
* |
|
|
115 |
* @api |
|
|
116 |
*/ |
|
|
117 |
public function addCollection(RouteCollection $collection, $prefix = '') |
|
|
118 |
{ |
|
|
119 |
$collection->addPrefix($prefix); |
|
|
120 |
|
|
|
121 |
$this->routes[] = $collection; |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
/** |
|
|
125 |
* Adds a prefix to all routes in the current set. |
|
|
126 |
* |
|
|
127 |
* @param string $prefix An optional prefix to add before each pattern of the route collection |
|
|
128 |
* |
|
|
129 |
* @api |
|
|
130 |
*/ |
|
|
131 |
public function addPrefix($prefix) |
|
|
132 |
{ |
|
|
133 |
// a prefix must not end with a slash |
|
|
134 |
$prefix = rtrim($prefix, '/'); |
|
|
135 |
|
|
|
136 |
if (!$prefix) { |
|
|
137 |
return; |
|
|
138 |
} |
|
|
139 |
|
|
|
140 |
// a prefix must start with a slash |
|
|
141 |
if ('/' !== $prefix[0]) { |
|
|
142 |
$prefix = '/'.$prefix; |
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
$this->prefix = $prefix.$this->prefix; |
|
|
146 |
|
|
|
147 |
foreach ($this->routes as $name => $route) { |
|
|
148 |
if ($route instanceof RouteCollection) { |
|
|
149 |
$route->addPrefix($prefix); |
|
|
150 |
} else { |
|
|
151 |
$route->setPattern($prefix.$route->getPattern()); |
|
|
152 |
} |
|
|
153 |
} |
|
|
154 |
} |
|
|
155 |
|
|
|
156 |
public function getPrefix() |
|
|
157 |
{ |
|
|
158 |
return $this->prefix; |
|
|
159 |
} |
|
|
160 |
|
|
|
161 |
/** |
|
|
162 |
* Returns an array of resources loaded to build this collection. |
|
|
163 |
* |
|
|
164 |
* @return ResourceInterface[] An array of resources |
|
|
165 |
*/ |
|
|
166 |
public function getResources() |
|
|
167 |
{ |
|
|
168 |
$resources = $this->resources; |
|
|
169 |
foreach ($this as $routes) { |
|
|
170 |
if ($routes instanceof RouteCollection) { |
|
|
171 |
$resources = array_merge($resources, $routes->getResources()); |
|
|
172 |
} |
|
|
173 |
} |
|
|
174 |
|
|
|
175 |
return array_unique($resources); |
|
|
176 |
} |
|
|
177 |
|
|
|
178 |
/** |
|
|
179 |
* Adds a resource for this collection. |
|
|
180 |
* |
|
|
181 |
* @param ResourceInterface $resource A resource instance |
|
|
182 |
*/ |
|
|
183 |
public function addResource(ResourceInterface $resource) |
|
|
184 |
{ |
|
|
185 |
$this->resources[] = $resource; |
|
|
186 |
} |
|
|
187 |
} |