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

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.      * @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.      * @param string|string[]|null $exclude        Glob patterns to exclude from the import
  57.      *
  58.      * @return mixed
  59.      *
  60.      * @throws LoaderLoadException
  61.      * @throws FileLoaderImportCircularReferenceException
  62.      * @throws FileLocatorFileNotFoundException
  63.      */
  64.     public function import($resource$type null$ignoreErrors false$sourceResource null/*, $exclude = null*/)
  65.     {
  66.         if (\func_num_args() < && __CLASS__ !== static::class && !== strpos(static::class, 'Symfony\Component\\') && __CLASS__ !== (new \ReflectionMethod($this__FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface && !$this instanceof \Mockery\MockInterface) {
  67.             @trigger_error(sprintf('The "%s()" method will have a new "$exclude = null" argument in version 5.0, not defining it is deprecated since Symfony 4.4.'__METHOD__), \E_USER_DEPRECATED);
  68.         }
  69.         $exclude = \func_num_args() >= func_get_arg(4) : null;
  70.         if (\is_string($resource) && \strlen($resource) !== ($i strcspn($resource'*?{[')) && false === strpos($resource"\n")) {
  71.             $excluded = [];
  72.             foreach ((array) $exclude as $pattern) {
  73.                 foreach ($this->glob($patterntrue$_falsetrue) as $path => $info) {
  74.                     // normalize Windows slashes and remove trailing slashes
  75.                     $excluded[rtrim(str_replace('\\''/'$path), '/')] = true;
  76.                 }
  77.             }
  78.             $ret = [];
  79.             $isSubpath !== $i && false !== strpos(substr($resource0$i), '/');
  80.             foreach ($this->glob($resourcefalse$_$ignoreErrors || !$isSubpathfalse$excluded) as $path => $info) {
  81.                 if (null !== $res $this->doImport($path'glob' === $type null $type$ignoreErrors$sourceResource)) {
  82.                     $ret[] = $res;
  83.                 }
  84.                 $isSubpath true;
  85.             }
  86.             if ($isSubpath) {
  87.                 return isset($ret[1]) ? $ret : ($ret[0] ?? null);
  88.             }
  89.         }
  90.         return $this->doImport($resource$type$ignoreErrors$sourceResource);
  91.     }
  92.     /**
  93.      * @internal
  94.      */
  95.     protected function glob(string $patternbool $recursive, &$resource nullbool $ignoreErrors falsebool $forExclusion false, array $excluded = [])
  96.     {
  97.         if (\strlen($pattern) === $i strcspn($pattern'*?{[')) {
  98.             $prefix $pattern;
  99.             $pattern '';
  100.         } elseif (=== $i || false === strpos(substr($pattern0$i), '/')) {
  101.             $prefix '.';
  102.             $pattern '/'.$pattern;
  103.         } else {
  104.             $prefix = \dirname(substr($pattern0$i));
  105.             $pattern substr($pattern, \strlen($prefix));
  106.         }
  107.         try {
  108.             $prefix $this->locator->locate($prefix$this->currentDirtrue);
  109.         } catch (FileLocatorFileNotFoundException $e) {
  110.             if (!$ignoreErrors) {
  111.                 throw $e;
  112.             }
  113.             $resource = [];
  114.             foreach ($e->getPaths() as $path) {
  115.                 $resource[] = new FileExistenceResource($path);
  116.             }
  117.             return;
  118.         }
  119.         $resource = new GlobResource($prefix$pattern$recursive$forExclusion$excluded);
  120.         yield from $resource;
  121.     }
  122.     private function doImport($resourcestring $type nullbool $ignoreErrors false$sourceResource null)
  123.     {
  124.         try {
  125.             $loader $this->resolve($resource$type);
  126.             if ($loader instanceof self && null !== $this->currentDir) {
  127.                 $resource $loader->getLocator()->locate($resource$this->currentDirfalse);
  128.             }
  129.             $resources = \is_array($resource) ? $resource : [$resource];
  130.             for ($i 0$i $resourcesCount = \count($resources); ++$i) {
  131.                 if (isset(self::$loading[$resources[$i]])) {
  132.                     if ($i == $resourcesCount 1) {
  133.                         throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
  134.                     }
  135.                 } else {
  136.                     $resource $resources[$i];
  137.                     break;
  138.                 }
  139.             }
  140.             self::$loading[$resource] = true;
  141.             try {
  142.                 $ret $loader->load($resource$type);
  143.             } finally {
  144.                 unset(self::$loading[$resource]);
  145.             }
  146.             return $ret;
  147.         } catch (FileLoaderImportCircularReferenceException $e) {
  148.             throw $e;
  149.         } catch (\Exception $e) {
  150.             if (!$ignoreErrors) {
  151.                 // prevent embedded imports from nesting multiple exceptions
  152.                 if ($e instanceof LoaderLoadException) {
  153.                     throw $e;
  154.                 }
  155.                 throw new LoaderLoadException($resource$sourceResource0$e$type);
  156.             }
  157.         }
  158.         return null;
  159.     }
  160. }