PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/PHP/Test/vendor/symfony/symfony/src/Symfony/Component/Routing/RouteCollection.php

https://bitbucket.org/AdriVanHoudt/school
PHP | 364 lines | 152 code | 37 blank | 175 comment | 10 complexity | 11d1a3571814720d34b9b4e5adaebed7 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing;
  11. use Symfony\Component\Config\Resource\ResourceInterface;
  12. /**
  13. * A RouteCollection represents a set of Route instances.
  14. *
  15. * When adding a route at the end of the collection, an existing route
  16. * with the same name is removed first. So there can only be one route
  17. * with a given name.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Tobias Schultze <http://tobion.de>
  21. *
  22. * @api
  23. */
  24. class RouteCollection implements \IteratorAggregate, \Countable
  25. {
  26. /**
  27. * @var Route[]
  28. */
  29. private $routes = array();
  30. /**
  31. * @var array
  32. */
  33. private $resources = array();
  34. /**
  35. * @var string
  36. * @deprecated since version 2.2, will be removed in 2.3
  37. */
  38. private $prefix = '';
  39. /**
  40. * @var RouteCollection|null
  41. * @deprecated since version 2.2, will be removed in 2.3
  42. */
  43. private $parent;
  44. public function __clone()
  45. {
  46. foreach ($this->routes as $name => $route) {
  47. $this->routes[$name] = clone $route;
  48. }
  49. }
  50. /**
  51. * Gets the parent RouteCollection.
  52. *
  53. * @return RouteCollection|null The parent RouteCollection or null when it's the root
  54. *
  55. * @deprecated since version 2.2, will be removed in 2.3
  56. */
  57. public function getParent()
  58. {
  59. return $this->parent;
  60. }
  61. /**
  62. * Gets the root RouteCollection.
  63. *
  64. * @return RouteCollection The root RouteCollection
  65. *
  66. * @deprecated since version 2.2, will be removed in 2.3
  67. */
  68. public function getRoot()
  69. {
  70. $parent = $this;
  71. while ($parent->getParent()) {
  72. $parent = $parent->getParent();
  73. }
  74. return $parent;
  75. }
  76. /**
  77. * Gets the current RouteCollection as an Iterator that includes all routes.
  78. *
  79. * It implements \IteratorAggregate.
  80. *
  81. * @see all()
  82. *
  83. * @return \ArrayIterator An \ArrayIterator object for iterating over routes
  84. */
  85. public function getIterator()
  86. {
  87. return new \ArrayIterator($this->routes);
  88. }
  89. /**
  90. * Gets the number of Routes in this collection.
  91. *
  92. * @return int The number of routes
  93. */
  94. public function count()
  95. {
  96. return count($this->routes);
  97. }
  98. /**
  99. * Adds a route.
  100. *
  101. * @param string $name The route name
  102. * @param Route $route A Route instance
  103. *
  104. * @api
  105. */
  106. public function add($name, Route $route)
  107. {
  108. unset($this->routes[$name]);
  109. $this->routes[$name] = $route;
  110. }
  111. /**
  112. * Returns all routes in this collection.
  113. *
  114. * @return Route[] An array of routes
  115. */
  116. public function all()
  117. {
  118. return $this->routes;
  119. }
  120. /**
  121. * Gets a route by name.
  122. *
  123. * @param string $name The route name
  124. *
  125. * @return Route|null A Route instance or null when not found
  126. */
  127. public function get($name)
  128. {
  129. return isset($this->routes[$name]) ? $this->routes[$name] : null;
  130. }
  131. /**
  132. * Removes a route or an array of routes by name from the collection
  133. *
  134. * For BC it's also removed from the root, which will not be the case in 2.3
  135. * as the RouteCollection won't be a tree structure.
  136. *
  137. * @param string|array $name The route name or an array of route names
  138. */
  139. public function remove($name)
  140. {
  141. // just for BC
  142. $root = $this->getRoot();
  143. foreach ((array) $name as $n) {
  144. unset($root->routes[$n]);
  145. unset($this->routes[$n]);
  146. }
  147. }
  148. /**
  149. * Adds a route collection at the end of the current set by appending all
  150. * routes of the added collection.
  151. *
  152. * @param RouteCollection $collection A RouteCollection instance
  153. *
  154. * @api
  155. */
  156. public function addCollection(RouteCollection $collection)
  157. {
  158. // This is to keep BC for getParent() and getRoot(). It does not prevent
  159. // infinite loops by recursive referencing. But we don't need that logic
  160. // anymore as the tree logic has been deprecated and we are just widening
  161. // the accepted range.
  162. $collection->parent = $this;
  163. // this is to keep BC
  164. $numargs = func_num_args();
  165. if ($numargs > 1) {
  166. $collection->addPrefix($this->prefix . func_get_arg(1));
  167. if ($numargs > 2) {
  168. $collection->addDefaults(func_get_arg(2));
  169. if ($numargs > 3) {
  170. $collection->addRequirements(func_get_arg(3));
  171. if ($numargs > 4) {
  172. $collection->addOptions(func_get_arg(4));
  173. }
  174. }
  175. }
  176. } else {
  177. // the sub-collection must have the prefix of the parent (current instance) prepended because it does not
  178. // necessarily already have it applied (depending on the order RouteCollections are added to each other)
  179. // this will be removed when the BC layer for getPrefix() is removed
  180. $collection->addPrefix($this->prefix);
  181. }
  182. // we need to remove all routes with the same names first because just replacing them
  183. // would not place the new route at the end of the merged array
  184. foreach ($collection->all() as $name => $route) {
  185. unset($this->routes[$name]);
  186. $this->routes[$name] = $route;
  187. }
  188. $this->resources = array_merge($this->resources, $collection->getResources());
  189. }
  190. /**
  191. * Adds a prefix to the path of all child routes.
  192. *
  193. * @param string $prefix An optional prefix to add before each pattern of the route collection
  194. * @param array $defaults An array of default values
  195. * @param array $requirements An array of requirements
  196. *
  197. * @api
  198. */
  199. public function addPrefix($prefix, array $defaults = array(), array $requirements = array())
  200. {
  201. $prefix = trim(trim($prefix), '/');
  202. if ('' === $prefix) {
  203. return;
  204. }
  205. // a prefix must start with a single slash and must not end with a slash
  206. $this->prefix = '/' . $prefix . $this->prefix;
  207. // this is to keep BC
  208. $options = func_num_args() > 3 ? func_get_arg(3) : array();
  209. foreach ($this->routes as $route) {
  210. $route->setPath('/' . $prefix . $route->getPath());
  211. $route->addDefaults($defaults);
  212. $route->addRequirements($requirements);
  213. $route->addOptions($options);
  214. }
  215. }
  216. /**
  217. * Returns the prefix that may contain placeholders.
  218. *
  219. * @return string The prefix
  220. *
  221. * @deprecated since version 2.2, will be removed in 2.3
  222. */
  223. public function getPrefix()
  224. {
  225. return $this->prefix;
  226. }
  227. /**
  228. * Sets the host pattern on all routes.
  229. *
  230. * @param string $pattern The pattern
  231. * @param array $defaults An array of default values
  232. * @param array $requirements An array of requirements
  233. */
  234. public function setHost($pattern, array $defaults = array(), array $requirements = array())
  235. {
  236. foreach ($this->routes as $route) {
  237. $route->setHost($pattern);
  238. $route->addDefaults($defaults);
  239. $route->addRequirements($requirements);
  240. }
  241. }
  242. /**
  243. * Adds defaults to all routes.
  244. *
  245. * An existing default value under the same name in a route will be overridden.
  246. *
  247. * @param array $defaults An array of default values
  248. */
  249. public function addDefaults(array $defaults)
  250. {
  251. if ($defaults) {
  252. foreach ($this->routes as $route) {
  253. $route->addDefaults($defaults);
  254. }
  255. }
  256. }
  257. /**
  258. * Adds requirements to all routes.
  259. *
  260. * An existing requirement under the same name in a route will be overridden.
  261. *
  262. * @param array $requirements An array of requirements
  263. */
  264. public function addRequirements(array $requirements)
  265. {
  266. if ($requirements) {
  267. foreach ($this->routes as $route) {
  268. $route->addRequirements($requirements);
  269. }
  270. }
  271. }
  272. /**
  273. * Adds options to all routes.
  274. *
  275. * An existing option value under the same name in a route will be overridden.
  276. *
  277. * @param array $options An array of options
  278. */
  279. public function addOptions(array $options)
  280. {
  281. if ($options) {
  282. foreach ($this->routes as $route) {
  283. $route->addOptions($options);
  284. }
  285. }
  286. }
  287. /**
  288. * Sets the schemes (e.g. 'https') all child routes are restricted to.
  289. *
  290. * @param string|array $schemes The scheme or an array of schemes
  291. */
  292. public function setSchemes($schemes)
  293. {
  294. foreach ($this->routes as $route) {
  295. $route->setSchemes($schemes);
  296. }
  297. }
  298. /**
  299. * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
  300. *
  301. * @param string|array $methods The method or an array of methods
  302. */
  303. public function setMethods($methods)
  304. {
  305. foreach ($this->routes as $route) {
  306. $route->setMethods($methods);
  307. }
  308. }
  309. /**
  310. * Returns an array of resources loaded to build this collection.
  311. *
  312. * @return ResourceInterface[] An array of resources
  313. */
  314. public function getResources()
  315. {
  316. return array_unique($this->resources);
  317. }
  318. /**
  319. * Adds a resource for this collection.
  320. *
  321. * @param ResourceInterface $resource A resource instance
  322. */
  323. public function addResource(ResourceInterface $resource)
  324. {
  325. $this->resources[] = $resource;
  326. }
  327. }