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

Autowiring class method/passed closure? #13

Open
oakleaf opened this issue Dec 4, 2022 · 1 comment
Open

Autowiring class method/passed closure? #13

oakleaf opened this issue Dec 4, 2022 · 1 comment

Comments

@oakleaf
Copy link

oakleaf commented Dec 4, 2022

Hello, Im experimenting with Zen (among other libraries) for finding the perfect DI for my framework. I really can't get my head around how to create the autowiring functionality for a specific method of my "kernel" class.

E.g.

class Kernel{
public function init(\Closure $_callback)
{
return $_callback(); // I want this CB to have autowired classes available
}
}

// Index.php
(new Kernel())->init(function(Request $request){

I have figured out to use EntryPoints in my configuration in order to register which classes that should be autowired, but I can't figure out how to put that into classes & methods like Im trying to explain above. Is it even possible?

@kocsismate
Copy link
Member

Hi,

Sorry, I may not get what the exact problem is, but my understanding is that you would like to resolve the Request class as the $request parameter. The simplest solution is to do the following:

class Kernel
{
    private Request $request;

    public function __construct(Request $request) {
        $this->request = $request;
    }

    public function init(\Closure $_callback)
    {
        return $_callback($this->request);
    }
}

// Index.php

$container = new Container(); // Instantiate Zen
$container->get(Kernel::class)->init(function(Request $request){}));

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

No branches or pull requests

2 participants