Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Add wildcard event identifier #2626

Closed
wants to merge 1 commit into from
Closed

Add wildcard event identifier #2626

wants to merge 1 commit into from

Conversation

jvandemo
Copy link

By adding a wildcard identifier, it is possible to attach listeners to all named instances in the shared event manager with just one line of code.

Example:

$sharedEventManager->attach('*', \Your\Controller::EVENT_INIT, function($e){

    /* @var $e \Zend\EventManager\Event */

    $event = $e->getName();
    $context = get_class($e->getTarget());
    $params = serialize($e->getParams());

    error_log(sprintf(
            'Shared event manager: %s called on %s, using params %s',
            $event,
            $context,
            $params
    ));
});

You can even combine it with a wildcard event to listen to all events.

Example with double wildcard:

$sharedEventManager->attach('*', '*', function($e){

    /* @var $e \Zend\EventManager\Event */

    $event = $e->getName();
    $context = get_class($e->getTarget());
    $params = serialize($e->getParams());

    error_log(sprintf(
            'Shared event manager: %s called on %s, using params %s',
            $event,
            $context,
            $params
    ));
});

Very easy to use and yet very powerful, so I thought it might be interesting for others too...

Change-Id: I17580674a217a80939cc908b754fa9edd734f870

Change-Id: I17580674a217a80939cc908b754fa9edd734f870
@weierophinney
Copy link
Member

This isn't necessary.

The EventManager implementation will already look at wildcard identifiers; you don't need to set it as an explicit EM identifier. See the following:

@jvandemo
Copy link
Author

Hi Matthew,

I was proposing to add an identifier wildcard, not an event wildcard (which is already implemented).

This currently works:

$sharedEventManager->attach('\Your\controller', '*', function($e){});

However, this currently doesn't currently trigger any events (using a wildcard as named instance):

$sharedEventManager->attach('*', '*', function($e){}); // Replace named instance by wildcard

This PR allows the last example to work out of the box...

@weierophinney
Copy link
Member

What I was getting at is the EM should be handling wildcard identifiers
already, with the line I linked you (there's another line later that
handles wildcard events). As such, if that code is not working, we need to
make it work. The solution is not adding the wildcard as an identifier, but
making the code work to pull listeners attached to wildcard identifiers.

On Sunday, September 30, 2012, Jurgen Van de Moere wrote:

Hi Matthew,

I was proposing to add an identifier wildcard, not an event wildcard
(which is already implemented).

This currently works:

$sharedEventManager->attach('\Your\controller', '*', function($e){});

However, this currently doesn't currently trigger any events (using a
wildcard as named instance):

$sharedEventManager->attach('', '', function($e){}); // Replace named instance by wildcard

This PR allows the last example to work out of the box...


Reply to this email directly or view it on GitHubhttps://github.com//pull/2626#issuecomment-9017048.

Matthew Weier O'Phinney
matthew@weierophinney.net
http://mwop.net/

@jvandemo
Copy link
Author

Ok, I understand what you're getting at. My listeners are not being triggered without explicitely setting the identifier, so I'll check the existing code to see where things go wrong.

Would you like me to post updates here or create an issue for it ?

Thanks !!

@weierophinney
Copy link
Member

You can do it in this pr if you want; simply start from master and force
push to the same branch.

On Sunday, September 30, 2012, Jurgen Van de Moere wrote:

Ok, I understand what you're getting at. My listeners are not being
triggered without explicitely setting the identifier, so I'll check the
existing code to see where things go wrong.

Would you like me to post updates here or create an issue for it ?

Thanks !!


Reply to this email directly or view it on GitHubhttps://github.com//pull/2626#issuecomment-9018156.

Matthew Weier O'Phinney
matthew@weierophinney.net
http://mwop.net/

weierophinney added a commit that referenced this pull request Oct 8, 2012
weierophinney added a commit to zendframework/zend-eventmanager that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants