No Description

AddTrait.php 3.1KB

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\Traits;
  11. use Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator;
  12. use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. use Symfony\Component\Routing\RouteCompiler;
  16. trait AddTrait
  17. {
  18. /**
  19. * @var RouteCollection
  20. */
  21. private $collection;
  22. private $name = '';
  23. private $prefixes;
  24. /**
  25. * Adds a route.
  26. *
  27. * @param string|array $path the path, or the localized paths of the route
  28. */
  29. final public function add(string $name, $path): RouteConfigurator
  30. {
  31. $paths = [];
  32. $parentConfigurator = $this instanceof CollectionConfigurator ? $this : ($this instanceof RouteConfigurator ? $this->parentConfigurator : null);
  33. if (\is_array($path)) {
  34. if (null === $this->prefixes) {
  35. $paths = $path;
  36. } elseif ($missing = array_diff_key($this->prefixes, $path)) {
  37. throw new \LogicException(sprintf('Route "%s" is missing routes for locale(s) "%s".', $name, implode('", "', array_keys($missing))));
  38. } else {
  39. foreach ($path as $locale => $localePath) {
  40. if (!isset($this->prefixes[$locale])) {
  41. throw new \LogicException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
  42. }
  43. $paths[$locale] = $this->prefixes[$locale].$localePath;
  44. }
  45. }
  46. } elseif (null !== $this->prefixes) {
  47. foreach ($this->prefixes as $locale => $prefix) {
  48. $paths[$locale] = $prefix.$path;
  49. }
  50. } else {
  51. $this->collection->add($this->name.$name, $route = $this->createRoute($path));
  52. return new RouteConfigurator($this->collection, $route, $this->name, $parentConfigurator, $this->prefixes);
  53. }
  54. $routes = new RouteCollection();
  55. foreach ($paths as $locale => $path) {
  56. $routes->add($name.'.'.$locale, $route = $this->createRoute($path));
  57. $this->collection->add($this->name.$name.'.'.$locale, $route);
  58. $route->setDefault('_locale', $locale);
  59. $route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
  60. $route->setDefault('_canonical_route', $this->name.$name);
  61. }
  62. return new RouteConfigurator($this->collection, $routes, $this->name, $parentConfigurator, $this->prefixes);
  63. }
  64. /**
  65. * Adds a route.
  66. *
  67. * @param string|array $path the path, or the localized paths of the route
  68. */
  69. final public function __invoke(string $name, $path): RouteConfigurator
  70. {
  71. return $this->add($name, $path);
  72. }
  73. private function createRoute(string $path): Route
  74. {
  75. return new Route($path);
  76. }
  77. }