public function getClassMetadata($class)
 {
     $class = ltrim($class, '\\');
     if (!isset($this->loadedClasses[$class])) {
         if ($this->cache !== null && $this->cache->has($class)) {
             $this->loadedClasses[$class] = $this->cache->read($class);
         } else {
             $metadata = new ClassMetadata($class);
             // Include constraints from the parent class
             if ($parent = $metadata->getReflectionClass()->getParentClass()) {
                 $metadata->mergeConstraints($this->getClassMetadata($parent->getName()));
             }
             // Include constraints from all implemented interfaces
             foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) {
                 $metadata->mergeConstraints($this->getClassMetadata($interface->getName()));
             }
             $this->loader->loadClassMetadata($metadata);
             $this->loadedClasses[$class] = $metadata;
             if ($this->cache !== null) {
                 $this->cache->write($metadata);
             }
         }
     }
     return $this->loadedClasses[$class];
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 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;
 }
 public function getClassMetadata($class)
 {
     $class = ltrim($class, '\\');
     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];
     }
     $metadata = new ClassMetadata($class);
     // Include constraints from the parent class
     if ($parent = $metadata->getReflectionClass()->getParentClass()) {
         $metadata->mergeConstraints($this->getClassMetadata($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->getClassMetadata($interface->name));
     }
     if (null !== $this->loader) {
         $this->loader->loadClassMetadata($metadata);
     }
     if (null !== $this->cache) {
         $this->cache->write($metadata);
     }
     return $this->loadedClasses[$class] = $metadata;
 }
Пример #4
0
 public function testRead()
 {
     $meta = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->setMethods(array('getClassName'))->getMock();
     $meta->expects($this->once())->method('getClassName')->will($this->returnValue('Foo\\Bar'));
     $this->assertFalse($this->cache->read('Foo\\Bar'), 'read() returns false when there is no entry');
     $this->cache->write($meta);
     $this->assertInstanceOf(ClassMetadata::class, $this->cache->read('Foo\\Bar'), 'read() returns metadata');
 }