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;
30 private $loadedClasses;
32 public function __construct(LoaderInterface $loader, Cache $cache = null)
34 $this->loader = $loader;
35 $this->cache = $cache;
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);
45 public function getMetadataFor($value)
47 $class = $this->getClass($value);
49 if (isset($this->loadedClasses[$class])) {
50 return $this->loadedClasses[$class];
53 if ($this->cache && ($this->loadedClasses[$class] = $this->cache->fetch($class))) {
54 return $this->loadedClasses[$class];
57 $classMetadata = new ClassMetadata($class);
58 $this->loader->loadClassMetadata($classMetadata);
60 $reflectionClass = $classMetadata->getReflectionClass();
62 // Include metadata from the parent class
63 if ($parent = $reflectionClass->getParentClass()) {
64 $classMetadata->merge($this->getMetadataFor($parent->name));
67 // Include metadata from all implemented interfaces
68 foreach ($reflectionClass->getInterfaces() as $interface) {
69 $classMetadata->merge($this->getMetadataFor($interface->name));
73 $this->cache->save($class, $classMetadata);
76 return $this->loadedClasses[$class] = $classMetadata;
82 public function hasMetadataFor($value)
85 $this->getClass($value);
88 } catch (InvalidArgumentException $invalidArgumentException) {
89 // Return false in case of exception