RoutingConfigurator.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. * @return ImportConfigurator
  31. */
  32. final public function import($resource, $type = null, $ignoreErrors = false)
  33. {
  34. $this->loader->setCurrentDir(\dirname($this->path));
  35. $imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file);
  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. /**
  46. * @return CollectionConfigurator
  47. */
  48. final public function collection($name = '')
  49. {
  50. return new CollectionConfigurator($this->collection, $name);
  51. }
  52. }