CollectionConfigurator.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. final public function collection(string $name = ''): self
  43. {
  44. return new self($this->collection, $this->name.$name, $this, $this->prefixes);
  45. }
  46. /**
  47. * Sets the prefix to add to the path of all child routes.
  48. *
  49. * @param string|array $prefix the prefix, or the localized prefixes
  50. *
  51. * @return $this
  52. */
  53. final public function prefix($prefix): self
  54. {
  55. if (\is_array($prefix)) {
  56. if (null === $this->parentPrefixes) {
  57. // no-op
  58. } elseif ($missing = array_diff_key($this->parentPrefixes, $prefix)) {
  59. throw new \LogicException(sprintf('Collection "%s" is missing prefixes for locale(s) "%s".', $this->name, implode('", "', array_keys($missing))));
  60. } else {
  61. foreach ($prefix as $locale => $localePrefix) {
  62. if (!isset($this->parentPrefixes[$locale])) {
  63. throw new \LogicException(sprintf('Collection "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $this->name, $locale));
  64. }
  65. $prefix[$locale] = $this->parentPrefixes[$locale].$localePrefix;
  66. }
  67. }
  68. $this->prefixes = $prefix;
  69. $this->route->setPath('/');
  70. } else {
  71. $this->prefixes = null;
  72. $this->route->setPath($prefix);
  73. }
  74. return $this;
  75. }
  76. private function createRoute(string $path): Route
  77. {
  78. return (clone $this->route)->setPath($path);
  79. }
  80. }