Skip to content

Commit

Permalink
Adding a public method getCallableArguments to return an array of the…
Browse files Browse the repository at this point in the history
… arguments mapped to values.
  • Loading branch information
brentscheffler committed Oct 13, 2020
1 parent 262a589 commit 345aa7d
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions src/Resolve.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,10 @@ public function setContainer(ContainerInterface $container): void
*/
public function call(callable $callable, array $parameters = [])
{
if( \is_array($callable) ){
[$class, $method] = $callable;

/** @psalm-suppress ArgumentTypeCoercion */
$reflectionClass = new ReflectionClass($class);
$reflectionMethod = $reflectionClass->getMethod($method);
$reflectionParameters = $reflectionMethod->getParameters();
}

elseif( \is_object($callable) && \method_exists($callable, "__invoke")) {

$reflectionObject = new ReflectionObject($callable);
$reflectionMethod = $reflectionObject->getMethod("__invoke");
$reflectionParameters = $reflectionMethod->getParameters();
}

elseif( \is_string($callable)) {
$reflectionFunction = new ReflectionFunction($callable);
$reflectionParameters = $reflectionFunction->getParameters();
}

else {
throw new CallableResolutionException("Resolve does not have support for this type of callable.");
}

$args = $this->resolveParameters(
$reflectionParameters,
$parameters
return \call_user_func_array(
$callable,
$this->getCallableArguments($callable, $parameters)
);

return \call_user_func_array($callable, $args);
}

/**
Expand Down Expand Up @@ -108,14 +81,55 @@ public function make(string $class_name, array $parameters = []): object
return $reflectionClass->newInstance();
}

$args = $this->resolveParameters(
$args = $this->resolveReflectionParameters(
$constructor->getParameters(),
$parameters
);

return $reflectionClass->newInstanceArgs($args);
}

/**
* Given a callable, get its arguments resolved using the container and optionally any
* user supplied parameters.
*
* @param callable $callable
* @param array $parameters
* @return array<mixed>
*/
public function getCallableArguments(callable $callable, array $parameters = []): array
{
if( \is_array($callable) ){
[$class, $method] = $callable;

/** @psalm-suppress ArgumentTypeCoercion */
$reflectionClass = new ReflectionClass($class);
$reflectionMethod = $reflectionClass->getMethod($method);
$reflectionParameters = $reflectionMethod->getParameters();
}

elseif( \is_object($callable) && \method_exists($callable, "__invoke")) {

$reflectionObject = new ReflectionObject($callable);
$reflectionMethod = $reflectionObject->getMethod("__invoke");
$reflectionParameters = $reflectionMethod->getParameters();
}

elseif( \is_string($callable)) {
$reflectionFunction = new ReflectionFunction($callable);
$reflectionParameters = $reflectionFunction->getParameters();
}

else {
throw new CallableResolutionException("Resolve does not have support for this type of callable.");
}

return $this->resolveReflectionParameters(
$reflectionParameters,
$parameters
);
}

/**
* Resolve parameters.
*
Expand All @@ -124,7 +138,7 @@ public function make(string $class_name, array $parameters = []): object
* @throws ParameterResolutionException
* @return array<mixed>
*/
protected function resolveParameters(array $reflectionParameters, array $parameters = []): array
protected function resolveReflectionParameters(array $reflectionParameters, array $parameters = []): array
{
return \array_map(
/**
Expand Down

0 comments on commit 345aa7d

Please sign in to comment.