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