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

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