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
25 protected static $loading = array();
31 public function __construct(FileLocatorInterface
$locator)
33 $this->locator
= $locator;
37 * Sets the current directory.
41 public function setCurrentDir($dir)
43 $this->currentDir
= $dir;
47 * Returns the file locator used by this loader.
49 * @return FileLocatorInterface
51 public function getLocator()
53 return $this->locator
;
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
66 * @throws FileLoaderLoadException
67 * @throws FileLoaderImportCircularReferenceException
69 public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
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));
94 $resource = $resources[$i];
98 self
::$loading[$resource] = true;
101 $ret = $loader->load($resource, $type);
102 } catch (\Exception
$e) {
103 unset(self
::$loading[$resource]);
105 } catch (\Throwable
$e) {
106 unset(self
::$loading[$resource]);
110 unset(self
::$loading[$resource]);
113 } catch (FileLoaderImportCircularReferenceException
$e) {
115 } catch (\Exception
$e) {
116 if (!$ignoreErrors) {
117 // prevent embedded imports from nesting multiple exceptions
118 if ($e instanceof FileLoaderLoadException
) {
122 throw new FileLoaderLoadException($resource, $sourceResource, null, $e);