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 $resource = $loader->getLocator()->locate($resource, $this->currentDir, false);
89 $resources = is_array($resource) ? $resource : array($resource);
90 for ($i = 0; $i < $resourcesCount = count($resources); ++$i) {
91 if (isset(self::$loading[$resources[$i]])) {
92 if ($i == $resourcesCount - 1) {
93 throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
96 $resource = $resources[$i];
100 self::$loading[$resource] = true;
103 $ret = $loader->load($resource, $type);
105 unset(self::$loading[$resource]);
109 } catch (FileLoaderImportCircularReferenceException $e) {
111 } catch (\Exception $e) {
112 if (!$ignoreErrors) {
113 // prevent embedded imports from nesting multiple exceptions
114 if ($e instanceof FileLoaderLoadException) {
118 throw new FileLoaderLoadException($resource, $sourceResource, null, $e);