auraphp / Aura.Di

Dependency Injection System
MIT License
349 stars 63 forks source link

Add a LazyLazy object #220

Closed frederikbosch closed 4 months ago

frederikbosch commented 4 months ago

Compared to Aura/Di version 4, all LazyInterface instances were directly invokable. This has changed in version 5. Now, LazyInterface requires to be passed a Resolver.

This has two advantages:

  1. You can instantiate a LazyInterface without needing the Resolver or the Container. This was required to implement the attributes #[Service], #[Instance] and #[Value].
  2. It reduces circular references when serializing.

But, sometimes, you do need a directly invokable object, e.g. when creating a route handler. Such an object needs to be invokable without passing the Resolver as a parameter. This is what the new LazyLazy solves. It wraps a LazyInterface with the Resolver, making the object directly invokable.

$routeHandler = $di->lazyLazy(
    $di->lazyCallable([
        $di->lazyNew(OrderController::class),
        'process'
    ])
);

$response = $routeHandler($request, $param1, $param2);