1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\Config\Loader;
13:
14: use Symfony\Component\Config\FileLocatorInterface;
15: use Symfony\Component\Config\Exception\FileLoaderLoadException;
16: use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException;
17:
18: /**
19: * FileLoader is the abstract class used by all built-in loaders that are file based.
20: *
21: * @author Fabien Potencier <fabien@symfony.com>
22: */
23: abstract class FileLoader extends Loader
24: {
25: protected static $loading = array();
26:
27: protected $locator;
28:
29: private $currentDir;
30:
31: /**
32: * Constructor.
33: *
34: * @param FileLocatorInterface $locator A FileLocatorInterface instance
35: */
36: public function __construct(FileLocatorInterface $locator)
37: {
38: $this->locator = $locator;
39: }
40:
41: public function setCurrentDir($dir)
42: {
43: $this->currentDir = $dir;
44: }
45:
46: public function getLocator()
47: {
48: return $this->locator;
49: }
50:
51: /**
52: * Imports a resource.
53: *
54: * @param mixed $resource A Resource
55: * @param string $type The resource type
56: * @param Boolean $ignoreErrors Whether to ignore import errors or not
57: * @param string $sourceResource The original resource importing the new resource
58: *
59: * @return mixed
60: *
61: * @throws FileLoaderLoadException
62: * @throws FileLoaderImportCircularReferenceException
63: */
64: public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
65: {
66: try {
67: $loader = $this->resolve($resource, $type);
68:
69: if ($loader instanceof FileLoader && null !== $this->currentDir) {
70: $resource = $this->locator->locate($resource, $this->currentDir);
71: }
72:
73: if (isset(self::$loading[$resource])) {
74: throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
75: }
76: self::$loading[$resource] = true;
77:
78: $ret = $loader->load($resource, $type);
79:
80: unset(self::$loading[$resource]);
81:
82: return $ret;
83: } catch (FileLoaderImportCircularReferenceException $e) {
84: throw $e;
85: } catch (\Exception $e) {
86: if (!$ignoreErrors) {
87: // prevent embedded imports from nesting multiple exceptions
88: if ($e instanceof FileLoaderLoadException) {
89: throw $e;
90: }
91:
92: throw new FileLoaderLoadException($resource, $sourceResource, null, $e);
93: }
94: }
95: }
96: }
97: