294dfd9e367591f80541f420acf0a53f48d51ced
[yaffs-website] / serializer / Mapping / Factory / ClassMetadataFactory.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\Serializer\Mapping\Factory;
13
14 use Doctrine\Common\Cache\Cache;
15 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16 use Symfony\Component\Serializer\Mapping\ClassMetadata;
17 use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;
18
19 /**
20  * Returns a {@link ClassMetadata}.
21  *
22  * @author Kévin Dunglas <dunglas@gmail.com>
23  */
24 class ClassMetadataFactory implements ClassMetadataFactoryInterface
25 {
26     use ClassResolverTrait;
27
28     private $loader;
29     private $cache;
30     private $loadedClasses;
31
32     public function __construct(LoaderInterface $loader, Cache $cache = null)
33     {
34         $this->loader = $loader;
35         $this->cache = $cache;
36
37         if (null !== $cache) {
38             @trigger_error(sprintf('Passing a Doctrine Cache instance as 2nd parameter of the "%s" constructor is deprecated since Symfony 3.1. This parameter will be removed in Symfony 4.0. Use the "%s" class instead.', __CLASS__, CacheClassMetadataFactory::class), E_USER_DEPRECATED);
39         }
40     }
41
42     /**
43      * {@inheritdoc}
44      */
45     public function getMetadataFor($value)
46     {
47         $class = $this->getClass($value);
48
49         if (isset($this->loadedClasses[$class])) {
50             return $this->loadedClasses[$class];
51         }
52
53         if ($this->cache && ($this->loadedClasses[$class] = $this->cache->fetch($class))) {
54             return $this->loadedClasses[$class];
55         }
56
57         $classMetadata = new ClassMetadata($class);
58         $this->loader->loadClassMetadata($classMetadata);
59
60         $reflectionClass = $classMetadata->getReflectionClass();
61
62         // Include metadata from the parent class
63         if ($parent = $reflectionClass->getParentClass()) {
64             $classMetadata->merge($this->getMetadataFor($parent->name));
65         }
66
67         // Include metadata from all implemented interfaces
68         foreach ($reflectionClass->getInterfaces() as $interface) {
69             $classMetadata->merge($this->getMetadataFor($interface->name));
70         }
71
72         if ($this->cache) {
73             $this->cache->save($class, $classMetadata);
74         }
75
76         return $this->loadedClasses[$class] = $classMetadata;
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     public function hasMetadataFor($value)
83     {
84         try {
85             $this->getClass($value);
86
87             return true;
88         } catch (InvalidArgumentException $invalidArgumentException) {
89             // Return false in case of exception
90         }
91
92         return false;
93     }
94 }