Skip to content

Commit

Permalink
feat(resolver): cleanup methods
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
  • Loading branch information
feryardiant committed Mar 31, 2021
1 parent 59fed7b commit c63f18c
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions src/Container/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ public function handle($instance, array $args = [])
}

$params = [];
$reflector = $this->createReflection($instance);
$ref = $this->createReflection($instance);

if ($isMethod = ($reflector instanceof ReflectionMethod)) {
$params[] = $reflector->isStatic() && ! is_object($instance[0]) ? null : $instance[0];
if ($isMethod = ($ref instanceof ReflectionMethod)) {
$params[] = $ref->isStatic() && ! is_object($instance[0]) ? null : $instance[0];
}

// If it was internal method resolve its params as a closure.
// @link https://bugs.php.net/bug.php?id=50798
$toResolve = $isMethod && $reflector->getName() === '__invoke'
? new ReflectionFunction($reflector->getClosure($instance[0]))
: $reflector;
$toResolve = $isMethod && $ref->getName() === '__invoke'
? new ReflectionFunction($ref->getClosure($instance[0]))
: $ref;

try {
$params[] = $this->resolveArgs($toResolve, $args);
} catch (Exception\NotFoundException $err) {
throw new Exception\UnresolvableException($err);
}

return $reflector->invokeArgs(...$params);
return $ref->invokeArgs(...$params);
}

/**
Expand All @@ -67,16 +67,21 @@ public function handle($instance, array $args = [])
*/
public function resolve($toResolve)
{
if (is_object($toResolve)) {
return $toResolve instanceof Closure ? $toResolve : $this->injectContainer($toResolve);
if (is_string($toResolve) && ! function_exists($toResolve)) {
$toResolve = false === strpos($toResolve, '::')
? $this->createInstance($toResolve)
: explode('::', $toResolve);
}

if (is_string($toResolve) && ! function_exists($toResolve)) {
if (false === strpos($toResolve, '::')) {
return $this->createInstance($toResolve);
if (is_object($toResolve)) {
if (
$toResolve instanceof ContainerAwareInterface
&& ! $toResolve->getContainer() instanceof ContainerInterface
) {
$toResolve->setContainer($this->getContainer());
}

$toResolve = explode('::', $toResolve);
return $toResolve;
}

if (is_array($toResolve)) {
Expand Down Expand Up @@ -104,11 +109,10 @@ private function createInstance(string $className)
}

try {
$reflector = new ReflectionClass($className);

$args = ($constructor = $reflector->getConstructor()) ? $this->resolveArgs($constructor) : [];
$ref = new ReflectionClass($className);
$args = ($constructor = $ref->getConstructor()) ? $this->resolveArgs($constructor) : [];

return $this->injectContainer($reflector->newInstanceArgs($args));
return $ref->newInstanceArgs($args);
} catch (Exception\UnresolvableException $err) {
throw $err;
} catch (\Throwable $err) {
Expand Down Expand Up @@ -199,22 +203,4 @@ private function assertCallable(&$instance): bool

return is_callable($instance);
}

/**
* Injecting Container instance if $instance implements ContainerAwareInterface.
*
* @param object $instance
* @return object
*/
private function injectContainer($instance)
{
if (
$instance instanceof ContainerAwareInterface
&& ! $instance->getContainer() instanceof ContainerInterface
) {
$instance->setContainer($this->getContainer());
}

return $instance;
}
}

0 comments on commit c63f18c

Please sign in to comment.