Skip to content

Commit

Permalink
added new injection feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Jul 14, 2014
1 parent 7c3715a commit d9950a6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/Injector.php
Expand Up @@ -22,8 +22,12 @@ function process($instance, $parameters)
// injection contains class injection
$injection = array();

foreach ($inspector->getClassInjection($class) as $name => $value) {
$injection[$name] = $this->getManager()->get($value);
foreach ($inspector->getClassInjection($class) as $name => $inject) {
if($inject['new']) {
$injection[$name] = $this->getManager()->create($inject['class']);
} else {
$injection[$name] = $this->getManager()->get($inject['class']);
}
}

$properties = $inspector->getClassProperties($class);
Expand Down
7 changes: 5 additions & 2 deletions src/Inspector.php
Expand Up @@ -108,7 +108,7 @@ public function getClassInjection($class)
$injection = array();
$reflectionClass = Reflection::getReflectionClass($class);
foreach ($reflectionClass->getProperties() as $property) {
if (stristr($property->getDocComment(), '@inject')) {
if (stristr($property->getDocComment(), '@inject') || stristr($property->getDocComment(), '@new')) {
foreach (explode("\n", $property->getDocComment()) as $line) {
if (!stristr($line, '@var')) {
continue;
Expand Down Expand Up @@ -156,7 +156,10 @@ public function getClassInjection($class)
}
}
}
$injection[$property->getName()] = $injected_class;
$injection[$property->getName()] = array(
'class' => $injected_class,
'new' => stristr($property->getDocComment(), '@new')
);
break;
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/Manager.php
Expand Up @@ -13,6 +13,11 @@ class Manager
*/
protected $instance = array();

/**
* @var array
*/
protected $instances = array();

/**
* @var array
*/
Expand Down Expand Up @@ -89,6 +94,26 @@ function getConfigureAllProperties()
return $this->configureAllProperties;
}

/**
* @param string $class
* @return mixed
* @throws Exception
*/
public function getInstance($class)
{
return $this->get($class);
}

/**
* @param string $class
* @return array
* @throws Exception
*/
public function getInstances($class)
{
return isset($this->instances[$class]) ? $this->instances[$class] : array();
}

/**
* @param string $class
* @return mixed
Expand Down Expand Up @@ -188,6 +213,12 @@ protected function createInstance($class, $config = array())
$this->getInjector()->process($instance, $parameters);
}

if (!isset($this->instances[$class])) {
$this->instances[$class] = array();
}

$this->instances[$class][] = $instance;

return $instance;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Common/InjectNew.php
@@ -0,0 +1,19 @@
<?php

namespace Common;

class InjectNew
{
/**
* @new
* @var Module
*/
public $module1;

/**
* @new
* @var Module
*/
public $module2;

}
24 changes: 24 additions & 0 deletions tests/NewTests.php
@@ -0,0 +1,24 @@
<?php

use Common\Application;
use Common\Module;
use Cti\Di\Configuration;
use Cti\Di\Manager;
use Cti\Di\Reference;

class NewTests extends PHPUnit_Framework_TestCase
{
function testNew()
{
$manager = new Manager;
$new = $manager->get('Common\InjectNew');
$this->assertInstanceOf('Common\InjectNew', $new);

$this->assertInstanceOf('Common\Module', $new->module1);
$this->assertInstanceOf('Common\Module', $new->module2);

$this->assertNotSame($new->module1, $new->module2);

$this->assertSame($manager->getInstances('Common\Module'), array($new->module1, $new->module2));
}
}
2 changes: 1 addition & 1 deletion tests/resources/configuration.php
@@ -1,2 +1,2 @@
<?php
<?php
return array();

0 comments on commit d9950a6

Please sign in to comment.