Coder Social home page Coder Social logo

seldaek / wildcardeventdispatcher Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jmikola/wildcardeventdispatcher

1.0 3.0 0.0 77 KB

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

Home Page: http://jmikola.net

License: MIT License

PHP 100.00%

wildcardeventdispatcher's Introduction

WildcardEventDispatcher

This library implements an event dispatcher, based on Symfony2'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.

Usage

WildcardEventDispatcher implements EventDispatcherInterface and may be used as you would Symfony2's standard EventDispatcher:

<?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:

<?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:

<?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.

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:

<?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 .*.

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:

<?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).

The multi-word # wildcard might be more appropriate here:

<?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:

<?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.

Documentation for actual AMQP syntax may be found in the following packages:

wildcardeventdispatcher's People

Contributors

jmikola avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.