comparison vendor/symfony/serializer/Mapping/Factory/ClassMetadataFactory.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 /**
29 * @var LoaderInterface
30 */
31 private $loader;
32
33 /**
34 * @var Cache
35 */
36 private $cache;
37
38 /**
39 * @var array
40 */
41 private $loadedClasses;
42
43 /**
44 * @param LoaderInterface $loader
45 * @param Cache|null $cache
46 */
47 public function __construct(LoaderInterface $loader, Cache $cache = null)
48 {
49 $this->loader = $loader;
50 $this->cache = $cache;
51
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);
54 }
55 }
56
57 /**
58 * {@inheritdoc}
59 */
60 public function getMetadataFor($value)
61 {
62 $class = $this->getClass($value);
63
64 if (isset($this->loadedClasses[$class])) {
65 return $this->loadedClasses[$class];
66 }
67
68 if ($this->cache && ($this->loadedClasses[$class] = $this->cache->fetch($class))) {
69 return $this->loadedClasses[$class];
70 }
71
72 $classMetadata = new ClassMetadata($class);
73 $this->loader->loadClassMetadata($classMetadata);
74
75 $reflectionClass = $classMetadata->getReflectionClass();
76
77 // Include metadata from the parent class
78 if ($parent = $reflectionClass->getParentClass()) {
79 $classMetadata->merge($this->getMetadataFor($parent->name));
80 }
81
82 // Include metadata from all implemented interfaces
83 foreach ($reflectionClass->getInterfaces() as $interface) {
84 $classMetadata->merge($this->getMetadataFor($interface->name));
85 }
86
87 if ($this->cache) {
88 $this->cache->save($class, $classMetadata);
89 }
90
91 return $this->loadedClasses[$class] = $classMetadata;
92 }
93
94 /**
95 * {@inheritdoc}
96 */
97 public function hasMetadataFor($value)
98 {
99 try {
100 $this->getClass($value);
101
102 return true;
103 } catch (InvalidArgumentException $invalidArgumentException) {
104 // Return false in case of exception
105 }
106
107 return false;
108 }
109 }