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