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\Validator\Mapping\Factory;
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;
20 * Creates new {@link ClassMetadataInterface} instances.
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.
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}.
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.
37 * @author Bernhard Schussek <bschussek@gmail.com>
39 class LazyLoadingMetadataFactory implements MetadataFactoryInterface
42 * The loader for loading the class metadata.
44 * @var LoaderInterface|null
49 * The cache for caching class metadata.
51 * @var CacheInterface|null
56 * The loaded metadata, indexed by class name.
58 * @var ClassMetadata[]
60 protected $loadedClasses = array();
63 * Creates a new metadata factory.
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
69 public function __construct(LoaderInterface $loader = null, CacheInterface $cache = null)
71 $this->loader = $loader;
72 $this->cache = $cache;
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.
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.
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.
90 public function getMetadataFor($value)
92 if (!is_object($value) && !is_string($value)) {
93 throw new NoSuchMetadataException(sprintf('Cannot create metadata for non-objects. Got: %s', gettype($value)));
96 $class = ltrim(is_object($value) ? get_class($value) : $value, '\\');
98 if (isset($this->loadedClasses[$class])) {
99 return $this->loadedClasses[$class];
102 if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
103 // Include constraints from the parent class
104 $this->mergeConstraints($metadata);
106 return $this->loadedClasses[$class] = $metadata;
109 if (!class_exists($class) && !interface_exists($class)) {
110 throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
113 $metadata = new ClassMetadata($class);
115 if (null !== $this->loader) {
116 $this->loader->loadClassMetadata($metadata);
119 if (null !== $this->cache) {
120 $this->cache->write($metadata);
123 // Include constraints from the parent class
124 $this->mergeConstraints($metadata);
126 return $this->loadedClasses[$class] = $metadata;
129 private function mergeConstraints(ClassMetadata $metadata)
131 // Include constraints from the parent class
132 if ($parent = $metadata->getReflectionClass()->getParentClass()) {
133 $metadata->mergeConstraints($this->getMetadataFor($parent->name));
136 $interfaces = $metadata->getReflectionClass()->getInterfaces();
138 $interfaces = array_filter($interfaces, function ($interface) use ($parent, $interfaces) {
139 $interfaceName = $interface->getName();
141 if ($parent && $parent->implementsInterface($interfaceName)) {
145 foreach ($interfaces as $i) {
146 if ($i !== $interface && $i->implementsInterface($interfaceName)) {
154 // Include constraints from all directly implemented interfaces
155 foreach ($interfaces as $interface) {
156 if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) {
159 $metadata->mergeConstraints($this->getMetadataFor($interface->name));
166 public function hasMetadataFor($value)
168 if (!is_object($value) && !is_string($value)) {
172 $class = ltrim(is_object($value) ? get_class($value) : $value, '\\');
174 if (class_exists($class) || interface_exists($class)) {