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;
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
28 protected static $loading = array();
31 * @var FileLocatorInterface
40 * @param FileLocatorInterface $locator A FileLocatorInterface instance
42 public function __construct(FileLocatorInterface $locator)
44 $this->locator = $locator;
48 * Sets the current directory.
52 public function setCurrentDir($dir)
54 $this->currentDir = $dir;
58 * Returns the file locator used by this loader.
60 * @return FileLocatorInterface
62 public function getLocator()
64 return $this->locator;
70 * @param mixed $resource A Resource
71 * @param string|null $type The resource type or null if unknown
72 * @param bool $ignoreErrors Whether to ignore import errors or not
73 * @param string|null $sourceResource The original resource importing the new resource
77 * @throws FileLoaderLoadException
78 * @throws FileLoaderImportCircularReferenceException
80 public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
83 $loader = $this->resolve($resource, $type);
85 if ($loader instanceof self && null !== $this->currentDir) {
86 // we fallback to the current locator to keep BC
87 // as some some loaders do not call the parent __construct()
88 // @deprecated should be removed in 3.0
89 $locator = $loader->getLocator();
90 if (null === $locator) {
91 @trigger_error('Not calling the parent constructor in '.get_class($loader).' which extends '.__CLASS__.' is deprecated since version 2.7 and will not be supported anymore in 3.0.', E_USER_DEPRECATED);
92 $locator = $this->locator;
95 $resource = $locator->locate($resource, $this->currentDir, false);
98 $resources = is_array($resource) ? $resource : array($resource);
99 for ($i = 0; $i < $resourcesCount = count($resources); ++$i) {
100 if (isset(self::$loading[$resources[$i]])) {
101 if ($i == $resourcesCount - 1) {
102 throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
105 $resource = $resources[$i];
109 self::$loading[$resource] = true;
112 $ret = $loader->load($resource, $type);
113 } catch (\Exception $e) {
114 unset(self::$loading[$resource]);
116 } catch (\Throwable $e) {
117 unset(self::$loading[$resource]);
121 unset(self::$loading[$resource]);
124 } catch (FileLoaderImportCircularReferenceException $e) {
126 } catch (\Exception $e) {
127 if (!$ignoreErrors) {
128 // prevent embedded imports from nesting multiple exceptions
129 if ($e instanceof FileLoaderLoadException) {
133 throw new FileLoaderLoadException($resource, $sourceResource, null, $e);