amphp / injector

A recursive dependency injector used to bootstrap and wire together S.O.L.I.D., object-oriented PHP applications.
MIT License
31 stars 7 forks source link

Add lazy support #9

Closed kelunik closed 3 years ago

szepeviktor commented 3 years ago

@kelunik Is it a factory that returns a Closure that instantiates the given class when executed?

kelunik commented 3 years ago

Yes. Feels a bit weird, suggestions welcome.

szepeviktor commented 3 years ago

I'd like to use it in WordPress actions. Tiny Closures would be fine! Do anything heavy only when the hook is fired.

return function () {
    return Injector::make($something);
};

⚠️ This is oversimplified.

kelunik commented 3 years ago

Proxies are now very simple to create: https://github.com/amphp/injector/blob/d624cf8daddff38d05436d8c08f75f488f5b5f94/examples/proxy.php#L89-L91

The logic for the class above in the example might be moved to a separate repository to avoid the dependency on proxy-manager.

/cc @overclokk