You are here

public function LazyLoadingMetadataFactory::getMetadataFor in Plug 7

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.

Overrides MetadataFactoryInterface::getMetadataFor

File

lib/Symfony/validator/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php, line 92

Class

LazyLoadingMetadataFactory
Creates new {@link ClassMetadataInterface} instances.

Namespace

Symfony\Component\Validator\Mapping\Factory

Code

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;
}