DirectoryLoader.php 1.3 KB

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