805c2a1f1cbcf7bc862d5622cd9570bff801f9f9
[yaffs-website] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / AbstractClassMetadataFactory.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;
21
22 use Doctrine\Common\Cache\Cache;
23 use Doctrine\Common\Util\ClassUtils;
24 use ReflectionException;
25
26 /**
27  * The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
28  * metadata mapping informations of a class which describes how a class should be mapped
29  * to a relational database.
30  *
31  * This class was abstracted from the ORM ClassMetadataFactory.
32  *
33  * @since  2.2
34  * @author Benjamin Eberlei <kontakt@beberlei.de>
35  * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
36  * @author Jonathan Wage <jonwage@gmail.com>
37  * @author Roman Borschel <roman@code-factory.org>
38  */
39 abstract class AbstractClassMetadataFactory implements ClassMetadataFactory
40 {
41     /**
42      * Salt used by specific Object Manager implementation.
43      *
44      * @var string
45      */
46     protected $cacheSalt = '$CLASSMETADATA';
47
48     /**
49      * @var \Doctrine\Common\Cache\Cache|null
50      */
51     private $cacheDriver;
52
53     /**
54      * @var ClassMetadata[]
55      */
56     private $loadedMetadata = [];
57
58     /**
59      * @var bool
60      */
61     protected $initialized = false;
62
63     /**
64      * @var ReflectionService|null
65      */
66     private $reflectionService = null;
67
68     /**
69      * Sets the cache driver used by the factory to cache ClassMetadata instances.
70      *
71      * @param \Doctrine\Common\Cache\Cache $cacheDriver
72      *
73      * @return void
74      */
75     public function setCacheDriver(Cache $cacheDriver = null)
76     {
77         $this->cacheDriver = $cacheDriver;
78     }
79
80     /**
81      * Gets the cache driver used by the factory to cache ClassMetadata instances.
82      *
83      * @return \Doctrine\Common\Cache\Cache|null
84      */
85     public function getCacheDriver()
86     {
87         return $this->cacheDriver;
88     }
89
90     /**
91      * Returns an array of all the loaded metadata currently in memory.
92      *
93      * @return ClassMetadata[]
94      */
95     public function getLoadedMetadata()
96     {
97         return $this->loadedMetadata;
98     }
99
100     /**
101      * Forces the factory to load the metadata of all classes known to the underlying
102      * mapping driver.
103      *
104      * @return array The ClassMetadata instances of all mapped classes.
105      */
106     public function getAllMetadata()
107     {
108         if ( ! $this->initialized) {
109             $this->initialize();
110         }
111
112         $driver = $this->getDriver();
113         $metadata = [];
114         foreach ($driver->getAllClassNames() as $className) {
115             $metadata[] = $this->getMetadataFor($className);
116         }
117
118         return $metadata;
119     }
120
121     /**
122      * Lazy initialization of this stuff, especially the metadata driver,
123      * since these are not needed at all when a metadata cache is active.
124      *
125      * @return void
126      */
127     abstract protected function initialize();
128
129     /**
130      * Gets the fully qualified class-name from the namespace alias.
131      *
132      * @param string $namespaceAlias
133      * @param string $simpleClassName
134      *
135      * @return string
136      */
137     abstract protected function getFqcnFromAlias($namespaceAlias, $simpleClassName);
138
139     /**
140      * Returns the mapping driver implementation.
141      *
142      * @return \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver
143      */
144     abstract protected function getDriver();
145
146     /**
147      * Wakes up reflection after ClassMetadata gets unserialized from cache.
148      *
149      * @param ClassMetadata     $class
150      * @param ReflectionService $reflService
151      *
152      * @return void
153      */
154     abstract protected function wakeupReflection(ClassMetadata $class, ReflectionService $reflService);
155
156     /**
157      * Initializes Reflection after ClassMetadata was constructed.
158      *
159      * @param ClassMetadata     $class
160      * @param ReflectionService $reflService
161      *
162      * @return void
163      */
164     abstract protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService);
165
166     /**
167      * Checks whether the class metadata is an entity.
168      *
169      * This method should return false for mapped superclasses or embedded classes.
170      *
171      * @param ClassMetadata $class
172      *
173      * @return boolean
174      */
175     abstract protected function isEntity(ClassMetadata $class);
176
177     /**
178      * Gets the class metadata descriptor for a class.
179      *
180      * @param string $className The name of the class.
181      *
182      * @return ClassMetadata
183      *
184      * @throws ReflectionException
185      * @throws MappingException
186      */
187     public function getMetadataFor($className)
188     {
189         if (isset($this->loadedMetadata[$className])) {
190             return $this->loadedMetadata[$className];
191         }
192
193         // Check for namespace alias
194         if (strpos($className, ':') !== false) {
195             list($namespaceAlias, $simpleClassName) = explode(':', $className, 2);
196
197             $realClassName = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName);
198         } else {
199             $realClassName = ClassUtils::getRealClass($className);
200         }
201
202         if (isset($this->loadedMetadata[$realClassName])) {
203             // We do not have the alias name in the map, include it
204             return $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
205         }
206
207         $loadingException = null;
208
209         try {
210             if ($this->cacheDriver) {
211                 $cached = $this->cacheDriver->fetch($realClassName . $this->cacheSalt);
212                 if ($cached instanceof ClassMetadata) {
213                     $this->loadedMetadata[$realClassName] = $cached;
214
215                     $this->wakeupReflection($cached, $this->getReflectionService());
216                 } else {
217                     foreach ($this->loadMetadata($realClassName) as $loadedClassName) {
218                         $this->cacheDriver->save(
219                             $loadedClassName . $this->cacheSalt,
220                             $this->loadedMetadata[$loadedClassName],
221                             null
222                         );
223                     }
224                 }
225             } else {
226                 $this->loadMetadata($realClassName);
227             }
228         } catch (MappingException $loadingException) {
229             if (! $fallbackMetadataResponse = $this->onNotFoundMetadata($realClassName)) {
230                 throw $loadingException;
231             }
232
233             $this->loadedMetadata[$realClassName] = $fallbackMetadataResponse;
234         }
235
236         if ($className !== $realClassName) {
237             // We do not have the alias name in the map, include it
238             $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
239         }
240
241         return $this->loadedMetadata[$className];
242     }
243
244     /**
245      * Checks whether the factory has the metadata for a class loaded already.
246      *
247      * @param string $className
248      *
249      * @return boolean TRUE if the metadata of the class in question is already loaded, FALSE otherwise.
250      */
251     public function hasMetadataFor($className)
252     {
253         return isset($this->loadedMetadata[$className]);
254     }
255
256     /**
257      * Sets the metadata descriptor for a specific class.
258      *
259      * NOTE: This is only useful in very special cases, like when generating proxy classes.
260      *
261      * @param string        $className
262      * @param ClassMetadata $class
263      *
264      * @return void
265      */
266     public function setMetadataFor($className, $class)
267     {
268         $this->loadedMetadata[$className] = $class;
269     }
270
271     /**
272      * Gets an array of parent classes for the given entity class.
273      *
274      * @param string $name
275      *
276      * @return array
277      */
278     protected function getParentClasses($name)
279     {
280         // Collect parent classes, ignoring transient (not-mapped) classes.
281         $parentClasses = [];
282         foreach (array_reverse($this->getReflectionService()->getParentClasses($name)) as $parentClass) {
283             if ( ! $this->getDriver()->isTransient($parentClass)) {
284                 $parentClasses[] = $parentClass;
285             }
286         }
287         return $parentClasses;
288     }
289
290     /**
291      * Loads the metadata of the class in question and all it's ancestors whose metadata
292      * is still not loaded.
293      *
294      * Important: The class $name does not necessarily exist at this point here.
295      * Scenarios in a code-generation setup might have access to XML/YAML
296      * Mapping files without the actual PHP code existing here. That is why the
297      * {@see Doctrine\Common\Persistence\Mapping\ReflectionService} interface
298      * should be used for reflection.
299      *
300      * @param string $name The name of the class for which the metadata should get loaded.
301      *
302      * @return array
303      */
304     protected function loadMetadata($name)
305     {
306         if ( ! $this->initialized) {
307             $this->initialize();
308         }
309
310         $loaded = [];
311
312         $parentClasses = $this->getParentClasses($name);
313         $parentClasses[] = $name;
314
315         // Move down the hierarchy of parent classes, starting from the topmost class
316         $parent = null;
317         $rootEntityFound = false;
318         $visited = [];
319         $reflService = $this->getReflectionService();
320         foreach ($parentClasses as $className) {
321             if (isset($this->loadedMetadata[$className])) {
322                 $parent = $this->loadedMetadata[$className];
323                 if ($this->isEntity($parent)) {
324                     $rootEntityFound = true;
325                     array_unshift($visited, $className);
326                 }
327                 continue;
328             }
329
330             $class = $this->newClassMetadataInstance($className);
331             $this->initializeReflection($class, $reflService);
332
333             $this->doLoadMetadata($class, $parent, $rootEntityFound, $visited);
334
335             $this->loadedMetadata[$className] = $class;
336
337             $parent = $class;
338
339             if ($this->isEntity($class)) {
340                 $rootEntityFound = true;
341                 array_unshift($visited, $className);
342             }
343
344             $this->wakeupReflection($class, $reflService);
345
346             $loaded[] = $className;
347         }
348
349         return $loaded;
350     }
351
352     /**
353      * Provides a fallback hook for loading metadata when loading failed due to reflection/mapping exceptions
354      *
355      * Override this method to implement a fallback strategy for failed metadata loading
356      *
357      * @param string $className
358      *
359      * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata|null
360      */
361     protected function onNotFoundMetadata($className)
362     {
363         return null;
364     }
365
366     /**
367      * Actually loads the metadata from the underlying metadata.
368      *
369      * @param ClassMetadata      $class
370      * @param ClassMetadata|null $parent
371      * @param bool               $rootEntityFound
372      * @param array              $nonSuperclassParents All parent class names
373      *                                                 that are not marked as mapped superclasses.
374      *
375      * @return void
376      */
377     abstract protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents);
378
379     /**
380      * Creates a new ClassMetadata instance for the given class name.
381      *
382      * @param string $className
383      *
384      * @return ClassMetadata
385      */
386     abstract protected function newClassMetadataInstance($className);
387
388     /**
389      * {@inheritDoc}
390      */
391     public function isTransient($class)
392     {
393         if ( ! $this->initialized) {
394             $this->initialize();
395         }
396
397         // Check for namespace alias
398         if (strpos($class, ':') !== false) {
399             list($namespaceAlias, $simpleClassName) = explode(':', $class, 2);
400             $class = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName);
401         }
402
403         return $this->getDriver()->isTransient($class);
404     }
405
406     /**
407      * Sets the reflectionService.
408      *
409      * @param ReflectionService $reflectionService
410      *
411      * @return void
412      */
413     public function setReflectionService(ReflectionService $reflectionService)
414     {
415         $this->reflectionService = $reflectionService;
416     }
417
418     /**
419      * Gets the reflection service associated with this metadata factory.
420      *
421      * @return ReflectionService
422      */
423     public function getReflectionService()
424     {
425         if ($this->reflectionService === null) {
426             $this->reflectionService = new RuntimeReflectionService();
427         }
428         return $this->reflectionService;
429     }
430 }