CollectionConfigurator.php 3.2 KB

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