Skip to content

Commit

Permalink
Tidied generic method caller.
Browse files Browse the repository at this point in the history
Typed arguments passed as strings are resolved.
  • Loading branch information
ekowabaka committed Sep 3, 2017
1 parent fcc7b04 commit 714c382
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,29 @@ public function resolve($type, $constructorArguments = [])

foreach($resolvedClass['calls'] ?? [] as $method => $parameters) {
$method = new \ReflectionMethod($instance, $method);
$method->invokeArgs($instance, $this->getConstructorArguments($method, $parameters));
$method->invokeArgs($instance, $this->getMethodArguments($method, $parameters));
}

return $instance;
}

private function getConstructorArguments($constructor, $constructorArguments)
private function resolveArgument($argument, $class)
{
if($class && is_string($argument)) {
return $this->resolve($argument);
}
return $argument;
}

private function getMethodArguments($method, $methodArguments)
{
$argumentValues = [];
$parameters = $constructor->getParameters();
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
$class = $parameter->getClass();
$className = $class ? $class->getName() : null;
if (isset($constructorArguments[$parameter->getName()])) {
$argumentValues[] = $constructorArguments[$parameter->getName()];
if (isset($methodArguments[$parameter->getName()])) {
$argumentValues[] = $this->resolveArgument($methodArguments[$parameter->getName()], $className);
} else {
$argumentValues[] = $className ? $this->resolve($className) : null;
}
Expand Down Expand Up @@ -109,6 +117,6 @@ private function getInstance($className, $constructorArguments = [])
);
}
$constructor = $reflection->getConstructor();
return $reflection->newInstanceArgs($constructor ? $this->getConstructorArguments($constructor, $constructorArguments) : []);
return $reflection->newInstanceArgs($constructor ? $this->getMethodArguments($constructor, $constructorArguments) : []);
}
}

0 comments on commit 714c382

Please sign in to comment.