FileLoader.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Config\Loader;
  11. use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException;
  12. use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
  13. use Symfony\Component\Config\Exception\LoaderLoadException;
  14. use Symfony\Component\Config\FileLocatorInterface;
  15. use Symfony\Component\Config\Resource\FileExistenceResource;
  16. use Symfony\Component\Config\Resource\GlobResource;
  17. /**
  18. * FileLoader is the abstract class used by all built-in loaders that are file based.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. abstract class FileLoader extends Loader
  23. {
  24. protected static $loading = [];
  25. protected $locator;
  26. private $currentDir;
  27. public function __construct(FileLocatorInterface $locator)
  28. {
  29. $this->locator = $locator;
  30. }
  31. /**
  32. * Sets the current directory.
  33. *
  34. * @param string $dir
  35. */
  36. public function setCurrentDir($dir)
  37. {
  38. $this->currentDir = $dir;
  39. }
  40. /**
  41. * Returns the file locator used by this loader.
  42. *
  43. * @return FileLocatorInterface
  44. */
  45. public function getLocator()
  46. {
  47. return $this->locator;
  48. }
  49. /**
  50. * Imports a resource.
  51. *
  52. * @param mixed $resource A Resource
  53. * @param string|null $type The resource type or null if unknown
  54. * @param bool $ignoreErrors Whether to ignore import errors or not
  55. * @param string|null $sourceResource The original resource importing the new resource
  56. *
  57. * @return mixed
  58. *
  59. * @throws LoaderLoadException
  60. * @throws FileLoaderImportCircularReferenceException
  61. * @throws FileLocatorFileNotFoundException
  62. */
  63. public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
  64. {
  65. if (\is_string($resource) && \strlen($resource) !== $i = strcspn($resource, '*?{[')) {
  66. $ret = [];
  67. $isSubpath = 0 !== $i && false !== strpos(substr($resource, 0, $i), '/');
  68. foreach ($this->glob($resource, false, $_, $ignoreErrors || !$isSubpath) as $path => $info) {
  69. if (null !== $res = $this->doImport($path, $type, $ignoreErrors, $sourceResource)) {
  70. $ret[] = $res;
  71. }
  72. $isSubpath = true;
  73. }
  74. if ($isSubpath) {
  75. return isset($ret[1]) ? $ret : (isset($ret[0]) ? $ret[0] : null);
  76. }
  77. }
  78. return $this->doImport($resource, $type, $ignoreErrors, $sourceResource);
  79. }
  80. /**
  81. * @internal
  82. */
  83. protected function glob(string $pattern, bool $recursive, &$resource = null, bool $ignoreErrors = false, bool $forExclusion = false, array $excluded = [])
  84. {
  85. if (\strlen($pattern) === $i = strcspn($pattern, '*?{[')) {
  86. $prefix = $pattern;
  87. $pattern = '';
  88. } elseif (0 === $i || false === strpos(substr($pattern, 0, $i), '/')) {
  89. $prefix = '.';
  90. $pattern = '/'.$pattern;
  91. } else {
  92. $prefix = \dirname(substr($pattern, 0, 1 + $i));
  93. $pattern = substr($pattern, \strlen($prefix));
  94. }
  95. try {
  96. $prefix = $this->locator->locate($prefix, $this->currentDir, true);
  97. } catch (FileLocatorFileNotFoundException $e) {
  98. if (!$ignoreErrors) {
  99. throw $e;
  100. }
  101. $resource = [];
  102. foreach ($e->getPaths() as $path) {
  103. $resource[] = new FileExistenceResource($path);
  104. }
  105. return;
  106. }
  107. $resource = new GlobResource($prefix, $pattern, $recursive, $forExclusion, $excluded);
  108. yield from $resource;
  109. }
  110. private function doImport($resource, $type = null, bool $ignoreErrors = false, $sourceResource = null)
  111. {
  112. try {
  113. $loader = $this->resolve($resource, $type);
  114. if ($loader instanceof self && null !== $this->currentDir) {
  115. $resource = $loader->getLocator()->locate($resource, $this->currentDir, false);
  116. }
  117. $resources = \is_array($resource) ? $resource : [$resource];
  118. for ($i = 0; $i < $resourcesCount = \count($resources); ++$i) {
  119. if (isset(self::$loading[$resources[$i]])) {
  120. if ($i == $resourcesCount - 1) {
  121. throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
  122. }
  123. } else {
  124. $resource = $resources[$i];
  125. break;
  126. }
  127. }
  128. self::$loading[$resource] = true;
  129. try {
  130. $ret = $loader->load($resource, $type);
  131. } finally {
  132. unset(self::$loading[$resource]);
  133. }
  134. return $ret;
  135. } catch (FileLoaderImportCircularReferenceException $e) {
  136. throw $e;
  137. } catch (\Exception $e) {
  138. if (!$ignoreErrors) {
  139. // prevent embedded imports from nesting multiple exceptions
  140. if ($e instanceof LoaderLoadException) {
  141. throw $e;
  142. }
  143. throw new LoaderLoadException($resource, $sourceResource, null, $e, $type);
  144. }
  145. }
  146. }
  147. }