PSR-14: ListenerProvider maybe unexpected behaviour

29 views
Skip to first unread message

Florian Hermann

unread,
Oct 28, 2019, 5:43:14 PM10/28/19
to PHP Framework Interoperability Group
Hi everyone,

I'm working on a PSR-14 implementation and I'm facing an issue :
If the argument of a listener is type-hinted with the type "object", should the ListenerProvider considers that this listener is matching all events?

I take this opportunity to ask a related question. If the listener is invalid (no argument, no type-hint for the argument or a scalar type), should I :
- throw an exception
- ignore the listener
- maybe let the choice to the user

Thanks you for your time
Regards

Florian Hermann

Larry Garfield

unread,
Oct 29, 2019, 4:59:08 PM10/29/19
to PHP-FIG
Hi Florian.

In PSR-14, the Provider can use whatever criteria it wants to determine the Listeners that are relevant for an Event, provided that the Listeners are type compatible. Reflecting on the parameter type is an obvious way, and the way we expect most implementations will do it, but that's not a requirement.

So if you're registering a Listener with a Provider, it's up to you how the Provider chooses to determine what Events it should apply to. That is, if someone does this:

$provider = new FlorianProvider();
$provider->addListener(function ($event) { });

Then addListener() is part of *your* API, not PSR-14. Throwing an exception, ignoring it, registering it for all events, those are all valid ways to implement *your* API, as long as what FlorianProvider::getListenersForEvent() returns is type-compatible with the event it's passed.

For me personally, I would likely choose to error on no-type and interpret `object` to mean "all events", since it's type compatible.

My own PSR-14 implementation, Tukio (https://github.com/crell/tukio), allows users to register listeners in a variety of ways with both reflection and explicit type specification. Some approaches only support explicit, others support both.

The PSR-14 util package also includes some examples of registering based on more criteria than just the type, which is also entirely valid.

In short, "it's up to you". :-)

Cheers.

--Larry Garfield
Reply all
Reply to author
Forward
0 new messages