DirectoryLoader.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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;
  11. use Symfony\Component\Config\Loader\FileLoader;
  12. use Symfony\Component\Config\Resource\DirectoryResource;
  13. use Symfony\Component\Routing\RouteCollection;
  14. class DirectoryLoader extends FileLoader
  15. {
  16. public function load(mixed $file, ?string $type = null): mixed
  17. {
  18. $path = $this->locator->locate($file);
  19. $collection = new RouteCollection();
  20. $collection->addResource(new DirectoryResource($path));
  21. foreach (scandir($path) as $dir) {
  22. if ('.' !== $dir[0]) {
  23. $this->setCurrentDir($path);
  24. $subPath = $path.'/'.$dir;
  25. $subType = null;
  26. if (is_dir($subPath)) {
  27. $subPath .= '/';
  28. $subType = 'directory';
  29. }
  30. $subCollection = $this->import($subPath, $subType, false, $path);
  31. $collection->addCollection($subCollection);
  32. }
  33. }
  34. return $collection;
  35. }
  36. public function supports(mixed $resource, ?string $type = null): bool
  37. {
  38. // only when type is forced to directory, not to conflict with AttributeLoader
  39. return 'directory' === $type;
  40. }
  41. }