Beispiel #1
0
 public function testReflectionAnnotatedClass()
 {
     $reflection = new ReflectionAnnotatedClass('Example');
     $this->assertTrue($reflection->hasAnnotation('FirstAnnotation'));
     $this->assertTrue($reflection->hasAnnotation('SecondAnnotation'));
     $this->assertFalse($reflection->hasAnnotation('NonExistentAnnotation'));
     $this->assertIsA($reflection->getAnnotation('FirstAnnotation'), 'FirstAnnotation');
     $this->assertIsA($reflection->getAnnotation('SecondAnnotation'), 'SecondAnnotation');
     $annotations = $reflection->getAnnotations();
     $this->assertEqual(count($annotations), 2);
     $this->assertIsA($annotations[0], 'FirstAnnotation');
     $this->assertIsA($annotations[1], 'SecondAnnotation');
     $this->assertFalse($reflection->getAnnotation('NonExistentAnnotation'));
     $this->assertIsA($reflection->getConstructor(), 'ReflectionAnnotatedMethod');
     $this->assertIsA($reflection->getMethod('exampleMethod'), 'ReflectionAnnotatedMethod');
     foreach ($reflection->getMethods() as $method) {
         $this->assertIsA($method, 'ReflectionAnnotatedMethod');
     }
     $this->assertIsA($reflection->getProperty('exampleProperty'), 'ReflectionAnnotatedProperty');
     foreach ($reflection->getProperties() as $property) {
         $this->assertIsA($property, 'ReflectionAnnotatedProperty');
     }
     foreach ($reflection->getInterfaces() as $interface) {
         $this->assertIsA($interface, 'ReflectionAnnotatedClass');
     }
     $this->assertIsA($reflection->getParentClass(), 'ReflectionAnnotatedClass');
 }
Beispiel #2
0
 /**
  * @param type $object 
  */
 public function inject(&$object, $reflection = null)
 {
     if ($reflection == null) {
         $reflection = new \ReflectionAnnotatedClass($object);
     }
     $properties = $reflection->getProperties();
     foreach ($properties as $property) {
         if ($property->hasAnnotation("Resource")) {
             $annotation = $property->getAnnotation("Resource");
             $resource = $property->getName();
             if (!empty($annotation->name)) {
                 $resource = $annotation->name;
             }
             $property->setAccessible(true);
             $property->setValue($object, $this->context->getResource($resource));
         }
     }
     $parent = $reflection->getParentClass();
     if ($parent != null) {
         $this->inject($object, $parent);
     }
 }
 private static function prepareForSerize(\ReflectionAnnotatedClass $reflection, $object, $parent = false)
 {
     $hash = spl_object_hash($object);
     if (in_array($hash, self::$prepared) && $parent == false) {
         return;
     }
     array_push(self::$prepared, $hash);
     $globalAltered = false;
     $defaults = null;
     foreach ($reflection->getProperties() as $property) {
         $altered = false;
         $property->setAccessible(true);
         $value = $property->getValue($object);
         if (self::isTransient($property)) {
             $defaults = $defaults == null ? $reflection->getDefaultProperties() : $defaults;
             $name = $property->getName();
             $property->setValue($object, array_key_exists($name, $defaults) ? $defaults[$name] : null);
             $altered = true;
         } else {
             if (is_object($value) && $value instanceof \Closure) {
                 $property->setValue($object, new SerializableClosure($value));
                 $altered = true;
             } else {
                 if (is_object($value) && !$value instanceof SerializableClosure && spl_object_hash($value) != $hash) {
                     $valueReflection = new \ReflectionAnnotatedClass($value);
                     $altered = self::prepareForSerize($valueReflection, $value);
                 } else {
                     if (is_array($value)) {
                         $newValue = self::prepareArrayForSerialize($value);
                         if (is_array($newValue)) {
                             $property->setValue($object, $newValue);
                             $altered = true;
                         }
                     }
                 }
             }
         }
         if ($altered) {
             self::addRestore($property, $object, $value);
             $globalAltered = true;
         }
     }
     $parent = $reflection->getParentClass();
     if ($parent != null) {
         self::prepareForSerize($parent, $object, true);
     }
     if (!$parent) {
         if ($object instanceof Detachable) {
             $object->detach();
         }
     }
     return $globalAltered;
 }