fbb8c66669fc4d53f72a7ea97c234a4e87d1a3fb
[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\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     protected $loader;
42     protected $cache;
43
44     /**
45      * The loaded metadata, indexed by class name.
46      *
47      * @var ClassMetadata[]
48      */
49     protected $loadedClasses = array();
50
51     /**
52      * Creates a new metadata factory.
53      *
54      * @param LoaderInterface|null $loader The loader for configuring new metadata
55      * @param CacheInterface|null  $cache  The cache for persisting metadata
56      *                                     between multiple PHP requests
57      */
58     public function __construct(LoaderInterface $loader = null, CacheInterface $cache = null)
59     {
60         $this->loader = $loader;
61         $this->cache = $cache;
62     }
63
64     /**
65      * {@inheritdoc}
66      *
67      * If the method was called with the same class name (or an object of that
68      * class) before, the same metadata instance is returned.
69      *
70      * If the factory was configured with a cache, this method will first look
71      * for an existing metadata instance in the cache. If an existing instance
72      * is found, it will be returned without further ado.
73      *
74      * Otherwise, a new metadata instance is created. If the factory was
75      * configured with a loader, the metadata is passed to the
76      * {@link LoaderInterface::loadClassMetadata()} method for further
77      * configuration. At last, the new object is returned.
78      */
79     public function getMetadataFor($value)
80     {
81         if (!is_object($value) && !is_string($value)) {
82             throw new NoSuchMetadataException(sprintf('Cannot create metadata for non-objects. Got: %s', gettype($value)));
83         }
84
85         $class = ltrim(is_object($value) ? get_class($value) : $value, '\\');
86
87         if (isset($this->loadedClasses[$class])) {
88             return $this->loadedClasses[$class];
89         }
90
91         if (!class_exists($class) && !interface_exists($class, false)) {
92             throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
93         }
94
95         if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
96             // Include constraints from the parent class
97             $this->mergeConstraints($metadata);
98
99             return $this->loadedClasses[$class] = $metadata;
100         }
101
102         $metadata = new ClassMetadata($class);
103
104         if (null !== $this->loader) {
105             $this->loader->loadClassMetadata($metadata);
106         }
107
108         if (null !== $this->cache) {
109             $this->cache->write($metadata);
110         }
111
112         // Include constraints from the parent class
113         $this->mergeConstraints($metadata);
114
115         return $this->loadedClasses[$class] = $metadata;
116     }
117
118     private function mergeConstraints(ClassMetadata $metadata)
119     {
120         // Include constraints from the parent class
121         if ($parent = $metadata->getReflectionClass()->getParentClass()) {
122             $metadata->mergeConstraints($this->getMetadataFor($parent->name));
123         }
124
125         $interfaces = $metadata->getReflectionClass()->getInterfaces();
126
127         $interfaces = array_filter($interfaces, function ($interface) use ($parent, $interfaces) {
128             $interfaceName = $interface->getName();
129
130             if ($parent && $parent->implementsInterface($interfaceName)) {
131                 return false;
132             }
133
134             foreach ($interfaces as $i) {
135                 if ($i !== $interface && $i->implementsInterface($interfaceName)) {
136                     return false;
137                 }
138             }
139
140             return true;
141         });
142
143         // Include constraints from all directly implemented interfaces
144         foreach ($interfaces as $interface) {
145             if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) {
146                 continue;
147             }
148             $metadata->mergeConstraints($this->getMetadataFor($interface->name));
149         }
150     }
151
152     /**
153      * {@inheritdoc}
154      */
155     public function hasMetadataFor($value)
156     {
157         if (!is_object($value) && !is_string($value)) {
158             return false;
159         }
160
161         $class = ltrim(is_object($value) ? get_class($value) : $value, '\\');
162
163         return class_exists($class) || interface_exists($class, false);
164     }
165 }