CollectionConfigurator.php 3.4 KB

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