annotate vendor/symfony/validator/Mapping/Factory/LazyLoadingMetadataFactory.php @ 19:fa3358dc1485 tip

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