Skip to content

Commit

Permalink
missing test more lazyLazy, add migrating text
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed May 28, 2024
1 parent 843ab24 commit f0d8e9e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 4 additions & 5 deletions docs/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
])
))
)
)
);
}
Expand Down
17 changes: 16 additions & 1 deletion docs/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions src/Injection/LazyLazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class LazyLazy
*/
protected LazyInterface $lazy;

/**
*
* Arguments for the callable.
*
* @var array
*
*/
protected array $params;

/**
*
* Constructor.
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}
8 changes: 8 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f0d8e9e

Please sign in to comment.