-
-
Notifications
You must be signed in to change notification settings - Fork 31
Move injector invocation into CallableFactory
#332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,30 +8,50 @@ | |
| use Psr\Container\ContainerInterface; | ||
| use ReflectionException; | ||
| use ReflectionMethod; | ||
| use Yiisoft\Injector\Injector; | ||
|
|
||
| use function is_array; | ||
| use function is_callable; | ||
| use function is_object; | ||
| use function is_string; | ||
|
|
||
| /** | ||
| * @internal Create real callable listener from configuration. | ||
| * @internal Create real callable listener from configuration, ready to be invoked with dependency injection. | ||
| */ | ||
| final class CallableFactory | ||
| { | ||
| private readonly Injector $injector; | ||
|
|
||
| public function __construct( | ||
| private readonly ContainerInterface $container, | ||
| ) {} | ||
| ?ContainerInterface $dependencyContainer = null, | ||
| ) { | ||
| $this->injector = new Injector($dependencyContainer ?? $container); | ||
| } | ||
|
|
||
| /** | ||
| * Create a real callable listener from definition. | ||
| * Create a real callable listener from definition. Calling the returned callable invokes the resolved listener, | ||
| * injecting its dependencies by type hinting. | ||
| * | ||
| * @param mixed $definition Definition to create listener from. | ||
| * | ||
| * @throws InvalidCallableConfigurationException Failed to create listener. | ||
| * @throws ContainerExceptionInterface Error while retrieving the entry from container. | ||
| * | ||
| * @psalm-return callable(mixed...): mixed | ||
| */ | ||
| public function create(mixed $definition): callable | ||
| { | ||
| $callable = $this->resolve($definition); | ||
|
|
||
| return fn(mixed ...$params): mixed => $this->injector->invoke($callable, $params); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidCallableConfigurationException Failed to resolve listener. | ||
| * @throws ContainerExceptionInterface Error while retrieving the entry from container. | ||
| */ | ||
| private function resolve(mixed $definition): callable | ||
| { | ||
| if ($definition === null) { | ||
| throw new InvalidCallableConfigurationException(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.