4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Mapping\Factory;
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;
20 * Returns a {@link ClassMetadata}.
22 * @author Kévin Dunglas <dunglas@gmail.com>
24 class ClassMetadataFactory implements ClassMetadataFactoryInterface
26 use ClassResolverTrait;
29 * @var LoaderInterface
41 private $loadedClasses;
44 * @param LoaderInterface $loader
45 * @param Cache|null $cache
47 public function __construct(LoaderInterface $loader, Cache $cache = null)
49 $this->loader = $loader;
50 $this->cache = $cache;
52 if (null !== $cache) {
53 @trigger_error(sprintf('Passing a Doctrine Cache instance as 2nd parameter of the "%s" constructor is deprecated since version 3.1. This parameter will be removed in Symfony 4.0. Use the "%s" class instead.', __CLASS__, CacheClassMetadataFactory::class), E_USER_DEPRECATED);
60 public function getMetadataFor($value)
62 $class = $this->getClass($value);
64 if (isset($this->loadedClasses[$class])) {
65 return $this->loadedClasses[$class];
68 if ($this->cache && ($this->loadedClasses[$class] = $this->cache->fetch($class))) {
69 return $this->loadedClasses[$class];
72 $classMetadata = new ClassMetadata($class);
73 $this->loader->loadClassMetadata($classMetadata);
75 $reflectionClass = $classMetadata->getReflectionClass();
77 // Include metadata from the parent class
78 if ($parent = $reflectionClass->getParentClass()) {
79 $classMetadata->merge($this->getMetadataFor($parent->name));
82 // Include metadata from all implemented interfaces
83 foreach ($reflectionClass->getInterfaces() as $interface) {
84 $classMetadata->merge($this->getMetadataFor($interface->name));
88 $this->cache->save($class, $classMetadata);
91 return $this->loadedClasses[$class] = $classMetadata;
97 public function hasMetadataFor($value)
100 $this->getClass($value);
103 } catch (InvalidArgumentException $invalidArgumentException) {
104 // Return false in case of exception