Security update for Core, with self-updated composer
[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                 $resource = $loader->getLocator()->locate($resource, $this->currentDir, false);
87             }
88
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));
94                     }
95                 } else {
96                     $resource = $resources[$i];
97                     break;
98                 }
99             }
100             self::$loading[$resource] = true;
101
102             try {
103                 $ret = $loader->load($resource, $type);
104             } finally {
105                 unset(self::$loading[$resource]);
106             }
107
108             return $ret;
109         } catch (FileLoaderImportCircularReferenceException $e) {
110             throw $e;
111         } catch (\Exception $e) {
112             if (!$ignoreErrors) {
113                 // prevent embedded imports from nesting multiple exceptions
114                 if ($e instanceof FileLoaderLoadException) {
115                     throw $e;
116                 }
117
118                 throw new FileLoaderLoadException($resource, $sourceResource, null, $e);
119             }
120         }
121     }
122 }