Skip to content

Commit

Permalink
added new inject method which can take an arbitrary object and does m…
Browse files Browse the repository at this point in the history
…ethod and property injection on it
  • Loading branch information
Box UK committed Nov 18, 2010
1 parent 5b18e22 commit 1dc5b1b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/BoxUK/Inject/Injector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@ public function getScope( $name );
* @param object $object
*/
public function checkScope( $object );

/**
* Do method and property injection on the specified object
*
* @param object $object
*/
public function inject( $object );

}
19 changes: 15 additions & 4 deletions lib/BoxUK/Inject/Standard.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,25 @@ public function checkScope( $object ) {
*/
public function getNewClass( $className ) {

$oClass = $this->hasInjectableConstructor( $className )
$object = $this->hasInjectableConstructor( $className )
? $this->getInstance( $className )
: new $className();

$this->injectMethods( $oClass );
$this->injectProperties( $oClass );
$this->inject( $object );

return $oClass;
return $object;

}

/**
* Do method and property injection on the specified object
*
* @param object $object
*/
public function inject( $object ) {

$this->injectMethods( $object );
$this->injectProperties( $object );

}

Expand Down
14 changes: 14 additions & 0 deletions tests/php/BoxUK/Inject/StandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ public function testPrivatePropertiesCanBeInjected() {
$this->assertInstanceOf( 'BoxUK\Inject\StandardInjectorTest_TestClass3', $class->getPrivateProperty() );
}

public function testInjectMethodDoesMethodInjection() {
$inject = $this->getInstance();
$class = new StandardInjectorTest_TestModel1();
$inject->inject( $class );
$this->assertInstanceOf( 'BoxUK\Inject\StandardInjectorTest_TestClass3', $class->object );
}

public function testInjectMethodDoesPropertyInjection() {
$inject = $this->getInstance();
$class = new StandardInjectorTest_TestClass7();
$inject->inject( $class );
$this->assertInstanceOf( 'BoxUK\Inject\StandardInjectorTest_TestClass3', $class->publicProperty );
}

}

class StandardInjectorTest_TestClass {}
Expand Down

0 comments on commit 1dc5b1b

Please sign in to comment.