CollectionConfigurator.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Loader\Configurator;
  11. use Symfony\Component\Routing\Route;
  12. use Symfony\Component\Routing\RouteCollection;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class CollectionConfigurator
  17. {
  18. use Traits\AddTrait;
  19. use Traits\RouteTrait;
  20. private $parent;
  21. private $parentConfigurator;
  22. public function __construct(RouteCollection $parent, $name, self $parentConfigurator = null)
  23. {
  24. $this->parent = $parent;
  25. $this->name = $name;
  26. $this->collection = new RouteCollection();
  27. $this->route = new Route('');
  28. $this->parentConfigurator = $parentConfigurator; // for GC control
  29. }
  30. public function __destruct()
  31. {
  32. $this->collection->addPrefix(rtrim($this->route->getPath(), '/'));
  33. $this->parent->addCollection($this->collection);
  34. }
  35. /**
  36. * Adds a route.
  37. *
  38. * @param string $name
  39. * @param string $path
  40. *
  41. * @return RouteConfigurator
  42. */
  43. final public function add($name, $path)
  44. {
  45. $this->collection->add($this->name.$name, $route = clone $this->route);
  46. return new RouteConfigurator($this->collection, $route->setPath($path), $this->name, $this);
  47. }
  48. /**
  49. * Creates a sub-collection.
  50. *
  51. * @return self
  52. */
  53. final public function collection($name = '')
  54. {
  55. return new self($this->collection, $this->name.$name, $this);
  56. }
  57. /**
  58. * Sets the prefix to add to the path of all child routes.
  59. *
  60. * @param string $prefix
  61. *
  62. * @return $this
  63. */
  64. final public function prefix($prefix)
  65. {
  66. $this->route->setPath($prefix);
  67. return $this;
  68. }
  69. }