RoutingConfigurator.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. private $loader;
  20. private $path;
  21. private $file;
  22. private $env;
  23. public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file, string $env = null)
  24. {
  25. $this->collection = $collection;
  26. $this->loader = $loader;
  27. $this->path = $path;
  28. $this->file = $file;
  29. $this->env = $env;
  30. }
  31. /**
  32. * @param string|string[]|null $exclude Glob patterns to exclude from the import
  33. */
  34. final public function import($resource, string $type = null, bool $ignoreErrors = false, $exclude = null): ImportConfigurator
  35. {
  36. $this->loader->setCurrentDir(\dirname($this->path));
  37. $imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude) ?: [];
  38. if (!\is_array($imported)) {
  39. return new ImportConfigurator($this->collection, $imported);
  40. }
  41. $mergedCollection = new RouteCollection();
  42. foreach ($imported as $subCollection) {
  43. $mergedCollection->addCollection($subCollection);
  44. }
  45. return new ImportConfigurator($this->collection, $mergedCollection);
  46. }
  47. final public function collection(string $name = ''): CollectionConfigurator
  48. {
  49. return new CollectionConfigurator($this->collection, $name);
  50. }
  51. /**
  52. * Get the current environment to be able to write conditional configuration.
  53. */
  54. final public function env(): ?string
  55. {
  56. return $this->env;
  57. }
  58. /**
  59. * @return static
  60. */
  61. final public function withPath(string $path): self
  62. {
  63. $clone = clone $this;
  64. $clone->path = $clone->file = $path;
  65. return $clone;
  66. }
  67. }