AddTrait.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\AliasConfigurator;
  12. use Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator;
  13. use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
  14. use Symfony\Component\Routing\RouteCollection;
  15. /**
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. trait AddTrait
  19. {
  20. use LocalizedRouteTrait;
  21. protected RouteCollection $collection;
  22. protected string $name = '';
  23. protected ?array $prefixes = null;
  24. /**
  25. * Adds a route.
  26. *
  27. * @param string|array $path the path, or the localized paths of the route
  28. */
  29. public function add(string $name, string|array $path): RouteConfigurator
  30. {
  31. $parentConfigurator = $this instanceof CollectionConfigurator ? $this : ($this instanceof RouteConfigurator ? $this->parentConfigurator : null);
  32. $route = $this->createLocalizedRoute($this->collection, $name, $path, $this->name, $this->prefixes);
  33. return new RouteConfigurator($this->collection, $route, $this->name, $parentConfigurator, $this->prefixes);
  34. }
  35. public function alias(string $name, string $alias): AliasConfigurator
  36. {
  37. return new AliasConfigurator($this->collection->addAlias($name, $alias));
  38. }
  39. /**
  40. * Adds a route.
  41. *
  42. * @param string|array $path the path, or the localized paths of the route
  43. */
  44. public function __invoke(string $name, string|array $path): RouteConfigurator
  45. {
  46. return $this->add($name, $path);
  47. }
  48. }