Permalink
Browse files

Merge branch 'master' of https://github.com/boxuk/boxuk-di

  • Loading branch information...
2 parents cb9d56e + b627a25 commit 62e75d2966435791f01d1ff1d56a2264483c45f5 @rodnaph rodnaph committed Nov 29, 2010
View
@@ -24,7 +24,7 @@ The main part of the library is the DI container, a simple example to fetch a cl
$libraryLoader = $injector->getClass( 'LibraryLoader' );
</pre>
-By default, the injector will create a new class each time it's asked for it. It's constructor parameters will be analysed to check types so that any dependencies can be injected into the new object (if these dependencies don't exist they will be created). It's methods will also be checked on creation for any that have been annotated for method injection (see below)
+By default, the injector will create a new class each time it's asked for it. Its constructor parameters will be analysed to check types so that any dependencies can be injected into the new object (if these dependencies don't exist they will be created). It's methods will also be checked on creation for any that have been annotated for method injection (see below)
## Scopes
@@ -186,6 +186,14 @@ private function myMethod() {
Don't use the injector as a service locator though inside your class, always specify your dependencies to be injected at construct time.
+## Inject Arbitrary Objects
+
+The injector also provides an *inject()* method which can be used to do method injection and property injection on arbitrary objects. These objects can have been created elsewhere but the injector will scan them for dependencies to inject.
+
+<pre>
+$injector->inject( $someObject );
+</pre>
+
## The Helper
The easiest way to create an injector is to use the _Helper_ class.
@@ -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,$annotation) );
+
+ }
+
+ /**
+ * Intercept method
+ */
+ public function getPropertiesWithAnnotation( $className, $annotation ) {
+
+ return $this->handle( 'getPropertiesWithAnnotation', array($className,$annotation) );
+
+ }
+
}
@@ -50,6 +50,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
*
@@ -123,6 +133,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
*
@@ -137,6 +137,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
*
@@ -266,6 +290,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
*
@@ -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 62e75d2

Please sign in to comment.