Version 1
[yaffs-website] / vendor / symfony / config / Loader / FileLoader.php
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     /**
26      * @var array
27      */
28     protected static $loading = array();
29
30     /**
31      * @var FileLocatorInterface
32      */
33     protected $locator;
34
35     private $currentDir;
36
37     /**
38      * Constructor.
39      *
40      * @param FileLocatorInterface $locator A FileLocatorInterface instance
41      */
42     public function __construct(FileLocatorInterface $locator)
43     {
44         $this->locator = $locator;
45     }
46
47     /**
48      * Sets the current directory.
49      *
50      * @param string $dir
51      */
52     public function setCurrentDir($dir)
53     {
54         $this->currentDir = $dir;
55     }
56
57     /**
58      * Returns the file locator used by this loader.
59      *
60      * @return FileLocatorInterface
61      */
62     public function getLocator()
63     {
64         return $this->locator;
65     }
66
67     /**
68      * Imports a resource.
69      *
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
74      *
75      * @return mixed
76      *
77      * @throws FileLoaderLoadException
78      * @throws FileLoaderImportCircularReferenceException
79      */
80     public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
81     {
82         try {
83             $loader = $this->resolve($resource, $type);
84
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;
93                 }
94
95                 $resource = $locator->locate($resource, $this->currentDir, false);
96             }
97
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));
103                     }
104                 } else {
105                     $resource = $resources[$i];
106                     break;
107                 }
108             }
109             self::$loading[$resource] = true;
110
111             try {
112                 $ret = $loader->load($resource, $type);
113             } catch (\Exception $e) {
114                 unset(self::$loading[$resource]);
115                 throw $e;
116             } catch (\Throwable $e) {
117                 unset(self::$loading[$resource]);
118                 throw $e;
119             }
120
121             unset(self::$loading[$resource]);
122
123             return $ret;
124         } catch (FileLoaderImportCircularReferenceException $e) {
125             throw $e;
126         } catch (\Exception $e) {
127             if (!$ignoreErrors) {
128                 // prevent embedded imports from nesting multiple exceptions
129                 if ($e instanceof FileLoaderLoadException) {
130                     throw $e;
131                 }
132
133                 throw new FileLoaderLoadException($resource, $sourceResource, null, $e);
134             }
135         }
136     }
137 }