DirectoryLoader.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\DependencyInjection\Loader;
  11. /**
  12. * DirectoryLoader is a recursive loader to go through directories.
  13. *
  14. * @author Sebastien Lavoie <seb@wemakecustom.com>
  15. */
  16. class DirectoryLoader extends FileLoader
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function load($file, string $type = null)
  22. {
  23. $file = rtrim($file, '/');
  24. $path = $this->locator->locate($file);
  25. $this->container->fileExists($path, false);
  26. foreach (scandir($path) as $dir) {
  27. if ('.' !== $dir[0]) {
  28. if (is_dir($path.'/'.$dir)) {
  29. $dir .= '/'; // append / to allow recursion
  30. }
  31. $this->setCurrentDir($path);
  32. $this->import($dir, null, false, $path);
  33. }
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function supports($resource, string $type = null)
  40. {
  41. if ('directory' === $type) {
  42. return true;
  43. }
  44. return null === $type && \is_string($resource) && '/' === substr($resource, -1);
  45. }
  46. }