On Mon, Oct 28, 2019, at 10:43 PM, Florian Hermann wrote:
> 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

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

-- 
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/055933fa-6d08-4c07-80f8-dec0dd3cfb81%40www.fastmail.com.

Reply via email to