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

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 $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($resourcestring $type nullbool $ignoreErrors falsestring $sourceResource null$exclude null)
  63.     {
  64.         if (\is_string($resource) && \strlen($resource) !== $i strcspn($resource'*?{[')) {
  65.             $excluded = [];
  66.             foreach ((array) $exclude as $pattern) {
  67.                 foreach ($this->glob($patterntrue$_falsetrue) as $path => $info) {
  68.                     // normalize Windows slashes
  69.                     $excluded[str_replace('\\''/'$path)] = true;
  70.                 }
  71.             }
  72.             $ret = [];
  73.             $isSubpath !== $i && false !== strpos(substr($resource0$i), '/');
  74.             foreach ($this->glob($resourcefalse$_$ignoreErrors || !$isSubpathfalse$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 : (isset($ret[0]) ? $ret[0] : null);
  82.             }
  83.         }
  84.         return $this->doImport($resource$type$ignoreErrors$sourceResource);
  85.     }
  86.     /**
  87.      * @internal
  88.      */
  89.     protected function glob(string $patternbool $recursive, &$resource nullbool $ignoreErrors falsebool $forExclusion false, array $excluded = [])
  90.     {
  91.         if (\strlen($pattern) === $i strcspn($pattern'*?{[')) {
  92.             $prefix $pattern;
  93.             $pattern '';
  94.         } elseif (=== $i || false === strpos(substr($pattern0$i), '/')) {
  95.             $prefix '.';
  96.             $pattern '/'.$pattern;
  97.         } else {
  98.             $prefix = \dirname(substr($pattern0$i));
  99.             $pattern substr($pattern, \strlen($prefix));
  100.         }
  101.         try {
  102.             $prefix $this->locator->locate($prefix$this->currentDirtrue);
  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($resourcestring $type nullbool $ignoreErrors falsestring $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->currentDirfalse);
  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$sourceResourcenull$e$type);
  150.             }
  151.         }
  152.         return null;
  153.     }
  154. }