vendor/symfony/config/Loader/FileLoader.php line 94

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