No Description

ImportConfigurator.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. use Symfony\Component\Routing\RouteCompiler;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class ImportConfigurator
  18. {
  19. use Traits\RouteTrait;
  20. private $parent;
  21. public function __construct(RouteCollection $parent, RouteCollection $route)
  22. {
  23. $this->parent = $parent;
  24. $this->route = $route;
  25. }
  26. /**
  27. * @return array
  28. */
  29. public function __sleep()
  30. {
  31. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  32. }
  33. public function __wakeup()
  34. {
  35. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  36. }
  37. public function __destruct()
  38. {
  39. $this->parent->addCollection($this->route);
  40. }
  41. /**
  42. * Sets the prefix to add to the path of all child routes.
  43. *
  44. * @param string|array $prefix the prefix, or the localized prefixes
  45. *
  46. * @return $this
  47. */
  48. final public function prefix($prefix, bool $trailingSlashOnRoot = true): self
  49. {
  50. if (!\is_array($prefix)) {
  51. $this->route->addPrefix($prefix);
  52. if (!$trailingSlashOnRoot) {
  53. $rootPath = (new Route(trim(trim($prefix), '/').'/'))->getPath();
  54. foreach ($this->route->all() as $route) {
  55. if ($route->getPath() === $rootPath) {
  56. $route->setPath(rtrim($rootPath, '/'));
  57. }
  58. }
  59. }
  60. } else {
  61. foreach ($prefix as $locale => $localePrefix) {
  62. $prefix[$locale] = trim(trim($localePrefix), '/');
  63. }
  64. foreach ($this->route->all() as $name => $route) {
  65. if (null === $locale = $route->getDefault('_locale')) {
  66. $this->route->remove($name);
  67. foreach ($prefix as $locale => $localePrefix) {
  68. $localizedRoute = clone $route;
  69. $localizedRoute->setDefault('_locale', $locale);
  70. $localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
  71. $localizedRoute->setDefault('_canonical_route', $name);
  72. $localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
  73. $this->route->add($name.'.'.$locale, $localizedRoute);
  74. }
  75. } elseif (!isset($prefix[$locale])) {
  76. throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
  77. } else {
  78. $route->setPath($prefix[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
  79. $this->route->add($name, $route);
  80. }
  81. }
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Sets the prefix to add to the name of all child routes.
  87. *
  88. * @return $this
  89. */
  90. final public function namePrefix(string $namePrefix): self
  91. {
  92. $this->route->addNamePrefix($namePrefix);
  93. return $this;
  94. }
  95. }