Skip to content

Commit

Permalink
Merge 52cea76 into 56d605d
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Sep 8, 2013
2 parents 56d605d + 52cea76 commit ab0e552
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/DI/Factory.php
Expand Up @@ -15,7 +15,9 @@
use DI\Definition\PropertyInjection;
use Exception;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use ReflectionParameter;

/**
* Factory class, responsible of instantiating classes
Expand Down Expand Up @@ -104,18 +106,27 @@ private function injectConstructor(ReflectionClass $classReflection, MethodInjec
. "$nbRequiredParameters parameters, " . count($parameterInjections) . " defined or guessed");
}

// No parameters
if (count($parameterInjections) === 0) {
return $classReflection->newInstance();
}

$parameters = $this->getMethodReflectionParameters($constructorReflection);

$args = array();
foreach ($parameterInjections as $parameterInjection) {
$entryName = $parameterInjection->getEntryName();

if ($entryName === null) {
// If the parameter is optional and wasn't specified, then we skip all next parameters
if ($parameters[$parameterInjection->getParameterName()]->isOptional()) {
break;
}
throw new DefinitionException("The parameter '" . $parameterInjection->getParameterName()
. "' of the constructor of '{$classReflection->name}' has no type defined or guessable");
}

// TODO handle lazy injections!
$args[] = $this->container->get($entryName);
}

Expand Down Expand Up @@ -167,10 +178,17 @@ private function injectMethod($object, MethodInjection $methodInjection)
return;
}

$parameters = $this->getMethodReflectionParameters($methodReflection);

$args = array();
foreach ($parameterInjections as $parameterInjection) {
$entryName = $parameterInjection->getEntryName();

if ($entryName === null) {
// If the parameter is optional and wasn't specified, then we skip all next parameters
if ($parameters[$parameterInjection->getParameterName()]->isOptional()) {
break;
}
throw new DefinitionException("The parameter '" . $parameterInjection->getParameterName()
. "' of {$classReflection->name}::$methodName has no type defined or guessable");
}
Expand Down Expand Up @@ -221,4 +239,22 @@ private function injectProperty($object, PropertyInjection $propertyInjection)
$property->setValue($object, $value);
}

/**
* @param ReflectionMethod $reflectionMethod
* @return ReflectionParameter[]
*/
private function getMethodReflectionParameters(ReflectionMethod $reflectionMethod)
{
$parameters = $reflectionMethod->getParameters();

$keys = array_map(
function (ReflectionParameter $parameter) {
return $parameter->getName();
},
$parameters
);

return array_combine($keys, $parameters);
}

}
14 changes: 12 additions & 2 deletions tests/IntegrationTests/DI/Fixtures/Class1.php
Expand Up @@ -62,20 +62,30 @@ class Class1
/**
* @param Class2 $param1
* @param Interface1 $param2
* @throws \Exception
*/
public function __construct(Class2 $param1, Interface1 $param2)
public function __construct(Class2 $param1, Interface1 $param2, $optional = true)
{
$this->constructorParam1 = $param1;
$this->constructorParam2 = $param2;

if ($optional !== true) {
throw new \Exception("Expected optional parameter to not be defined");
}
}

/**
* @Inject
* @param Class2 $param1
* @throws \Exception
*/
public function method1(Class2 $param1)
public function method1(Class2 $param1, $optional = true)
{
$this->method1Param1 = $param1;

if ($optional !== true) {
throw new \Exception("Expected optional parameter to not be defined");
}
}

/**
Expand Down

0 comments on commit ab0e552

Please sign in to comment.