You are here

class LazyLoadingMetadataFactory in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Mapping/Factory/LazyLoadingMetadataFactory.php \Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory

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>

Hierarchy

Expanded class hierarchy of LazyLoadingMetadataFactory

3 files declare their use of LazyLoadingMetadataFactory
ClassMetadataFactory.php in vendor/symfony/validator/Mapping/ClassMetadataFactory.php
LazyLoadingMetadataFactoryTest.php in vendor/symfony/validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php
ValidatorBuilder.php in vendor/symfony/validator/ValidatorBuilder.php

File

vendor/symfony/validator/Mapping/Factory/LazyLoadingMetadataFactory.php, line 41

Namespace

Symfony\Component\Validator\Mapping\Factory
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LazyLoadingMetadataFactory::$cache protected property The cache for caching class metadata.
LazyLoadingMetadataFactory::$loadedClasses protected property The loaded metadata, indexed by class name.
LazyLoadingMetadataFactory::$loader protected property The loader for loading the class metadata.
LazyLoadingMetadataFactory::getMetadataFor public function If the method was called with the same class name (or an object of that class) before, the same metadata instance is returned. Overrides MetadataFactoryInterface::getMetadataFor
LazyLoadingMetadataFactory::hasMetadataFor public function Returns whether the class is able to return metadata for the given value. Overrides MetadataFactoryInterface::hasMetadataFor
LazyLoadingMetadataFactory::__construct public function Creates a new metadata factory.