Download the PHP package hiraeth/signal without Composer
On this page you can find all versions of the php package hiraeth/signal. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package signal
Signal
Signal is a generic callback wrapper / proxy which enables custom resolution, including lazy instantiation and dependency injection in places where it might not otherwise be supported.
If you think you know what the above means and want to skip the explanation you can simply:
Install
Othewise, read on to understand...
What's the Problem?
I was looking at using igorw/evenement
and saw this:
Lots of libraries support generic callbacks which are usually shown as anonymous functions. This is great if you write a lot of custom wiring, not so great if you separate wiring/configuration data from wiring logic, and use classes, e.g.:
FILE: config.php
Now when you read the config, you could do:
But then you're resolving all your handlers and their dependencies up front. So maybe instead you do:
OK, that's not so bad, but now you're kind of assuming your handlers will always implement __invoke
. And what if you need to handle different callback styles elsewhere? Now you have custom proxy callbacks all over.
Is There a Better Way?
Yes! ONE CALLBACK (RESOLVER) TO RULE THEM ALL!
Then:
Isn't that nice?
If you want to make it even nicer, you can move your resolver functionality into a separate class that implements __invoke($signal)
:
Is that All?
No... because whatever $handler
is gets passed to your custom resolver, you can do whatever you want when you resolve the handler. For example, maybe you want to handle "artisan" callbacks (not sure why, but whatever):
Maybe you want to create URL callbacks:
Who the hell knows! The world is your oyster.
OK, Aren't You Still Instantiating Everything Up Front?
No... $signal->create($handler)
does not return the resolved handler. Rather, it merely tracks the $handler
and returns a proxy callback in its place, so the handler isn't resolved until it actually needs to be. See the one test it has: