Permalink
Browse files

added ability to specify class injected for InjectProperty annotation

  • Loading branch information...
1 parent 2877f24 commit 4b3976a9d923b3d782b99596048fecc4a0e8f084 @rodnaph rodnaph committed Nov 19, 2010
View
11 lib/BoxUK/Inject/Annotation/InjectProperty.class.php
@@ -9,4 +9,13 @@
* @link http://github.com/boxuk/boxuk-di
* @since 1.0
*/
-class InjectProperty extends Annotation {}
+class InjectProperty extends Annotation {
+
+ /**
+ * The class to inject for this property - this is optional and will
+ * default to the var annotation value.
+ * @var string
+ */
+ public $class;
+
+}
View
5 lib/BoxUK/Inject/Standard.class.php
@@ -348,7 +348,10 @@ protected function injectProperties( $object ) {
protected function injectProperty( $object, $propertyName ) {
$className = get_class( $object );
- $propertyClass = $this->reflector->getPropertyClass( $className, $propertyName );
+ $annotation = $this->reflector->getPropertyAnnotation( $className, $propertyName, self::INJECT_PROPERTY );
+ $propertyClass = $annotation->class
+ ? $annotation->class
+ : $this->reflector->getPropertyClass( $className, $propertyName );
$propertyValue = $this->getClass( $propertyClass );
if ( $this->reflector->isPublicProperty($className,$propertyName) ) {
View
8 lib/BoxUK/Reflect/Caching.class.php
@@ -247,6 +247,14 @@ public function getPropertyClass( $className, $propertyName ) {
/**
* Intercept method
*/
+ public function getPropertyAnnotation( $className, $propertyName, $annotation ) {
+
+ return $this->handle( 'getPropertyAnnotation', array($className,$propertyName,$annotation) );
+ }
+
+ /**
+ * Intercept method
+ */
public function isPublicProperty( $className, $propertyName ) {
return $this->handle( 'isPublicProperty', array($className,$propertyName) );
View
11 lib/BoxUK/Reflect/Reflector.class.php
@@ -146,6 +146,17 @@ public function propertyHasAnnotation( $className, $propertyName, $annotation );
public function getPropertyClass( $className, $propertyName );
/**
+ * Returns the annotation for the specified property, or false
+ *
+ * @param string $className
+ * @param string $propertyName
+ * @param string $annotation
+ *
+ * @return Annotation
+ */
+ public function getPropertyAnnotation( $className, $propertyName, $annotation );
+
+ /**
* Determines if a property is public or not. Returns true if it is public,
* or false if it's protected or private.
*
View
17 lib/BoxUK/Reflect/Standard.class.php
@@ -305,6 +305,23 @@ public function getPropertyClass( $className, $propertyName ) {
}
/**
+ * Returns a named annotation on the property
+ *
+ * @param string $className
+ * @param string $propertyName
+ * @param string $annotation
+ *
+ * @return Annotation
+ */
+ public function getPropertyAnnotation( $className, $propertyName, $annotation ) {
+
+ $property = new ReflectionAnnotatedProperty( $className, $propertyName );
+
+ return $property->getAnnotation( $annotation );
+
+ }
+
+ /**
* Indicates if a given class property is public
*
* @param string $className
View
12 tests/php/BoxUK/Inject/StandardTest.php
@@ -176,6 +176,12 @@ public function testInjectMethodDoesPropertyInjection() {
$this->assertInstanceOf( 'BoxUK\Inject\StandardInjectorTest_TestClass3', $class->publicProperty );
}
+ public function testPropertyInjectionCanHaveTheClassSpecified() {
+ $inject = $this->getInstance();
+ $class = $inject->getClass( 'BoxUK\Inject\StandardInjectorTest_TestClass7' );
+ $this->assertInstanceOf( 'BoxUK\Inject\StandardInjectorTest_TestClass', $class->publicPropertyWithClass );
+ }
+
}
class StandardInjectorTest_TestClass {}
@@ -230,6 +236,12 @@ class StandardInjectorTest_TestClass7 {
public $publicProperty;
/**
+ * @InjectProperty(class="BoxUK\Inject\StandardInjectorTest_TestClass")
+ * @var BoxUK\Inject\StandardInjectorTest_TestClass3
+ */
+ public $publicPropertyWithClass;
+
+ /**
* @InjectProperty
* @var BoxUK\Inject\StandardInjectorTest_TestClass3
*/
View
13 tests/php/BoxUK/Reflect/StandardTest.php
@@ -65,6 +65,10 @@ public function testGettingAClassAnnotationReturnsIt() {
$this->assertEquals( 'FooBar', $annotation->implements );
}
+ public function testGettingAClassAnnotationReturnsFalseWhenItDoesntExist() {
+ $this->assertFalse( $this->reflector->getClassAnnotation( 'BoxUK\Reflect\SimpleReflectorTest_Class2', 'InjectMethod' ) );
+ }
+
public function testIgnoredClassPatternsRespectedWhenGettingAClassesMethods() {
$this->reflector->addIgnoredClassPattern( 'Doctrine_.*' );
$methods = $this->reflector->getMethods( '\BoxUK\Reflect\ChildClass' );
@@ -134,6 +138,15 @@ public function testIspublicpropertyReturnsFalseWhenPropertyIsPrivate() {
$this->assertFalse( $this->reflector->isPublicProperty('BoxUK\Reflect\SimpleReflectorTest_Class1','private') );
}
+ public function testGettingAPropertyAnnotationReturnsIt() {
+ $annotation = $this->reflector->getPropertyAnnotation( 'BoxUK\Reflect\SimpleReflectorTest_Class1', 'public', 'InjectProperty' );
+ $this->assertInstanceOf( 'InjectProperty', $annotation );
+ }
+
+ public function testGettingAPropertyAnnotationReturnsFalseWhenItDoesntExist() {
+ $this->assertFalse( $this->reflector->getPropertyAnnotation( 'BoxUK\Reflect\SimpleReflectorTest_Class1', 'public', 'InjectMethod' ) );
+ }
+
}
class SimpleReflectorTest_Class1 {

0 comments on commit 4b3976a

Please sign in to comment.