|
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 static protected $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 public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null) |
|
62 { |
|
63 try { |
|
64 $loader = $this->resolve($resource, $type); |
|
65 |
|
66 if ($loader instanceof FileLoader && null !== $this->currentDir) { |
|
67 $resource = $this->locator->locate($resource, $this->currentDir); |
|
68 } |
|
69 |
|
70 if (isset(self::$loading[$resource])) { |
|
71 throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading)); |
|
72 } |
|
73 self::$loading[$resource] = true; |
|
74 |
|
75 $ret = $loader->load($resource); |
|
76 |
|
77 unset(self::$loading[$resource]); |
|
78 |
|
79 return $ret; |
|
80 } catch (FileLoaderImportCircularReferenceException $e) { |
|
81 throw $e; |
|
82 } catch (\Exception $e) { |
|
83 if (!$ignoreErrors) { |
|
84 // prevent embedded imports from nesting multiple exceptions |
|
85 if ($e instanceof FileLoaderLoadException) { |
|
86 throw $e; |
|
87 } |
|
88 |
|
89 throw new FileLoaderLoadException($resource, $sourceResource, null, $e); |
|
90 } |
|
91 } |
|
92 } |
|
93 } |