Drupal investigation

ChainRouteCollection.php 7.4KB

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