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
45 * The loaded metadata, indexed by class name.
47 * @var ClassMetadata[]
49 protected $loadedClasses = array();
52 * Creates a new metadata factory.
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
58 public function __construct(LoaderInterface $loader = null, CacheInterface $cache = null)
60 $this->loader = $loader;
61 $this->cache = $cache;
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.
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.
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.
79 public function getMetadataFor($value)
81 if (!is_object($value) && !is_string($value)) {
82 throw new NoSuchMetadataException(sprintf('Cannot create metadata for non-objects. Got: %s', gettype($value)));
85 $class = ltrim(is_object($value) ? get_class($value) : $value, '\\');
87 if (isset($this->loadedClasses[$class])) {
88 return $this->loadedClasses[$class];
91 if (!class_exists($class) && !interface_exists($class, false)) {
92 throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
95 if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
96 // Include constraints from the parent class
97 $this->mergeConstraints($metadata);
99 return $this->loadedClasses[$class] = $metadata;
102 $metadata = new ClassMetadata($class);
104 if (null !== $this->loader) {
105 $this->loader->loadClassMetadata($metadata);
108 if (null !== $this->cache) {
109 $this->cache->write($metadata);
112 // Include constraints from the parent class
113 $this->mergeConstraints($metadata);
115 return $this->loadedClasses[$class] = $metadata;
118 private function mergeConstraints(ClassMetadata $metadata)
120 // Include constraints from the parent class
121 if ($parent = $metadata->getReflectionClass()->getParentClass()) {
122 $metadata->mergeConstraints($this->getMetadataFor($parent->name));
125 $interfaces = $metadata->getReflectionClass()->getInterfaces();
127 $interfaces = array_filter($interfaces, function ($interface) use ($parent, $interfaces) {
128 $interfaceName = $interface->getName();
130 if ($parent && $parent->implementsInterface($interfaceName)) {
134 foreach ($interfaces as $i) {
135 if ($i !== $interface && $i->implementsInterface($interfaceName)) {
143 // Include constraints from all directly implemented interfaces
144 foreach ($interfaces as $interface) {
145 if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) {
148 $metadata->mergeConstraints($this->getMetadataFor($interface->name));
155 public function hasMetadataFor($value)
157 if (!is_object($value) && !is_string($value)) {
161 $class = ltrim(is_object($value) ? get_class($value) : $value, '\\');
163 return class_exists($class) || interface_exists($class, false);