Skip to content

Commit

Permalink
Merge pull request #143 from Jagepard/wip
Browse files Browse the repository at this point in the history
handle Closure
  • Loading branch information
Jagepard committed Mar 25, 2024
2 parents ea1cd8e + 397dc97 commit 97ec2a8
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/Rudra.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,20 @@ public function get(string $key = null): mixed
}
}

$this->set([$key, $this->waiting()->get($key)]);
$waiting = $this->waiting()->get($key);

if ($waiting instanceof Closure) {
return $waiting();
}

$this->set([$key, $waiting]);
}

if (empty($key)) {
return $this->services();
}

$service = $this->services()->get($key);

return ($service instanceof Closure) ? $service() : $service;
return $this->services()->get($key);
}

/**
Expand Down Expand Up @@ -276,6 +280,11 @@ private function getParamsIoC(ReflectionMethod $constructor, ?array $params): ar
if (null !== $value->getType()?->getName() && $this->binding()->has($value->getType()->getName())) {
$className = $this->binding()->get($value->getType()->getName());

if ($className instanceof Closure) {
$paramsIoC[] = $className();
continue;
}

if (is_string($className) && str_contains($className, 'Factory')) {
$paramsIoC[] = (new $className)->create();
continue;
Expand All @@ -284,7 +293,8 @@ private function getParamsIoC(ReflectionMethod $constructor, ?array $params): ar
if (is_object($className)) {
$paramsIoC[] = $className;
} elseif ($this->waiting()->has($className)) {
$paramsIoC[] = $this->get($className);
$service = $this->get($className);
$paramsIoC[] = ($service instanceof Closure) ? $service() : $service;
} else {
$paramsIoC[] = new $className;
}
Expand Down

0 comments on commit 97ec2a8

Please sign in to comment.