RouteCollection.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class RouteCollection
  18. {
  19. protected $routes;
  20. protected $resources;
  21. /**
  22. * Constructor.
  23. */
  24. public function __construct()
  25. {
  26. $this->routes = array();
  27. $this->resources = array();
  28. }
  29. /**
  30. * Adds a route.
  31. *
  32. * @param string $name The route name
  33. * @param Route $route A Route instance
  34. *
  35. * @throws \InvalidArgumentException When route name contains non valid characters
  36. */
  37. public function add($name, Route $route)
  38. {
  39. if (!preg_match('/^[a-z0-9A-Z_.]+$/', $name)) {
  40. throw new \InvalidArgumentException(sprintf('Name "%s" contains non valid characters for a route name.', $name));
  41. }
  42. $this->routes[$name] = $route;
  43. }
  44. /**
  45. * Returns the array of routes.
  46. *
  47. * @return array An array of routes
  48. */
  49. public function all()
  50. {
  51. return $this->routes;
  52. }
  53. /**
  54. * Gets a route by name.
  55. *
  56. * @param string $name The route name
  57. *
  58. * @return Route $route A Route instance
  59. */
  60. public function get($name)
  61. {
  62. return isset($this->routes[$name]) ? $this->routes[$name] : null;
  63. }
  64. /**
  65. * Adds a route collection to the current set of routes (at the end of the current set).
  66. *
  67. * @param RouteCollection $collection A RouteCollection instance
  68. * @param string $prefix An optional prefix to add before each pattern of the route collection
  69. */
  70. public function addCollection(RouteCollection $collection, $prefix = '')
  71. {
  72. $collection->addPrefix($prefix);
  73. foreach ($collection->getResources() as $resource) {
  74. $this->addResource($resource);
  75. }
  76. $this->routes = array_merge($this->routes, $collection->all());
  77. }
  78. /**
  79. * Adds a prefix to all routes in the current set.
  80. *
  81. * @param string $prefix An optional prefix to add before each pattern of the route collection
  82. */
  83. public function addPrefix($prefix)
  84. {
  85. if (!$prefix) {
  86. return;
  87. }
  88. foreach ($this->all() as $route) {
  89. $route->setPattern($prefix.$route->getPattern());
  90. }
  91. }
  92. /**
  93. * Returns an array of resources loaded to build this collection.
  94. *
  95. * @return ResourceInterface[] An array of resources
  96. */
  97. public function getResources()
  98. {
  99. return array_unique($this->resources);
  100. }
  101. /**
  102. * Adds a resource for this collection.
  103. *
  104. * @param ResourceInterface $resource A resource instance
  105. */
  106. public function addResource(ResourceInterface $resource)
  107. {
  108. $this->resources[] = $resource;
  109. }
  110. }