public function loadMetadataForClass($className)
 {
     $metadata = new ClassMetadata($className);
     $reflectionClass = new \ReflectionClass($className);
     $reflectionProperties = $reflectionClass->getProperties();
     foreach ($reflectionProperties as $reflectionProperty) {
         $name = $reflectionProperty->getName();
         $pMetadata = new PropertyMetadata($name);
         $annotations = $this->reader->getPropertyAnnotations($reflectionProperty);
         foreach ($annotations as $annotation) {
             if ($annotation instanceof Annotations\Expose) {
                 $pMetadata->setExpose((bool) $annotation->value);
             } elseif ($annotation instanceof Annotations\Type) {
                 $pMetadata->setType((string) $annotation->value);
             } elseif ($annotation instanceof Annotations\Groups) {
                 $pMetadata->setGroups($annotation->value);
             } elseif ($annotation instanceof Annotations\SerializedName) {
                 $pMetadata->setSerializedName((string) $annotation->value);
             } elseif ($annotation instanceof Annotations\SinceVersion) {
                 $pMetadata->setSinceVersion((string) $annotation->value);
             } elseif ($annotation instanceof Annotations\UntilVersion) {
                 $pMetadata->setUntilVersion((string) $annotation->value);
             }
         }
         $metadata->addPropertyMetadata($pMetadata);
     }
     return $metadata;
 }
Пример #2
0
 /**
  * {@inheritDoc}
  *
  * @param string $className
  * @param string $file
  * @return null|ClassMetadata
  * @throws RuntimeException
  */
 protected function loadMetadataFromFile($className, $file)
 {
     $config = Yaml::parse(file_get_contents($file));
     if (!isset($config[$name = $className])) {
         throw new RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $className, $file));
     }
     $config = $config[$name];
     $metadata = new ClassMetadata($name);
     $metadata->addFileResource($file);
     foreach ($config['properties'] as $propertyName => $propertyOptions) {
         $pMetadata = new PropertyMetadata($propertyName);
         if (isset($propertyOptions['expose'])) {
             $pMetadata->setExpose((bool) $propertyOptions['expose']);
         }
         if (isset($propertyOptions['serialized_name'])) {
             $pMetadata->setSerializedName((string) $propertyOptions['serialized_name']);
         }
         if (isset($propertyOptions['since_version'])) {
             $pMetadata->setSinceVersion((string) $propertyOptions['since_version']);
         }
         if (isset($propertyOptions['until_version'])) {
             $pMetadata->setUntilVersion((string) $propertyOptions['until_version']);
         }
         if (isset($propertyOptions['type'])) {
             $pMetadata->setType((string) $propertyOptions['type']);
         }
         if (isset($propertyOptions['groups'])) {
             $pMetadata->setGroups($propertyOptions['groups']);
         }
         $metadata->addPropertyMetadata($pMetadata);
     }
     return $metadata;
 }
Пример #3
0
 /**
  * @depends testPutClassMetadataInCache
  */
 public function testRemoveClassMetadataFromCache()
 {
     $metadata = new ClassMetadata('test');
     $metadata->addFileResource(__FILE__);
     $this->unitUderTest->putClassMetadataInCache($metadata);
     $this->unitUderTest->removeClassMetadataFromCache('test');
     $this->assertFileNotExists($this->cacheDir . '/test.cache.php');
 }
 public function testAddClassMetadata()
 {
     $metadata = new ClassMetadata('testClass');
     sleep(2);
     $unitUnderTest = new ClassHierarchyMetadata();
     $property = new PropertyMetadata('property');
     $property->setType('string');
     $metadata->addFileResource('/tmp/fileNotExists')->addPropertyMetadata($property);
     $unitUnderTest->addFileResource('/tmp/fileNotExists2');
     $property = new PropertyMetadata('property2');
     $property->setType('integer');
     $unitUnderTest->addPropertyMetadata($property);
     $unitUnderTest->addClassMetadata($metadata);
     $this->assertEquals($metadata->getName(), $unitUnderTest->getName());
     $this->assertEquals($metadata->getCreatedAt(), $unitUnderTest->getCreatedAt());
     $this->assertCount(2, $unitUnderTest->getFileResources());
     $this->assertCount(2, $unitUnderTest->getProperties());
     $this->assertEquals(array('/tmp/fileNotExists2', '/tmp/fileNotExists'), $unitUnderTest->getFileResources());
     $this->assertArrayHasKey('property', $unitUnderTest->getProperties());
     $this->assertArrayHasKey('property2', $unitUnderTest->getProperties());
 }
 /**
  * @depends testConstructor
  * @depends testSettersGetters
  */
 public function testSerializeUnserialize()
 {
     $unitUnderTest = new ClassMetadata('testClass');
     $property = new PropertyMetadata('property');
     $property->setType('string');
     $unitUnderTest->addFileResource('/tmp/fileNotExists')->addPropertyMetadata($property);
     $serializedString = serialize($unitUnderTest);
     $unserializedObject = unserialize($serializedString);
     $this->assertInstanceOf('Opensoft\\SimpleSerializer\\Metadata\\ClassMetadata', $unserializedObject);
     $this->assertEquals('testClass', $unserializedObject->getName());
     $this->assertNotNull($unserializedObject->getCreatedAt());
     $fileResources = $unserializedObject->getFileResources();
     $this->assertCount(1, $fileResources);
     $this->assertEquals('/tmp/fileNotExists', $fileResources[0]);
     $properties = $unserializedObject->getProperties();
     $this->assertCount(1, $properties);
     $this->assertArrayHasKey('property', $properties);
     $this->assertInstanceOf('Opensoft\\SimpleSerializer\\Metadata\\PropertyMetadata', $properties['property']);
     $this->assertEquals('property', $properties['property']->getName());
     $this->assertEquals('string', $properties['property']->getType());
 }
Пример #6
0
 /**
  * @param ClassMetadata $metadata
  */
 public function putClassMetadataInCache(ClassMetadata $metadata)
 {
     $path = $this->dir . '/' . strtr($metadata->getName(), '\\', '-') . '.cache.php';
     file_put_contents($path, '<?php return unserialize(' . var_export(serialize($metadata), true) . ');');
 }