No Description

RoutingConfigurator.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }