annotate vendor/symfony/validator/Mapping/Factory/LazyLoadingMetadataFactory.php @ 0:4c8ae668cc8c

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