RoutingConfigurator.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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;
  11. use Symfony\Component\Routing\Loader\PhpFileLoader;
  12. use Symfony\Component\Routing\RouteCollection;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class RoutingConfigurator
  17. {
  18. use Traits\AddTrait;
  19. public function __construct(
  20. RouteCollection $collection,
  21. private PhpFileLoader $loader,
  22. private string $path,
  23. private string $file,
  24. private ?string $env = null,
  25. ) {
  26. $this->collection = $collection;
  27. }
  28. /**
  29. * @param string|string[]|null $exclude Glob patterns to exclude from the import
  30. */
  31. final public function import(string|array $resource, ?string $type = null, bool $ignoreErrors = false, string|array|null $exclude = null): ImportConfigurator
  32. {
  33. $this->loader->setCurrentDir(\dirname($this->path));
  34. $imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude) ?: [];
  35. if (!\is_array($imported)) {
  36. return new ImportConfigurator($this->collection, $imported);
  37. }
  38. $mergedCollection = new RouteCollection();
  39. foreach ($imported as $subCollection) {
  40. $mergedCollection->addCollection($subCollection);
  41. }
  42. return new ImportConfigurator($this->collection, $mergedCollection);
  43. }
  44. final public function collection(string $name = ''): CollectionConfigurator
  45. {
  46. return new CollectionConfigurator($this->collection, $name);
  47. }
  48. /**
  49. * Get the current environment to be able to write conditional configuration.
  50. */
  51. final public function env(): ?string
  52. {
  53. return $this->env;
  54. }
  55. final public function withPath(string $path): static
  56. {
  57. $clone = clone $this;
  58. $clone->path = $clone->file = $path;
  59. return $clone;
  60. }
  61. }