Keine Beschreibung

CollectionConfigurator.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /**
  33. * @return array
  34. */
  35. public function __sleep()
  36. {
  37. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  38. }
  39. public function __wakeup()
  40. {
  41. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  42. }
  43. public function __destruct()
  44. {
  45. if (null === $this->prefixes) {
  46. $this->collection->addPrefix($this->route->getPath());
  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($prefix): self
  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. private function createRoute(string $path): Route
  88. {
  89. return (clone $this->route)->setPath($path);
  90. }
  91. }