RoutingConfigurator.php 1.9 KB

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