Skip to content

Commit

Permalink
change signature to resolve(callable)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed May 18, 2023
1 parent d55eb0c commit e096e7e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Unreleased

- Accept `Closure|string` in `getParametersToResolve()`
- Added `resolve(Closure)` to `ContainerInterface`
- Added `resolve(callable)` to `ContainerInterface`

## 0.4.0
### 2023-04-27
Expand Down
9 changes: 5 additions & 4 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,20 @@ public function get(string $id): mixed
return $this->createInstance($id);
}

public function resolve(Closure $closure): mixed
public function resolve(callable $callable): mixed
{
$reflectionFn = new ReflectionFunction($closure);
$callable = Closure::fromCallable($callable);
$reflectionFn = new ReflectionFunction($callable);
$callableKey = md5(serialize($reflectionFn->__toString()));

if (!isset($this->cachedDependencies[$callableKey])) {
$this->cachedDependencies[$callableKey] = $this
->getDependencyResolver()
->resolveDependencies($closure);
->resolveDependencies($callable);
}

/** @psalm-suppress MixedMethodCall */
return $closure(...$this->cachedDependencies[$callableKey]);
return $callable(...$this->cachedDependencies[$callableKey]);
}

public function factory(Closure $instance): Closure
Expand Down
2 changes: 1 addition & 1 deletion src/Container/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function get(string $id): mixed;
/**
* Resolve the closure loading automatically all arguments based on current bindings.
*/
public function resolve(Closure $closure): mixed;
public function resolve(callable $callable): mixed;

/**
* Check if an instance exists.
Expand Down

0 comments on commit e096e7e

Please sign in to comment.