comparison vendor/symfony/validator/Mapping/Factory/LazyLoadingMetadataFactory.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\Validator\Mapping\Factory;
13
14 use Symfony\Component\Validator\Exception\NoSuchMetadataException;
15 use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
16 use Symfony\Component\Validator\Mapping\ClassMetadata;
17 use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
18
19 /**
20 * Creates new {@link ClassMetadataInterface} instances.
21 *
22 * Whenever {@link getMetadataFor()} is called for the first time with a given
23 * class name or object of that class, a new metadata instance is created and
24 * returned. On subsequent requests for the same class, the same metadata
25 * instance will be returned.
26 *
27 * You can optionally pass a {@link LoaderInterface} instance to the constructor.
28 * Whenever a new metadata instance is created, it is passed to the loader,
29 * which can configure the metadata based on configuration loaded from the
30 * filesystem or a database. If you want to use multiple loaders, wrap them in a
31 * {@link LoaderChain}.
32 *
33 * You can also optionally pass a {@link CacheInterface} instance to the
34 * constructor. This cache will be used for persisting the generated metadata
35 * between multiple PHP requests.
36 *
37 * @author Bernhard Schussek <bschussek@gmail.com>
38 */
39 class LazyLoadingMetadataFactory implements MetadataFactoryInterface
40 {
41 /**
42 * The loader for loading the class metadata.
43 *
44 * @var LoaderInterface|null
45 */
46 protected $loader;
47
48 /**
49 * The cache for caching class metadata.
50 *
51 * @var CacheInterface|null
52 */
53 protected $cache;
54
55 /**
56 * The loaded metadata, indexed by class name.
57 *
58 * @var ClassMetadata[]
59 */
60 protected $loadedClasses = array();
61
62 /**
63 * Creates a new metadata factory.
64 *
65 * @param LoaderInterface|null $loader The loader for configuring new metadata
66 * @param CacheInterface|null $cache The cache for persisting metadata
67 * between multiple PHP requests
68 */
69 public function __construct(LoaderInterface $loader = null, CacheInterface $cache = null)
70 {
71 $this->loader = $loader;
72 $this->cache = $cache;
73 }
74
75 /**
76 * {@inheritdoc}
77 *
78 * If the method was called with the same class name (or an object of that
79 * class) before, the same metadata instance is returned.
80 *
81 * If the factory was configured with a cache, this method will first look
82 * for an existing metadata instance in the cache. If an existing instance
83 * is found, it will be returned without further ado.
84 *
85 * Otherwise, a new metadata instance is created. If the factory was
86 * configured with a loader, the metadata is passed to the
87 * {@link LoaderInterface::loadClassMetadata()} method for further
88 * configuration. At last, the new object is returned.
89 */
90 public function getMetadataFor($value)
91 {
92 if (!is_object($value) && !is_string($value)) {
93 throw new NoSuchMetadataException(sprintf('Cannot create metadata for non-objects. Got: %s', gettype($value)));
94 }
95
96 $class = ltrim(is_object($value) ? get_class($value) : $value, '\\');
97
98 if (isset($this->loadedClasses[$class])) {
99 return $this->loadedClasses[$class];
100 }
101
102 if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
103 // Include constraints from the parent class
104 $this->mergeConstraints($metadata);
105
106 return $this->loadedClasses[$class] = $metadata;
107 }
108
109 if (!class_exists($class) && !interface_exists($class)) {
110 throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
111 }
112
113 $metadata = new ClassMetadata($class);
114
115 if (null !== $this->loader) {
116 $this->loader->loadClassMetadata($metadata);
117 }
118
119 if (null !== $this->cache) {
120 $this->cache->write($metadata);
121 }
122
123 // Include constraints from the parent class
124 $this->mergeConstraints($metadata);
125
126 return $this->loadedClasses[$class] = $metadata;
127 }
128
129 private function mergeConstraints(ClassMetadata $metadata)
130 {
131 // Include constraints from the parent class
132 if ($parent = $metadata->getReflectionClass()->getParentClass()) {
133 $metadata->mergeConstraints($this->getMetadataFor($parent->name));
134 }
135
136 $interfaces = $metadata->getReflectionClass()->getInterfaces();
137
138 $interfaces = array_filter($interfaces, function ($interface) use ($parent, $interfaces) {
139 $interfaceName = $interface->getName();
140
141 if ($parent && $parent->implementsInterface($interfaceName)) {
142 return false;
143 }
144
145 foreach ($interfaces as $i) {
146 if ($i !== $interface && $i->implementsInterface($interfaceName)) {
147 return false;
148 }
149 }
150
151 return true;
152 });
153
154 // Include constraints from all directly implemented interfaces
155 foreach ($interfaces as $interface) {
156 if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) {
157 continue;
158 }
159 $metadata->mergeConstraints($this->getMetadataFor($interface->name));
160 }
161 }
162
163 /**
164 * {@inheritdoc}
165 */
166 public function hasMetadataFor($value)
167 {
168 if (!is_object($value) && !is_string($value)) {
169 return false;
170 }
171
172 $class = ltrim(is_object($value) ? get_class($value) : $value, '\\');
173
174 if (class_exists($class) || interface_exists($class)) {
175 return true;
176 }
177
178 return false;
179 }
180 }