vendor/symfony/symfony/src/Symfony/Component/Config/Loader/FileLoader.php line 153

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\FileLoaderLoadException;
  13. use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
  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.      *
  57.      * @return mixed
  58.      *
  59.      * @throws FileLoaderLoadException
  60.      * @throws FileLoaderImportCircularReferenceException
  61.      * @throws FileLocatorFileNotFoundException
  62.      */
  63.     public function import($resource$type null$ignoreErrors false$sourceResource null)
  64.     {
  65.         if (\is_string($resource) && \strlen($resource) !== $i strcspn($resource'*?{[')) {
  66.             $ret = [];
  67.             $isSubpath !== $i && false !== strpos(substr($resource0$i), '/');
  68.             foreach ($this->glob($resourcefalse$_$ignoreErrors || !$isSubpath) as $path => $info) {
  69.                 if (null !== $res $this->doImport($path'glob' === $type null $type$ignoreErrors$sourceResource)) {
  70.                     $ret[] = $res;
  71.                 }
  72.                 $isSubpath true;
  73.             }
  74.             if ($isSubpath) {
  75.                 return isset($ret[1]) ? $ret : (isset($ret[0]) ? $ret[0] : null);
  76.             }
  77.         }
  78.         return $this->doImport($resource$type$ignoreErrors$sourceResource);
  79.     }
  80.     /**
  81.      * @internal
  82.      */
  83.     protected function glob($pattern$recursive, &$resource null$ignoreErrors false)
  84.     {
  85.         if (\strlen($pattern) === $i strcspn($pattern'*?{[')) {
  86.             $prefix $pattern;
  87.             $pattern '';
  88.         } elseif (=== $i || false === strpos(substr($pattern0$i), '/')) {
  89.             $prefix '.';
  90.             $pattern '/'.$pattern;
  91.         } else {
  92.             $prefix = \dirname(substr($pattern0$i));
  93.             $pattern substr($pattern, \strlen($prefix));
  94.         }
  95.         try {
  96.             $prefix $this->locator->locate($prefix$this->currentDirtrue);
  97.         } catch (FileLocatorFileNotFoundException $e) {
  98.             if (!$ignoreErrors) {
  99.                 throw $e;
  100.             }
  101.             $resource = [];
  102.             foreach ($e->getPaths() as $path) {
  103.                 $resource[] = new FileExistenceResource($path);
  104.             }
  105.             return;
  106.         }
  107.         $resource = new GlobResource($prefix$pattern$recursive);
  108.         foreach ($resource as $path => $info) {
  109.             yield $path => $info;
  110.         }
  111.     }
  112.     private function doImport($resource$type null$ignoreErrors false$sourceResource null)
  113.     {
  114.         try {
  115.             $loader $this->resolve($resource$type);
  116.             if ($loader instanceof self && null !== $this->currentDir) {
  117.                 $resource $loader->getLocator()->locate($resource$this->currentDirfalse);
  118.             }
  119.             $resources = \is_array($resource) ? $resource : [$resource];
  120.             for ($i 0$i $resourcesCount = \count($resources); ++$i) {
  121.                 if (isset(self::$loading[$resources[$i]])) {
  122.                     if ($i == $resourcesCount 1) {
  123.                         throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
  124.                     }
  125.                 } else {
  126.                     $resource $resources[$i];
  127.                     break;
  128.                 }
  129.             }
  130.             self::$loading[$resource] = true;
  131.             try {
  132.                 $ret $loader->load($resource$type);
  133.             } finally {
  134.                 unset(self::$loading[$resource]);
  135.             }
  136.             return $ret;
  137.         } catch (FileLoaderImportCircularReferenceException $e) {
  138.             throw $e;
  139.         } catch (\Exception $e) {
  140.             if (!$ignoreErrors) {
  141.                 // prevent embedded imports from nesting multiple exceptions
  142.                 if ($e instanceof FileLoaderLoadException) {
  143.                     throw $e;
  144.                 }
  145.                 throw new FileLoaderLoadException($resource$sourceResourcenull$e$type);
  146.             }
  147.         }
  148.         return null;
  149.     }
  150. }