Skip to content

Commit

Permalink
The ability to inject the object through the parameters by name
Browse files Browse the repository at this point in the history
  • Loading branch information
sapsan4eg committed Jun 9, 2016
1 parent 442edc0 commit c434df2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
28 changes: 26 additions & 2 deletions src/Inject.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static function method($className, $methodName, array $parameters = null)

throw new InjectException("Inject error: class " . $className . " not exist.");
}

$classCheck = new \ReflectionClass($className);

if ('__construct' == $methodName) {
Expand All @@ -117,13 +118,15 @@ protected static function getParameters(\ReflectionMethod $method, array $parame

try {
foreach ($method->getParameters() as $parameter) {
if (self::sameParameter($parameter, $parameters)) {
if (self::canBeInjectedParameter($parameter, $parameters)) {
$arguments[$parameter->getName()] = self::instantiation($parameters[$parameter->getName()]);
} elseif (self::sameParameter($parameter, $parameters)) {
$arguments[$parameter->getName()] = $parameters[$parameter->getName()];
} elseif (null != $parameter->getClass() && self::container()->isInjected($parameter->getClass()->name, $method->getDocComment())) {
$arguments[$parameter->getName()] = self::instantiation(self::container()->getServiceName($parameter->getClass()->name, $method->getDocComment(), $parameter->getName()));
} elseif (self::container()->isInstantiate($parameter->getClass())) {
$arguments[$parameter->getName()] = self::instantiation($parameter->getClass()->name);
} elseif (true != $parameter->isOptional()) {
} elseif (true != $parameter->isOptional()) {
throw new InjectRequiredParameterException("Inject error: required parameter [" . $parameter->getName() . "] in " . $method->getDeclaringClass()->name . "::" . $method->getName() . " is not specified.");
}
}
Expand Down Expand Up @@ -160,6 +163,27 @@ protected static function sameParameter(\ReflectionParameter $parameter, $parame
return true;
}

protected static function canBeInjectedParameter(\ReflectionParameter $parameter, $parameters)
{
if (null != $parameter->getClass()) {
return false;
}

if (!isset($parameters[$parameter->getName()])) {
return false;
}

if (!is_string($parameters[$parameter->getName()])) {
return false;
}

if (class_exists($parameters[$parameter->getName()]) || interface_exists($parameters[$parameter->getName()])) {
return true;
}

return false;
}

/**
* @param object $class
* @param array|null $properties
Expand Down
8 changes: 8 additions & 0 deletions tests/InjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ public function testCheckSingle()
$this->assertInstanceOf('Single', $class->getSingle());
$this->assertInstanceOf('SecondSingle', $class->getSecond());
}

public function testCheckInjectClassLikeParameter()
{
Inject::bind('InjectClassParameter', ['default' => ['name' => 'InjectClassParameter', 'parameters' => ['param' => 'Next']]]);
$class = Inject::instantiation("InjectClassParameter");
#$this->assertEquals('ChildClass', $class->getClass());
$this->assertInstanceOf('Next', $class->getClass());
}
}
25 changes: 7 additions & 18 deletions tests/TestClasses/Classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,16 @@ public function getSecond()
}
}

interface DecoratorInterface
class InjectClassParameter
{
public function helloWorld();
}

class TestDecorator implements DecoratorInterface
{
protected $decorator;

/**
* TestDecorator constructor.
* @param DecoratorInterface $decorator
* @emptyDecoratorPlease
*/
public function __construct(DecoratorInterface $decorator)
protected $class;
public function __construct($param = null)
{
$this->decorator = $decorator;
$this->class = $param;
}

public function helloWorld()
public function getClass()
{
$this->decorator->helloWorld();
return $this->class;
}
}
}

0 comments on commit c434df2

Please sign in to comment.