composer package updates
[openemr.git] / vendor / symfony / config / Loader / FileLoader.php
blob5ea8a8984bc7e8ee29ddaaa387fb0b4e8910ee10
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Loader;
14 use Symfony\Component\Config\FileLocatorInterface;
15 use Symfony\Component\Config\Exception\FileLoaderLoadException;
16 use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException;
18 /**
19 * FileLoader is the abstract class used by all built-in loaders that are file based.
21 * @author Fabien Potencier <fabien@symfony.com>
23 abstract class FileLoader extends Loader
25 protected static $loading = array();
27 protected $locator;
29 private $currentDir;
31 public function __construct(FileLocatorInterface $locator)
33 $this->locator = $locator;
36 /**
37 * Sets the current directory.
39 * @param string $dir
41 public function setCurrentDir($dir)
43 $this->currentDir = $dir;
46 /**
47 * Returns the file locator used by this loader.
49 * @return FileLocatorInterface
51 public function getLocator()
53 return $this->locator;
56 /**
57 * Imports a resource.
59 * @param mixed $resource A Resource
60 * @param string|null $type The resource type or null if unknown
61 * @param bool $ignoreErrors Whether to ignore import errors or not
62 * @param string|null $sourceResource The original resource importing the new resource
64 * @return mixed
66 * @throws FileLoaderLoadException
67 * @throws FileLoaderImportCircularReferenceException
69 public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
71 try {
72 $loader = $this->resolve($resource, $type);
74 if ($loader instanceof self && null !== $this->currentDir) {
75 // we fallback to the current locator to keep BC
76 // as some some loaders do not call the parent __construct()
77 // @deprecated should be removed in 3.0
78 $locator = $loader->getLocator();
79 if (null === $locator) {
80 @trigger_error('Not calling the parent constructor in '.get_class($loader).' which extends '.__CLASS__.' is deprecated since Symfony 2.7 and will not be supported anymore in 3.0.', E_USER_DEPRECATED);
81 $locator = $this->locator;
84 $resource = $locator->locate($resource, $this->currentDir, false);
87 $resources = is_array($resource) ? $resource : array($resource);
88 for ($i = 0; $i < $resourcesCount = count($resources); ++$i) {
89 if (isset(self::$loading[$resources[$i]])) {
90 if ($i == $resourcesCount - 1) {
91 throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
93 } else {
94 $resource = $resources[$i];
95 break;
98 self::$loading[$resource] = true;
100 try {
101 $ret = $loader->load($resource, $type);
102 } catch (\Exception $e) {
103 unset(self::$loading[$resource]);
104 throw $e;
105 } catch (\Throwable $e) {
106 unset(self::$loading[$resource]);
107 throw $e;
110 unset(self::$loading[$resource]);
112 return $ret;
113 } catch (FileLoaderImportCircularReferenceException $e) {
114 throw $e;
115 } catch (\Exception $e) {
116 if (!$ignoreErrors) {
117 // prevent embedded imports from nesting multiple exceptions
118 if ($e instanceof FileLoaderLoadException) {
119 throw $e;
122 throw new FileLoaderLoadException($resource, $sourceResource, null, $e);