<?php function e($n,$p,$c=0){static $a;@krsort($a[$n]);if($c)$a[$n][$p][]=$c;else foreach($a[$n] as$q)foreach($q as$r)$r($p);}
function it($m,$p){echo ($p?'✔︎':'✘')." It $m\n"; if(!$p){$GLOBALS['f']=1;}}function done(){if(@$GLOBALS['f'])die(1);}
ob_start();
e(
'event',
0,
function ($data) {
echo "event, $data, priority 0;";
}
);
e(
'event',
10,
function ($data) {
echo "event, $data, priority 10;";
}
);
e(
'other_event',
-5,
function ($data) {
echo "other_event, $data, priority -5;";
}
);
e(
'other_event',
5,
function ($data) {
echo "other_event, $data priority 5;";
}
);
e('event', 'dataX');
e('other_event', 'dataY');
$output = ob_get_contents();
ob_end_clean();
it(
'calls event listeners in the right order',
$output === 'event, dataX, priority 10;event, dataX, priority 0;other_event, dataY priority 5;other_event, dataY, priority -5;'
);
class Foo {
private $eventDispatcher;
function __construct($eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
function doStuff()
{
// ...
call_user_func($this->eventDispatcher, "my_event", "my data");
}
}
ob_start();
$foo = new Foo(@e);
e("my_event", 5, function($data) { echo "My event was called with '$data'";});
$foo->doStuff();
$output = ob_get_contents();ob_end_clean();
it(
'is perfectly possible to inject the event dispatcher as a dependency into classes.',
$output === "My event was called with 'my data'"
);
preferences:
31.62 ms | 402 KiB | 5 Q