/** * @return \Phalcon\Annotations\Collection */ private function getAnnotationClass() { $reader = new \Phalcon\Annotations\Reader(); $parsing = $reader->parse($this->refClass->getName()); //Create the reflection $annotations = new \Phalcon\Annotations\Reflection($parsing); return $annotations->getClassAnnotations(); }
/** * * @param \ReflectionClass $withClassReflection * @return string */ public static function getTypeHash(\ReflectionClass $withClassReflection = null) { $ref = $withClassReflection ? $withClassReflection : static::getReflection(); $props = $ref->getProperties(); //Parse the annotations in a class $reader = new \Phalcon\Annotations\Reader(); $parsing = $reader->parse($ref->getName()); //Create the reflection $annotations = new \Phalcon\Annotations\Reflection($parsing); $text = $annotations->getClassAnnotations()->has('cached') ? "X" : "0"; foreach ($props as $prop) { $text .= $prop->getName(); //.($prop->hasAnnotation('lazy')?"M":"0"); } //\Nette\Diagnostics\Debugger::barDump(array('hash:'=>$text), "HFOR: ".$ref->getName()); return md5($text); }
public function testInvalidAnnotation() { $reader = new Phalcon\Annotations\Reader(); try { $parsing = $reader->parse('TestInvalid'); $this->assertFalse(); } catch (\Exception $e) { $this->assertRegExp('/Syntax error, unexpected EOF in .*/', $e->getMessage()); } }
<?php //Parse the annotations in a class $reader = new \Phalcon\Annotations\Reader(); $parsing = $reader->parse('MyComponent'); //Create the reflection $reflection = new \Phalcon\Annotations\Reflection($parsing); //Get the annotations in the class docblock $classAnnotations = $reflection->getClassAnnotations();
public function testReflection() { //Empty reflection $reflection = new Phalcon\Annotations\Reflection(); $classAnnotations = $reflection->getClassAnnotations(); $this->assertEquals($classAnnotations, null); $methodsAnnotations = $reflection->getMethodsAnnotations(); $this->assertEquals($methodsAnnotations, null); $propertiesAnnotations = $reflection->getPropertiesAnnotations(); $this->assertEquals($propertiesAnnotations, null); //Parsing a real class $reader = new Phalcon\Annotations\Reader(); $parsing = $reader->parse('TestClass'); $reflection = new Phalcon\Annotations\Reflection($parsing); //Annotations in the class' dockblock $classAnnotations = $reflection->getClassAnnotations(); $this->assertEquals(get_class($classAnnotations), 'Phalcon\\Annotations\\Collection'); $number = 0; foreach ($classAnnotations as $annotation) { $this->assertEquals(get_class($annotation), 'Phalcon\\Annotations\\Annotation'); $number++; } $this->assertEquals($number, 9); $this->assertEquals(count($classAnnotations), 9); //Annotations in Methods $methodsAnnotations = $reflection->getMethodsAnnotations(); $this->assertEquals(gettype($methodsAnnotations), 'array'); $this->assertEquals(get_class($methodsAnnotations['testMethod1']), 'Phalcon\\Annotations\\Collection'); $total = 0; foreach ($methodsAnnotations as $method => $annotations) { $this->assertEquals(gettype($method), 'string'); $number = 0; foreach ($annotations as $annotation) { $this->assertEquals(get_class($annotation), 'Phalcon\\Annotations\\Annotation'); $number++; $total++; } $this->assertTrue($number > 0); } $this->assertEquals($total, 14); $annotations = $methodsAnnotations['testMethod1']; $this->assertTrue($annotations->has('Simple')); $this->assertFalse($annotations->has('NoSimple')); $annotation = $annotations->get('Simple'); $this->assertEquals($annotation->getName(), 'Simple'); $this->assertEquals($annotation->numberArguments(), 0); $this->assertEquals($annotation->getArguments(), null); $this->assertFalse($annotation->hasArgument('none')); $annotation = $annotations->get('NamedMultipleParams'); $this->assertEquals($annotation->getName(), 'NamedMultipleParams'); $this->assertEquals($annotation->numberArguments(), 2); $this->assertEquals($annotation->getArguments(), array('first' => 'First', 'second' => 'Second')); $this->assertTrue($annotation->hasArgument('first')); $this->assertEquals($annotation->getArgument('first'), 'First'); $this->assertFalse($annotation->hasArgument('none')); //Annotations in properties $propertiesAnnotations = $reflection->getPropertiesAnnotations(); $this->assertEquals(gettype($propertiesAnnotations), 'array'); $this->assertEquals(get_class($propertiesAnnotations['testProp1']), 'Phalcon\\Annotations\\Collection'); $total = 0; foreach ($propertiesAnnotations as $property => $annotations) { $this->assertEquals(gettype($method), 'string'); $number = 0; foreach ($annotations as $annotation) { $this->assertEquals(get_class($annotation), 'Phalcon\\Annotations\\Annotation'); $number++; $total++; } $this->assertTrue($number > 0); } $this->assertEquals($total, 10); }