ImportConfigurator.php 3.0 KB

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