Skip to content
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

Add a LazyLazy object #220

Merged
merged 5 commits into from
May 28, 2024
Merged

Add a LazyLazy object #220

merged 5 commits into from
May 28, 2024

Conversation

frederikbosch
Copy link
Contributor

@frederikbosch frederikbosch commented May 28, 2024

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);

@frederikbosch frederikbosch merged commit 43642d9 into 5.x May 28, 2024
11 checks passed
@frederikbosch frederikbosch deleted the lazy_lazy branch August 23, 2024 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant