Permalink
Browse files

added new reflector methods to only fetch methods and properties with…

… an annotation so these can be cached and the injector no longer has to check methods/properties that dont have annotations
  • Loading branch information...
1 parent 7306136 commit 72ff4feac998d8ae790c3efb53ecda2ca0242882 @rodnaph rodnaph committed Nov 21, 2010
@@ -297,12 +297,10 @@ protected function getParamClassFromAnnotation( $className, $methodName, $paramN
protected function injectMethods( $object ) {
$className = get_class( $object );
- $methods = $this->reflector->getMethods( $className );
+ $methods = $this->reflector->getMethodsWithAnnotation( $className, self::INJECT_METHOD );
foreach ( $methods as $methodName ) {
- if ( $this->reflector->methodHasAnnotation($className,$methodName,self::INJECT_METHOD) ) {
- $this->injectMethod( $object, $methodName );
- }
+ $this->injectMethod( $object, $methodName );
}
}
@@ -329,12 +327,10 @@ protected function injectMethod( $object, $methodName ) {
protected function injectProperties( $object ) {
$className = get_class( $object );
- $properties = $this->reflector->getProperties( $className );
+ $properties = $this->reflector->getPropertiesWithAnnotation( $className, self::INJECT_PROPERTY );
foreach ( $properties as $propertyName ) {
- if ( $this->reflector->propertyHasAnnotation($className,$propertyName,self::INJECT_PROPERTY) ) {
- $this->injectProperty( $object, $propertyName );
- }
+ $this->injectProperty( $object, $propertyName );
}
}
@@ -261,4 +261,22 @@ public function isPublicProperty( $className, $propertyName ) {
}
+ /**
+ * Intercept method
+ */
+ public function getMethodsWithAnnotation( $className, $annotation ) {
+
+ return $this->handle( 'getMethodsWithAnnotation', array($className,$propertyName) );
+
+ }
+
+ /**
+ * Intercept method
+ */
+ public function getPropertiesWithAnnotation( $className, $annotation ) {
+
+ return $this->handle( 'getPropertiesWithAnnotation', array($className,$propertyName) );
+
+ }
+
}
@@ -51,6 +51,16 @@ public function getMethodParams( $className, $methodName );
public function getMethods( $className );
/**
+ * Return all methods from a class that have a specified annotation
+ *
+ * @param string $className
+ * @param string $annotation
+ *
+ * @return array
+ */
+ public function getMethodsWithAnnotation( $className, $annotation );
+
+ /**
* Indicates if a class has the specified annotation
*
* @param string $className
@@ -124,6 +134,16 @@ public function addIgnoredClassPattern( $regex );
public function getProperties( $className );
/**
+ * Return all class properties that have a specific annotation
+ *
+ * @param string $className
+ * @param string $annotation
+ *
+ * @return array
+ */
+ public function getPropertiesWithAnnotation( $className, $annotation );
+
+ /**
* Indicates if a property has a particular annotation
*
* @param string $className
@@ -138,6 +138,30 @@ public function getMethods( $className ) {
}
/**
+ * Returns the classes methods with the specified annotation
+ *
+ * @param string $className
+ * @param string $annotation
+ *
+ * @return array
+ */
+ public function getMethodsWithAnnotation( $className, $annotation ) {
+
+ $methods = $this->getMethods( $className );
+ $methodsWithAnnotation = array();
+
+ foreach ( $methods as $methodName ) {
+ $method = new ReflectionAnnotatedMethod( $className, $methodName );
+ if ( $method->hasAnnotation($annotation) ) {
+ $methodsWithAnnotation[] = $methodName;
+ }
+ }
+
+ return $methodsWithAnnotation;
+
+ }
+
+ /**
* Indicates if the class name matches one of the ignored patterns
*
* @param string $className
@@ -267,6 +291,29 @@ public function getProperties( $className ) {
}
/**
+ * Returns all the classes properties that have the specified annotation
+ *
+ * @param string $className
+ * @param string $annotation
+ *
+ * @return array
+ */
+ public function getPropertiesWithAnnotation( $className, $annotation ) {
+
+ $properties = $this->getProperties( $className );
+ $propertiesWithAnnotation = array();
+
+ foreach ( $properties as $propertyName ) {
+ if ( $this->propertyHasAnnotation($className,$propertyName,$annotation) ) {
+ $propertiesWithAnnotation[] = $propertyName;
+ }
+ }
+
+ return $propertiesWithAnnotation;
+
+ }
+
+ /**
* Indicates if a classes property has the specified annotation
*
* @param string $className
@@ -147,6 +147,16 @@ public function testGettingAPropertyAnnotationReturnsFalseWhenItDoesntExist() {
$this->assertFalse( $this->reflector->getPropertyAnnotation( 'BoxUK\Reflect\SimpleReflectorTest_Class1', 'public', 'InjectMethod' ) );
}
+ public function testGetmethodswithannotationReturnsOnlyTheMethodsThatHaveThatAnnotation() {
+ $methods = $this->reflector->getMethodsWithAnnotation( 'BoxUK\Reflect\Doctrine_Class', 'InjectMethod' );
+ $this->assertEquals( 1, count($methods) );
+ }
+
+ public function testGetpropertieswithannotationReturnsOnlyThePropertiesThatHaveThatAnnotation() {
+ $properties = $this->reflector->getPropertiesWithAnnotation( 'BoxUK\Reflect\SimpleReflectorTest_Class1', 'InjectProperty' );
+ $this->assertEquals( 2, count($properties) );
+ }
+
}
class SimpleReflectorTest_Class1 {
@@ -184,6 +194,7 @@ class Doctrine_Class {
* @InjectMethod
*/
public function docFoo() {}
+ public function anotherMethod() {}
}
class ChildClass extends Doctrine_Class {

0 comments on commit 72ff4fe

Please sign in to comment.