CollectionConfigurator.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. private $parentPrefixes;
  23. public function __construct(RouteCollection $parent, string $name, self $parentConfigurator = null, array $parentPrefixes = null)
  24. {
  25. $this->parent = $parent;
  26. $this->name = $name;
  27. $this->collection = new RouteCollection();
  28. $this->route = new Route('');
  29. $this->parentConfigurator = $parentConfigurator; // for GC control
  30. $this->parentPrefixes = $parentPrefixes;
  31. }
  32. public function __destruct()
  33. {
  34. if (null === $this->prefixes) {
  35. $this->collection->addPrefix($this->route->getPath());
  36. }
  37. $this->parent->addCollection($this->collection);
  38. }
  39. /**
  40. * Creates a sub-collection.
  41. *
  42. * @return self
  43. */
  44. final public function collection($name = '')
  45. {
  46. return new self($this->collection, $this->name.$name, $this, $this->prefixes);
  47. }
  48. /**
  49. * Sets the prefix to add to the path of all child routes.
  50. *
  51. * @param string|array $prefix the prefix, or the localized prefixes
  52. *
  53. * @return $this
  54. */
  55. final public function prefix($prefix)
  56. {
  57. if (\is_array($prefix)) {
  58. if (null === $this->parentPrefixes) {
  59. // no-op
  60. } elseif ($missing = array_diff_key($this->parentPrefixes, $prefix)) {
  61. throw new \LogicException(sprintf('Collection "%s" is missing prefixes for locale(s) "%s".', $this->name, implode('", "', array_keys($missing))));
  62. } else {
  63. foreach ($prefix as $locale => $localePrefix) {
  64. if (!isset($this->parentPrefixes[$locale])) {
  65. throw new \LogicException(sprintf('Collection "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $this->name, $locale));
  66. }
  67. $prefix[$locale] = $this->parentPrefixes[$locale].$localePrefix;
  68. }
  69. }
  70. $this->prefixes = $prefix;
  71. $this->route->setPath('/');
  72. } else {
  73. $this->prefixes = null;
  74. $this->route->setPath($prefix);
  75. }
  76. return $this;
  77. }
  78. private function createRoute($path): Route
  79. {
  80. return (clone $this->route)->setPath($path);
  81. }
  82. }