2017 © Pedro Peláez
 

library wildcard-event-dispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

image

jmikola/wildcard-event-dispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  • Thursday, April 12, 2018
  • by jmikola
  • Repository
  • 3 Watchers
  • 32 Stars
  • 108,867 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 9 Versions
  • 10 % Grown

The README.md

WildcardEventDispatcher

Build Status, (*1)

This library implements an event dispatcher, based on Symfony's interface, with wildcard syntax inspired by AMQP topic exchanges. Listeners may be bound to a wildcard pattern and be notified if a dispatched event's name matches that pattern. Literal event name matching is still supported., (*2)

Note: the wildcard syntax used by this library is not compatible with the simpler event dispatching introduced in Symfony 4.3, which uses the FQCN of the event in lieu of a custom string (e.g. core.request)., (*3)

If you are interested in using this library in a Symfony project, you may also want to take a look at the corresponding bundle., (*4)

Installation

The library is published as a package and is installable via Composer:, (*5)

$ composer require jmikola/wildcard-event-dispatcher=^2.0

Compatibility

This library requires Symfony 4.3 or above., (*6)

Usage

WildcardEventDispatcher implements EventDispatcherInterface and may be used as you would Symfony's standard EventDispatcher:, (*7)

<?php

use Jmikola\WildcardEventDispatcher\WildcardEventDispatcher;
use Symfony\Component\EventDispatcher\Event;

$dispatcher = new WildcardEventDispatcher();
$dispatcher->addListener('core.*', function(Event $e) {
    echo $e->getName();
});
$dispatcher->dispatch('core.request');

// "core.request" will be printed

Internally, WildcardEventDispatcher actually composes an EventDispatcherInterface instance, which it relies upon for event handling. By default, WildcardEventDispatcher will construct an EventDispatcher object for internal use, but you may specify a particular EventDispatcherInterface instance to wrap in the constructor:, (*8)

<?php

use Jmikola\WildcardEventDispatcher\WildcardEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new WildcardEventDispatcher(new EventDispatcher());

Wildcard Syntax

Single-word Wildcard

Consider the scenario where the same listener is defined for multiple events, all of which share a common prefix:, (*9)

<?php

$coreListener = function(Event $e) {};

$dispatcher = new WildcardEventDispatcher();
$dispatcher->addListener('core.exception', $coreListener);
$dispatcher->addListener('core.request', $coreListener);
$dispatcher->addListener('core.response', $coreListener);

These event names all consist of two dot-separated words. This concept of a word will be important in understanding how wildcard patterns apply., (*10)

In this example, the listener is responsible for observing all core events in the application. Let's suppose it needs to log some details about these events to an external server. We can refactor multiple addListener() calls by using the single-word * wildcard:, (*11)

<?php

$coreListener = function(Event $e) {};

$dispatcher = new WildcardEventDispatcher();
$dispatcher->addListener('core.*', $coreListener);

The listener will now observe all events named core or starting with core. and followed by another word. The matching of core alone may not make sense, but this is implemented in order to be consistent with AMQP. A trailing * after a non-empty sequence may match the preceding sequence sans .*., (*12)

Multi-word Wildcard

Suppose there was a core event in your application named core.foo.bar. The aforementioned core.* pattern would not catch this event. You could use:, (*13)

<?php

$coreListener = function(Event $e) {};

$dispatcher = new WildcardEventDispatcher();
$dispatcher->addListener('core.*.*', $coreListener);

This syntax would match core.foo and core.foo.bar, but core would no longer be matched (assuming there was such an event)., (*14)

The multi-word # wildcard might be more appropriate here:, (*15)

<?php

$coreListener = function(Event $e) {};

$dispatcher = new WildcardEventDispatcher();
$dispatcher->addListener('core.#', $coreListener);

Suppose there was also an listener in the application that needed to listen on all events in the application. The multi-word # wildcard could be used:, (*16)

<?php

$allListener = function(Event $e) {};

$dispatcher = new WildcardEventDispatcher();
$dispatcher->addListener('#', $allListener);

Additional Wildcard Documentation

When in doubt, the unit tests for ListenerPattern are a good resource for inferring how wildcards will be interpreted. This library aims to mimic the behavior of AMQP topic wildcards completely, but there may be shortcomings., (*17)

Documentation for actual AMQP syntax may be found in the following packages:, (*18)

The Versions

12/04 2018

dev-master

9999999-dev https://github.com/jmikola/WildcardEventDispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  Sources   Download

MIT

The Requires

 

The Development Requires

dispatcher event event dispatcher amqp

12/04 2018

1.1.1

1.1.1.0 https://github.com/jmikola/WildcardEventDispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  Sources   Download

MIT

The Requires

 

The Development Requires

dispatcher event event dispatcher amqp

12/04 2018

dev-gh11

dev-gh11 https://github.com/jmikola/WildcardEventDispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  Sources   Download

MIT

The Requires

 

The Development Requires

dispatcher event event dispatcher amqp

14/09 2016

1.1.0

1.1.0.0 https://github.com/jmikola/WildcardEventDispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  Sources   Download

MIT

The Requires

 

dispatcher event event dispatcher amqp

30/07 2013

v1.0.x-dev

1.0.9999999.9999999-dev https://github.com/jmikola/WildcardEventDispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  Sources   Download

MIT

The Requires

 

dispatcher event event dispatcher amqp

30/07 2013

v1.0.3

1.0.3.0 https://github.com/jmikola/WildcardEventDispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  Sources   Download

MIT

The Requires

 

dispatcher event event dispatcher amqp

06/06 2013

v1.0.2

1.0.2.0 https://github.com/jmikola/WildcardEventDispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  Sources   Download

MIT

The Requires

 

dispatcher event event dispatcher amqp

26/02 2013

v1.0.1

1.0.1.0 https://github.com/jmikola/WildcardEventDispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  Sources   Download

MIT

The Requires

 

dispatcher event event dispatcher amqp

13/02 2012

v1.0.0

1.0.0.0 https://github.com/jmikola/WildcardEventDispatcher

Event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

  Sources   Download

MIT

The Requires

 

dispatcher event event dispatcher amqp