FileLoader.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. public function setCurrentDir(string $dir)
  35. {
  36. $this->currentDir = $dir;
  37. }
  38. /**
  39. * Returns the file locator used by this loader.
  40. *
  41. * @return FileLocatorInterface
  42. */
  43. public function getLocator()
  44. {
  45. return $this->locator;
  46. }
  47. /**
  48. * Imports a resource.
  49. *
  50. * @param mixed $resource A Resource
  51. * @param string|null $type The resource type or null if unknown
  52. * @param bool $ignoreErrors Whether to ignore import errors or not
  53. * @param string|null $sourceResource The original resource importing the new resource
  54. * @param string|string[]|null $exclude Glob patterns to exclude from the import
  55. *
  56. * @return mixed
  57. *
  58. * @throws LoaderLoadException
  59. * @throws FileLoaderImportCircularReferenceException
  60. * @throws FileLocatorFileNotFoundException
  61. */
  62. public function import($resource, string $type = null, bool $ignoreErrors = false, string $sourceResource = null, $exclude = null)
  63. {
  64. if (\is_string($resource) && \strlen($resource) !== ($i = strcspn($resource, '*?{[')) && false === strpos($resource, "\n")) {
  65. $excluded = [];
  66. foreach ((array) $exclude as $pattern) {
  67. foreach ($this->glob($pattern, true, $_, false, true) as $path => $info) {
  68. // normalize Windows slashes
  69. $excluded[str_replace('\\', '/', $path)] = true;
  70. }
  71. }
  72. $ret = [];
  73. $isSubpath = 0 !== $i && false !== strpos(substr($resource, 0, $i), '/');
  74. foreach ($this->glob($resource, false, $_, $ignoreErrors || !$isSubpath, false, $excluded) as $path => $info) {
  75. if (null !== $res = $this->doImport($path, 'glob' === $type ? null : $type, $ignoreErrors, $sourceResource)) {
  76. $ret[] = $res;
  77. }
  78. $isSubpath = true;
  79. }
  80. if ($isSubpath) {
  81. return isset($ret[1]) ? $ret : ($ret[0] ?? null);
  82. }
  83. }
  84. return $this->doImport($resource, $type, $ignoreErrors, $sourceResource);
  85. }
  86. /**
  87. * @internal
  88. */
  89. protected function glob(string $pattern, bool $recursive, &$resource = null, bool $ignoreErrors = false, bool $forExclusion = false, array $excluded = [])
  90. {
  91. if (\strlen($pattern) === $i = strcspn($pattern, '*?{[')) {
  92. $prefix = $pattern;
  93. $pattern = '';
  94. } elseif (0 === $i || false === strpos(substr($pattern, 0, $i), '/')) {
  95. $prefix = '.';
  96. $pattern = '/'.$pattern;
  97. } else {
  98. $prefix = \dirname(substr($pattern, 0, 1 + $i));
  99. $pattern = substr($pattern, \strlen($prefix));
  100. }
  101. try {
  102. $prefix = $this->locator->locate($prefix, $this->currentDir, true);
  103. } catch (FileLocatorFileNotFoundException $e) {
  104. if (!$ignoreErrors) {
  105. throw $e;
  106. }
  107. $resource = [];
  108. foreach ($e->getPaths() as $path) {
  109. $resource[] = new FileExistenceResource($path);
  110. }
  111. return;
  112. }
  113. $resource = new GlobResource($prefix, $pattern, $recursive, $forExclusion, $excluded);
  114. yield from $resource;
  115. }
  116. private function doImport($resource, string $type = null, bool $ignoreErrors = false, string $sourceResource = null)
  117. {
  118. try {
  119. $loader = $this->resolve($resource, $type);
  120. if ($loader instanceof self && null !== $this->currentDir) {
  121. $resource = $loader->getLocator()->locate($resource, $this->currentDir, false);
  122. }
  123. $resources = \is_array($resource) ? $resource : [$resource];
  124. for ($i = 0; $i < $resourcesCount = \count($resources); ++$i) {
  125. if (isset(self::$loading[$resources[$i]])) {
  126. if ($i == $resourcesCount - 1) {
  127. throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
  128. }
  129. } else {
  130. $resource = $resources[$i];
  131. break;
  132. }
  133. }
  134. self::$loading[$resource] = true;
  135. try {
  136. $ret = $loader->load($resource, $type);
  137. } finally {
  138. unset(self::$loading[$resource]);
  139. }
  140. return $ret;
  141. } catch (FileLoaderImportCircularReferenceException $e) {
  142. throw $e;
  143. } catch (\Exception $e) {
  144. if (!$ignoreErrors) {
  145. // prevent embedded imports from nesting multiple exceptions
  146. if ($e instanceof LoaderLoadException) {
  147. throw $e;
  148. }
  149. throw new LoaderLoadException($resource, $sourceResource, 0, $e, $type);
  150. }
  151. }
  152. return null;
  153. }
  154. }