AddTrait.php 3.0 KB

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