Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added ability to specify class injected for InjectProperty annotation

  • Loading branch information...
commit 4b3976a9d923b3d782b99596048fecc4a0e8f084 1 parent 2877f24
rodnaph rodnaph authored
11 lib/BoxUK/Inject/Annotation/InjectProperty.class.php
@@ -9,4 +9,13 @@
9 9 * @link http://github.com/boxuk/boxuk-di
10 10 * @since 1.0
11 11 */
12   -class InjectProperty extends Annotation {}
  12 +class InjectProperty extends Annotation {
  13 +
  14 + /**
  15 + * The class to inject for this property - this is optional and will
  16 + * default to the var annotation value.
  17 + * @var string
  18 + */
  19 + public $class;
  20 +
  21 +}
5 lib/BoxUK/Inject/Standard.class.php
@@ -348,7 +348,10 @@ protected function injectProperties( $object ) {
348 348 protected function injectProperty( $object, $propertyName ) {
349 349
350 350 $className = get_class( $object );
351   - $propertyClass = $this->reflector->getPropertyClass( $className, $propertyName );
  351 + $annotation = $this->reflector->getPropertyAnnotation( $className, $propertyName, self::INJECT_PROPERTY );
  352 + $propertyClass = $annotation->class
  353 + ? $annotation->class
  354 + : $this->reflector->getPropertyClass( $className, $propertyName );
352 355 $propertyValue = $this->getClass( $propertyClass );
353 356
354 357 if ( $this->reflector->isPublicProperty($className,$propertyName) ) {
8 lib/BoxUK/Reflect/Caching.class.php
@@ -247,6 +247,14 @@ public function getPropertyClass( $className, $propertyName ) {
247 247 /**
248 248 * Intercept method
249 249 */
  250 + public function getPropertyAnnotation( $className, $propertyName, $annotation ) {
  251 +
  252 + return $this->handle( 'getPropertyAnnotation', array($className,$propertyName,$annotation) );
  253 + }
  254 +
  255 + /**
  256 + * Intercept method
  257 + */
250 258 public function isPublicProperty( $className, $propertyName ) {
251 259
252 260 return $this->handle( 'isPublicProperty', array($className,$propertyName) );
11 lib/BoxUK/Reflect/Reflector.class.php
@@ -146,6 +146,17 @@ public function propertyHasAnnotation( $className, $propertyName, $annotation );
146 146 public function getPropertyClass( $className, $propertyName );
147 147
148 148 /**
  149 + * Returns the annotation for the specified property, or false
  150 + *
  151 + * @param string $className
  152 + * @param string $propertyName
  153 + * @param string $annotation
  154 + *
  155 + * @return Annotation
  156 + */
  157 + public function getPropertyAnnotation( $className, $propertyName, $annotation );
  158 +
  159 + /**
149 160 * Determines if a property is public or not. Returns true if it is public,
150 161 * or false if it's protected or private.
151 162 *
17 lib/BoxUK/Reflect/Standard.class.php
@@ -305,6 +305,23 @@ public function getPropertyClass( $className, $propertyName ) {
305 305 }
306 306
307 307 /**
  308 + * Returns a named annotation on the property
  309 + *
  310 + * @param string $className
  311 + * @param string $propertyName
  312 + * @param string $annotation
  313 + *
  314 + * @return Annotation
  315 + */
  316 + public function getPropertyAnnotation( $className, $propertyName, $annotation ) {
  317 +
  318 + $property = new ReflectionAnnotatedProperty( $className, $propertyName );
  319 +
  320 + return $property->getAnnotation( $annotation );
  321 +
  322 + }
  323 +
  324 + /**
308 325 * Indicates if a given class property is public
309 326 *
310 327 * @param string $className
12 tests/php/BoxUK/Inject/StandardTest.php
@@ -176,6 +176,12 @@ public function testInjectMethodDoesPropertyInjection() {
176 176 $this->assertInstanceOf( 'BoxUK\Inject\StandardInjectorTest_TestClass3', $class->publicProperty );
177 177 }
178 178
  179 + public function testPropertyInjectionCanHaveTheClassSpecified() {
  180 + $inject = $this->getInstance();
  181 + $class = $inject->getClass( 'BoxUK\Inject\StandardInjectorTest_TestClass7' );
  182 + $this->assertInstanceOf( 'BoxUK\Inject\StandardInjectorTest_TestClass', $class->publicPropertyWithClass );
  183 + }
  184 +
179 185 }
180 186
181 187 class StandardInjectorTest_TestClass {}
@@ -230,6 +236,12 @@ class StandardInjectorTest_TestClass7 {
230 236 public $publicProperty;
231 237
232 238 /**
  239 + * @InjectProperty(class="BoxUK\Inject\StandardInjectorTest_TestClass")
  240 + * @var BoxUK\Inject\StandardInjectorTest_TestClass3
  241 + */
  242 + public $publicPropertyWithClass;
  243 +
  244 + /**
233 245 * @InjectProperty
234 246 * @var BoxUK\Inject\StandardInjectorTest_TestClass3
235 247 */
13 tests/php/BoxUK/Reflect/StandardTest.php
@@ -65,6 +65,10 @@ public function testGettingAClassAnnotationReturnsIt() {
65 65 $this->assertEquals( 'FooBar', $annotation->implements );
66 66 }
67 67
  68 + public function testGettingAClassAnnotationReturnsFalseWhenItDoesntExist() {
  69 + $this->assertFalse( $this->reflector->getClassAnnotation( 'BoxUK\Reflect\SimpleReflectorTest_Class2', 'InjectMethod' ) );
  70 + }
  71 +
68 72 public function testIgnoredClassPatternsRespectedWhenGettingAClassesMethods() {
69 73 $this->reflector->addIgnoredClassPattern( 'Doctrine_.*' );
70 74 $methods = $this->reflector->getMethods( '\BoxUK\Reflect\ChildClass' );
@@ -134,6 +138,15 @@ public function testIspublicpropertyReturnsFalseWhenPropertyIsPrivate() {
134 138 $this->assertFalse( $this->reflector->isPublicProperty('BoxUK\Reflect\SimpleReflectorTest_Class1','private') );
135 139 }
136 140
  141 + public function testGettingAPropertyAnnotationReturnsIt() {
  142 + $annotation = $this->reflector->getPropertyAnnotation( 'BoxUK\Reflect\SimpleReflectorTest_Class1', 'public', 'InjectProperty' );
  143 + $this->assertInstanceOf( 'InjectProperty', $annotation );
  144 + }
  145 +
  146 + public function testGettingAPropertyAnnotationReturnsFalseWhenItDoesntExist() {
  147 + $this->assertFalse( $this->reflector->getPropertyAnnotation( 'BoxUK\Reflect\SimpleReflectorTest_Class1', 'public', 'InjectMethod' ) );
  148 + }
  149 +
137 150 }
138 151
139 152 class SimpleReflectorTest_Class1 {

0 comments on commit 4b3976a

Please sign in to comment.
Something went wrong with that request. Please try again.