diff --git a/CHANGELOG.md b/CHANGELOG.md index 98596a8..63a77da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## Unreleased + +- (ADD) $container->lazyLazy() to create a callable from a LazyInterface that is directly invokable. + ## 5.0.0-alpha.1 - (ADD) Inject via attributes diff --git a/docs/attributes.md b/docs/attributes.md index 0454af8..89e8e78 100644 --- a/docs/attributes.md +++ b/docs/attributes.md @@ -214,13 +214,12 @@ class Route implements AttributeConfigInterface { new RealRoute( $this->method, $this->uri, - new InvokableHandler(new LazyLazy( - $resolver, - new Lazy([ - LazyNew::fromClassName($reflector->getDeclaringClass()), + $container->lazyLazy( + $di->lazyCallable([ + $di->lazyNew($reflector->getDeclaringClass()), $reflector->getName() ]) - )) + ) ) ); } diff --git a/docs/migrating.md b/docs/migrating.md index f252719..be9cd75 100644 --- a/docs/migrating.md +++ b/docs/migrating.md @@ -24,7 +24,22 @@ There a few API changes in the objects. They probably do not have consequences f exposed publicly, but the changes are listed below nonetheless. - The `InjectionFactory` now has no dependencies. The `Resolver` is not injected anymore and the `getResolver()` and `newInstance()` methods have been removed. -- The `LazyInterface` now requires a `Resolver` to be passed to `__invoke`. Its implementations therefore also have different constructor signatures. +- The `LazyInterface` now requires a `Resolver` to be passed to `__invoke`. Its implementations therefore also have different constructor signatures. + +If you need an object that is directly invokable, without the need of passing a Resolver or any other object from the +container, use `$container->lazyLazy()` to create an invokable object that is injectable into an external method or class. + +```php +$routeHandler = $container->lazyLazy( + $di->lazyCallable([ + $di->lazyNew(OrderController::class), + 'process' + ]) +); +``` + +In the above example `$routeHandler` can be injected in any place that receives a `callable` and can be called freely +without any further dependency on `$container`. ### Dropped PHP 7 diff --git a/src/Container.php b/src/Container.php index 0439b1e..46786e5 100644 --- a/src/Container.php +++ b/src/Container.php @@ -16,6 +16,8 @@ use Aura\Di\Injection\LazyCallable; use Aura\Di\Injection\LazyGet; use Aura\Di\Injection\LazyInclude; +use Aura\Di\Injection\LazyInterface; +use Aura\Di\Injection\LazyLazy; use Aura\Di\Injection\LazyNew; use Aura\Di\Injection\LazyRequire; use Aura\Di\Injection\LazyValue; @@ -327,6 +329,21 @@ public function lazy($callable, ...$params): Lazy return $this->injectionFactory->newLazy($callable, $params); } + /** + * + * Returns a LazyLazy object that can be called directly without the requirement of a Resolver or Container. + * + * @param LazyInterface $lazy The lazy object generated by this Container. + * + * @param array $params + * + * @return LazyLazy + */ + public function lazyLazy(LazyInterface $lazy, ...$params): LazyLazy + { + return new LazyLazy($this->resolver, $lazy, $params); + } + /** * * Returns a lazy object that wraps an array that may contain diff --git a/src/Injection/LazyLazy.php b/src/Injection/LazyLazy.php index b23bfb1..908614a 100644 --- a/src/Injection/LazyLazy.php +++ b/src/Injection/LazyLazy.php @@ -38,6 +38,15 @@ class LazyLazy */ protected LazyInterface $lazy; + /** + * + * Arguments for the callable. + * + * @var array + * + */ + protected array $params; + /** * * Constructor. @@ -47,10 +56,11 @@ class LazyLazy * @param LazyInterface $lazy The service container. * */ - public function __construct(Resolver $resolver, LazyInterface $lazy) + public function __construct(Resolver $resolver, LazyInterface $lazy, array $params = []) { $this->resolver = $resolver; $this->lazy = $lazy; + $this->params = $params; } /** @@ -62,6 +72,6 @@ public function __construct(Resolver $resolver, LazyInterface $lazy) */ public function __invoke(): object { - return \call_user_func($this->lazy, $this->resolver); + return \call_user_func($this->lazy, $this->resolver, ...$this->params); } } diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 7b4e930..30d87d5 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -176,6 +176,14 @@ public function testLazyNew() $this->assertInstanceOf('Aura\Di\Fake\FakeOtherClass', $foo); } + public function testLazyLazy() + { + $lazy = $this->container->lazyNew('Aura\Di\Fake\FakeOtherClass'); + $this->assertInstanceOf('Aura\Di\Injection\LazyNew', $lazy); + $callable = $this->container->lazyLazy($lazy); + $this->assertInstanceOf('Aura\Di\Fake\FakeOtherClass', $callable()); + } + public function testLazyNewWithVariadic() { // Variadics are only available in PHP >= 5.6, and not in HHVM