AddTrait.php 1.5 KB

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