AddTrait.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. /**
  22. * @var RouteCollection
  23. */
  24. protected $collection;
  25. protected $name = '';
  26. protected $prefixes;
  27. /**
  28. * Adds a route.
  29. *
  30. * @param string|array $path the path, or the localized paths of the route
  31. */
  32. public function add(string $name, $path): RouteConfigurator
  33. {
  34. $parentConfigurator = $this instanceof CollectionConfigurator ? $this : ($this instanceof RouteConfigurator ? $this->parentConfigurator : null);
  35. $route = $this->createLocalizedRoute($this->collection, $name, $path, $this->name, $this->prefixes);
  36. return new RouteConfigurator($this->collection, $route, $this->name, $parentConfigurator, $this->prefixes);
  37. }
  38. public function alias(string $name, string $alias): AliasConfigurator
  39. {
  40. return new AliasConfigurator($this->collection->addAlias($name, $alias));
  41. }
  42. /**
  43. * Adds a route.
  44. *
  45. * @param string|array $path the path, or the localized paths of the route
  46. */
  47. public function __invoke(string $name, $path): RouteConfigurator
  48. {
  49. return $this->add($name, $path);
  50. }
  51. }