123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?php
- namespace Symfony\Component\Routing;
- use Symfony\Component\Config\Resource\ResourceInterface;
- class RouteCollection implements \IteratorAggregate, \Countable
- {
-
- private $routes = [];
-
- private $resources = [];
- public function __clone()
- {
- foreach ($this->routes as $name => $route) {
- $this->routes[$name] = clone $route;
- }
- }
-
- public function getIterator()
- {
- return new \ArrayIterator($this->routes);
- }
-
- public function count()
- {
- return \count($this->routes);
- }
- public function add(string $name, Route $route)
- {
- unset($this->routes[$name]);
- $this->routes[$name] = $route;
- }
-
- public function all()
- {
- return $this->routes;
- }
-
- public function get(string $name)
- {
- return isset($this->routes[$name]) ? $this->routes[$name] : null;
- }
-
- public function remove($name)
- {
- foreach ((array) $name as $n) {
- unset($this->routes[$n]);
- }
- }
-
- public function addCollection(self $collection)
- {
-
-
- foreach ($collection->all() as $name => $route) {
- unset($this->routes[$name]);
- $this->routes[$name] = $route;
- }
- foreach ($collection->getResources() as $resource) {
- $this->addResource($resource);
- }
- }
-
- public function addPrefix(string $prefix, array $defaults = [], array $requirements = [])
- {
- $prefix = trim(trim($prefix), '/');
- if ('' === $prefix) {
- return;
- }
- foreach ($this->routes as $route) {
- $route->setPath('/'.$prefix.$route->getPath());
- $route->addDefaults($defaults);
- $route->addRequirements($requirements);
- }
- }
-
- public function addNamePrefix(string $prefix)
- {
- $prefixedRoutes = [];
- foreach ($this->routes as $name => $route) {
- $prefixedRoutes[$prefix.$name] = $route;
- if (null !== $name = $route->getDefault('_canonical_route')) {
- $route->setDefault('_canonical_route', $prefix.$name);
- }
- }
- $this->routes = $prefixedRoutes;
- }
-
- public function setHost(?string $pattern, array $defaults = [], array $requirements = [])
- {
- foreach ($this->routes as $route) {
- $route->setHost($pattern);
- $route->addDefaults($defaults);
- $route->addRequirements($requirements);
- }
- }
-
- public function setCondition(?string $condition)
- {
- foreach ($this->routes as $route) {
- $route->setCondition($condition);
- }
- }
-
- public function addDefaults(array $defaults)
- {
- if ($defaults) {
- foreach ($this->routes as $route) {
- $route->addDefaults($defaults);
- }
- }
- }
-
- public function addRequirements(array $requirements)
- {
- if ($requirements) {
- foreach ($this->routes as $route) {
- $route->addRequirements($requirements);
- }
- }
- }
-
- public function addOptions(array $options)
- {
- if ($options) {
- foreach ($this->routes as $route) {
- $route->addOptions($options);
- }
- }
- }
-
- public function setSchemes($schemes)
- {
- foreach ($this->routes as $route) {
- $route->setSchemes($schemes);
- }
- }
-
- public function setMethods($methods)
- {
- foreach ($this->routes as $route) {
- $route->setMethods($methods);
- }
- }
-
- public function getResources()
- {
- return array_values($this->resources);
- }
-
- public function addResource(ResourceInterface $resource)
- {
- $key = (string) $resource;
- if (!isset($this->resources[$key])) {
- $this->resources[$key] = $resource;
- }
- }
- }
|