8 Commits
7 changed files with 60 additions and 31 deletions
+1 -3
View File
@@ -1,5 +1,3 @@
composer.lock
phpunit.xml
vendor
composer.lock
composer.phar
+7
View File
@@ -0,0 +1,7 @@
language: php
php:
- 5.3
- 5.4
before_script: composer install --dev
+6
View File
@@ -1,11 +1,17 @@
# WildcardEventDispatcher
[![Build Status](https://travis-ci.org/jmikola/WildcardEventDispatcher.png?branch=master)](https://travis-ci.org/jmikola/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.
If you are interested in using this library in a Symfony2 project, you may also
want to take a look at the corresponding [bundle][].
[Symfony2's interface]: https://github.com/symfony/EventDispatcher
[bundle]: https://github.com/jmikola/JmikolaWildcardEventDispatcherBundle
## Usage ##
+2 -1
View File
@@ -8,9 +8,10 @@
"authors": [
{ "name": "Jeremy Mikola", "email": "jmikola@gmail.com" }
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.2",
"symfony/event-dispatcher": ">=2.0-dev,<2.2-dev"
"symfony/event-dispatcher": "~2.0"
},
"autoload": {
"psr-0": { "Jmikola": "src/" }
@@ -92,10 +92,14 @@ class WildcardEventDispatcher implements EventDispatcherInterface
public function addSubscriber(EventSubscriberInterface $subscriber)
{
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
if (is_array($params)) {
$this->addListener($eventName, array($subscriber, $params[0]), $params[1]);
} else {
if (is_string($params)) {
$this->addListener($eventName, array($subscriber, $params));
} elseif (is_string($params[0])) {
$this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
} else {
foreach ($params as $listener) {
$this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
}
}
}
}
@@ -106,7 +110,13 @@ class WildcardEventDispatcher implements EventDispatcherInterface
public function removeSubscriber(EventSubscriberInterface $subscriber)
{
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
$this->removeListener($eventName, array($subscriber, is_array($params) ? $params[0] : $params));
if (is_array($params) && is_array($params[0])) {
foreach ($params as $listener) {
$this->removeListener($eventName, array($subscriber, $listener[0]));
}
} else {
$this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0]));
}
}
}
@@ -115,27 +115,35 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
*/
$subscriber = new TestEventSubscriber();
$i = 0;
$defaultPriority = 0;
$numSubscribedEvents = count($subscriber->getSubscribedEvents());
$this->innerDispatcher->expects($this->at(0))
->method('addListener')
->with('core.request', array($subscriber, 'onRequest'), 0);
$this->innerDispatcher->expects($this->at(1))
->method('addListener')
->with('core.exception', array($subscriber, 'onException'), 10);
$this->innerDispatcher->expects($this->at(2))
->method('addListener')
->with('core.multi', array($subscriber, 'onMulti1'), 10);
$this->innerDispatcher->expects($this->at(3))
->method('addListener')
->with('core.multi', array($subscriber, 'onMulti2'), 20);
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
$method = is_array($params) ? $params[0] : $params;
$priority = is_array($params) ? $params[1] : $defaultPriority;
$this->innerDispatcher->expects($this->at(4))
->method('removeListener')
->with('core.request', array($subscriber, 'onRequest'));
$this->innerDispatcher->expects($this->at(5))
->method('removeListener')
->with('core.exception', array($subscriber, 'onException'));
$this->innerDispatcher->expects($this->at(6))
->method('removeListener')
->with('core.multi', array($subscriber, 'onMulti1'));
$this->innerDispatcher->expects($this->at(7))
->method('removeListener')
->with('core.multi', array($subscriber, 'onMulti2'));
$this->innerDispatcher->expects($this->at($i))
->method('addListener')
->with($eventName, array($subscriber, $method), $priority);
$this->innerDispatcher->expects($this->at($numSubscribedEvents + $i))
->method('removeListener')
->with($eventName, array($subscriber, $method));
++$i;
}
$this->dispatcher->addSubscriber($subscriber, $priority);
$this->dispatcher->removeSubscriber($subscriber, $priority);
$this->dispatcher->addSubscriber($subscriber);
$this->dispatcher->removeSubscriber($subscriber);
}
private function getMockEventDispatcher()
@@ -151,6 +159,7 @@ class TestEventSubscriber implements EventSubscriberInterface
return array(
'core.request' => 'onRequest',
'core.exception' => array('onException', 10),
'core.multi' => array(array('onMulti1', 10), array('onMulti2', 20)),
);
}
}
+3 -5
View File
@@ -1,9 +1,7 @@
<?php
<?php
if (file_exists($file = __DIR__.'/../vendor/.composer/autoload.php')) {
$loader = require_once $file;
} else {
if (!is_file($autoloadFile = __DIR__.'/../vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run test suite.');
}
$loader->add('Jmikola\\WildcardEventDispatcher', __DIR__.'/../src');
require $autoloadFile;