Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator\Mapping\Factory; Chris@0: Chris@0: use Symfony\Component\Validator\Exception\NoSuchMetadataException; Chris@0: use Symfony\Component\Validator\Mapping\Cache\CacheInterface; Chris@0: use Symfony\Component\Validator\Mapping\ClassMetadata; Chris@0: use Symfony\Component\Validator\Mapping\Loader\LoaderInterface; Chris@0: Chris@0: /** Chris@0: * Creates new {@link ClassMetadataInterface} instances. Chris@0: * Chris@0: * Whenever {@link getMetadataFor()} is called for the first time with a given Chris@0: * class name or object of that class, a new metadata instance is created and Chris@0: * returned. On subsequent requests for the same class, the same metadata Chris@0: * instance will be returned. Chris@0: * Chris@0: * You can optionally pass a {@link LoaderInterface} instance to the constructor. Chris@0: * Whenever a new metadata instance is created, it is passed to the loader, Chris@0: * which can configure the metadata based on configuration loaded from the Chris@0: * filesystem or a database. If you want to use multiple loaders, wrap them in a Chris@0: * {@link LoaderChain}. Chris@0: * Chris@0: * You can also optionally pass a {@link CacheInterface} instance to the Chris@0: * constructor. This cache will be used for persisting the generated metadata Chris@0: * between multiple PHP requests. Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: class LazyLoadingMetadataFactory implements MetadataFactoryInterface Chris@0: { Chris@0: protected $loader; Chris@0: protected $cache; Chris@0: Chris@0: /** Chris@0: * The loaded metadata, indexed by class name. Chris@0: * Chris@0: * @var ClassMetadata[] Chris@0: */ Chris@17: protected $loadedClasses = []; Chris@0: Chris@0: /** Chris@0: * Creates a new metadata factory. Chris@0: * Chris@0: * @param LoaderInterface|null $loader The loader for configuring new metadata Chris@0: * @param CacheInterface|null $cache The cache for persisting metadata Chris@0: * between multiple PHP requests Chris@0: */ Chris@0: public function __construct(LoaderInterface $loader = null, CacheInterface $cache = null) Chris@0: { Chris@0: $this->loader = $loader; Chris@0: $this->cache = $cache; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * If the method was called with the same class name (or an object of that Chris@0: * class) before, the same metadata instance is returned. Chris@0: * Chris@0: * If the factory was configured with a cache, this method will first look Chris@0: * for an existing metadata instance in the cache. If an existing instance Chris@0: * is found, it will be returned without further ado. Chris@0: * Chris@0: * Otherwise, a new metadata instance is created. If the factory was Chris@0: * configured with a loader, the metadata is passed to the Chris@0: * {@link LoaderInterface::loadClassMetadata()} method for further Chris@0: * configuration. At last, the new object is returned. Chris@0: */ Chris@0: public function getMetadataFor($value) Chris@0: { Chris@17: if (!\is_object($value) && !\is_string($value)) { Chris@17: throw new NoSuchMetadataException(sprintf('Cannot create metadata for non-objects. Got: %s', \gettype($value))); Chris@0: } Chris@0: Chris@17: $class = ltrim(\is_object($value) ? \get_class($value) : $value, '\\'); Chris@0: Chris@0: if (isset($this->loadedClasses[$class])) { Chris@0: return $this->loadedClasses[$class]; Chris@0: } Chris@0: Chris@16: if (!class_exists($class) && !interface_exists($class, false)) { Chris@16: throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class)); Chris@16: } Chris@16: Chris@0: if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) { Chris@0: // Include constraints from the parent class Chris@0: $this->mergeConstraints($metadata); Chris@0: Chris@0: return $this->loadedClasses[$class] = $metadata; Chris@0: } Chris@0: Chris@0: $metadata = new ClassMetadata($class); Chris@0: Chris@0: if (null !== $this->loader) { Chris@0: $this->loader->loadClassMetadata($metadata); Chris@0: } Chris@0: Chris@0: if (null !== $this->cache) { Chris@0: $this->cache->write($metadata); Chris@0: } Chris@0: Chris@0: // Include constraints from the parent class Chris@0: $this->mergeConstraints($metadata); Chris@0: Chris@0: return $this->loadedClasses[$class] = $metadata; Chris@0: } Chris@0: Chris@0: private function mergeConstraints(ClassMetadata $metadata) Chris@0: { Chris@0: // Include constraints from the parent class Chris@0: if ($parent = $metadata->getReflectionClass()->getParentClass()) { Chris@0: $metadata->mergeConstraints($this->getMetadataFor($parent->name)); Chris@0: } Chris@0: Chris@0: $interfaces = $metadata->getReflectionClass()->getInterfaces(); Chris@0: Chris@0: $interfaces = array_filter($interfaces, function ($interface) use ($parent, $interfaces) { Chris@0: $interfaceName = $interface->getName(); Chris@0: Chris@0: if ($parent && $parent->implementsInterface($interfaceName)) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: foreach ($interfaces as $i) { Chris@0: if ($i !== $interface && $i->implementsInterface($interfaceName)) { Chris@0: return false; Chris@0: } Chris@0: } Chris@0: Chris@0: return true; Chris@0: }); Chris@0: Chris@0: // Include constraints from all directly implemented interfaces Chris@0: foreach ($interfaces as $interface) { Chris@0: if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) { Chris@0: continue; Chris@0: } Chris@0: $metadata->mergeConstraints($this->getMetadataFor($interface->name)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function hasMetadataFor($value) Chris@0: { Chris@17: if (!\is_object($value) && !\is_string($value)) { Chris@0: return false; Chris@0: } Chris@0: Chris@17: $class = ltrim(\is_object($value) ? \get_class($value) : $value, '\\'); Chris@0: Chris@16: return class_exists($class) || interface_exists($class, false); Chris@0: } Chris@0: }