Security update to Drupal 8.4.6
[yaffs-website] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / Driver / FileDriver.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\Common\Persistence\Mapping\Driver;
21
22 use Doctrine\Common\Persistence\Mapping\MappingException;
23
24 /**
25  * Base driver for file-based metadata drivers.
26  *
27  * A file driver operates in a mode where it loads the mapping files of individual
28  * classes on demand. This requires the user to adhere to the convention of 1 mapping
29  * file per class and the file names of the mapping files must correspond to the full
30  * class name, including namespace, with the namespace delimiters '\', replaced by dots '.'.
31  *
32  * @link   www.doctrine-project.org
33  * @since  2.2
34  * @author Benjamin Eberlei <kontakt@beberlei.de>
35  * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
36  * @author Jonathan H. Wage <jonwage@gmail.com>
37  * @author Roman Borschel <roman@code-factory.org>
38  */
39 abstract class FileDriver implements MappingDriver
40 {
41     /**
42      * @var FileLocator
43      */
44     protected $locator;
45
46     /**
47      * @var array|null
48      */
49     protected $classCache;
50
51     /**
52      * @var string|null
53      */
54     protected $globalBasename;
55
56     /**
57      * Initializes a new FileDriver that looks in the given path(s) for mapping
58      * documents and operates in the specified operating mode.
59      *
60      * @param string|array|FileLocator $locator       A FileLocator or one/multiple paths
61      *                                                where mapping documents can be found.
62      * @param string|null              $fileExtension
63      */
64     public function __construct($locator, $fileExtension = null)
65     {
66         if ($locator instanceof FileLocator) {
67             $this->locator = $locator;
68         } else {
69             $this->locator = new DefaultFileLocator((array)$locator, $fileExtension);
70         }
71     }
72
73     /**
74      * Sets the global basename.
75      *
76      * @param string $file
77      *
78      * @return void
79      */
80     public function setGlobalBasename($file)
81     {
82         $this->globalBasename = $file;
83     }
84
85     /**
86      * Retrieves the global basename.
87      *
88      * @return string|null
89      */
90     public function getGlobalBasename()
91     {
92         return $this->globalBasename;
93     }
94
95     /**
96      * Gets the element of schema meta data for the class from the mapping file.
97      * This will lazily load the mapping file if it is not loaded yet.
98      *
99      * @param string $className
100      *
101      * @return array The element of schema meta data.
102      *
103      * @throws MappingException
104      */
105     public function getElement($className)
106     {
107         if ($this->classCache === null) {
108             $this->initialize();
109         }
110
111         if (isset($this->classCache[$className])) {
112             return $this->classCache[$className];
113         }
114
115         $result = $this->loadMappingFile($this->locator->findMappingFile($className));
116         if (!isset($result[$className])) {
117             throw MappingException::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->locator->getFileExtension());
118         }
119
120         $this->classCache[$className] = $result[$className];
121
122         return $result[$className];
123     }
124
125     /**
126      * {@inheritDoc}
127      */
128     public function isTransient($className)
129     {
130         if ($this->classCache === null) {
131             $this->initialize();
132         }
133
134         if (isset($this->classCache[$className])) {
135             return false;
136         }
137
138         return !$this->locator->fileExists($className);
139     }
140
141     /**
142      * {@inheritDoc}
143      */
144     public function getAllClassNames()
145     {
146         if ($this->classCache === null) {
147             $this->initialize();
148         }
149
150         if (! $this->classCache) {
151             return (array) $this->locator->getAllClassNames($this->globalBasename);
152         }
153
154         return array_merge(
155             array_keys($this->classCache),
156             (array) $this->locator->getAllClassNames($this->globalBasename)
157         );
158     }
159
160     /**
161      * Loads a mapping file with the given name and returns a map
162      * from class/entity names to their corresponding file driver elements.
163      *
164      * @param string $file The mapping file to load.
165      *
166      * @return array
167      */
168     abstract protected function loadMappingFile($file);
169
170     /**
171      * Initializes the class cache from all the global files.
172      *
173      * Using this feature adds a substantial performance hit to file drivers as
174      * more metadata has to be loaded into memory than might actually be
175      * necessary. This may not be relevant to scenarios where caching of
176      * metadata is in place, however hits very hard in scenarios where no
177      * caching is used.
178      *
179      * @return void
180      */
181     protected function initialize()
182     {
183         $this->classCache = [];
184         if (null !== $this->globalBasename) {
185             foreach ($this->locator->getPaths() as $path) {
186                 $file = $path.'/'.$this->globalBasename.$this->locator->getFileExtension();
187                 if (is_file($file)) {
188                     $this->classCache = array_merge(
189                         $this->classCache,
190                         $this->loadMappingFile($file)
191                     );
192                 }
193             }
194         }
195     }
196
197     /**
198      * Retrieves the locator used to discover mapping files by className.
199      *
200      * @return FileLocator
201      */
202     public function getLocator()
203     {
204         return $this->locator;
205     }
206
207     /**
208      * Sets the locator used to discover mapping files by className.
209      *
210      * @param FileLocator $locator
211      */
212     public function setLocator(FileLocator $locator)
213     {
214         $this->locator = $locator;
215     }
216 }