CollectionConfigurator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\HostTrait;
  20. use Traits\RouteTrait;
  21. private string|array|null $host = null;
  22. public function __construct(
  23. private RouteCollection $parent,
  24. string $name,
  25. private ?self $parentConfigurator = null, // for GC control
  26. private ?array $parentPrefixes = null,
  27. ) {
  28. $this->name = $name;
  29. $this->collection = new RouteCollection();
  30. $this->route = new Route('');
  31. }
  32. public function __sleep(): array
  33. {
  34. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  35. }
  36. public function __wakeup(): void
  37. {
  38. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  39. }
  40. public function __destruct()
  41. {
  42. if (null === $this->prefixes) {
  43. $this->collection->addPrefix($this->route->getPath());
  44. }
  45. if (null !== $this->host) {
  46. $this->addHost($this->collection, $this->host);
  47. }
  48. $this->parent->addCollection($this->collection);
  49. }
  50. /**
  51. * Creates a sub-collection.
  52. */
  53. final public function collection(string $name = ''): self
  54. {
  55. return new self($this->collection, $this->name.$name, $this, $this->prefixes);
  56. }
  57. /**
  58. * Sets the prefix to add to the path of all child routes.
  59. *
  60. * @param string|array $prefix the prefix, or the localized prefixes
  61. *
  62. * @return $this
  63. */
  64. final public function prefix(string|array $prefix): static
  65. {
  66. if (\is_array($prefix)) {
  67. if (null === $this->parentPrefixes) {
  68. // no-op
  69. } elseif ($missing = array_diff_key($this->parentPrefixes, $prefix)) {
  70. throw new \LogicException(sprintf('Collection "%s" is missing prefixes for locale(s) "%s".', $this->name, implode('", "', array_keys($missing))));
  71. } else {
  72. foreach ($prefix as $locale => $localePrefix) {
  73. if (!isset($this->parentPrefixes[$locale])) {
  74. throw new \LogicException(sprintf('Collection "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $this->name, $locale));
  75. }
  76. $prefix[$locale] = $this->parentPrefixes[$locale].$localePrefix;
  77. }
  78. }
  79. $this->prefixes = $prefix;
  80. $this->route->setPath('/');
  81. } else {
  82. $this->prefixes = null;
  83. $this->route->setPath($prefix);
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Sets the host to use for all child routes.
  89. *
  90. * @param string|array $host the host, or the localized hosts
  91. *
  92. * @return $this
  93. */
  94. final public function host(string|array $host): static
  95. {
  96. $this->host = $host;
  97. return $this;
  98. }
  99. /**
  100. * This method overrides the one from LocalizedRouteTrait.
  101. */
  102. private function createRoute(string $path): Route
  103. {
  104. return (clone $this->route)->setPath($path);
  105. }
  106. }