Skip to content
Snippets Groups Projects
Commit 4a4e139c authored by Robin Appelman's avatar Robin Appelman
Browse files

forward previously registerd hooks

parent 925d09cb
Branches
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@ abstract class BasicEmitter implements Emitter {
/**
* @var (callable[])[] $listeners
*/
private $listeners = array();
protected $listeners = array();
/**
* @param string $scope
......
......@@ -38,5 +38,13 @@ abstract class ForwardingEmitter extends BasicEmitter {
*/
protected function forward($emitter) {
$this->forwardEmitters[] = $emitter;
//forward all previously connected hooks
foreach ($this->listeners as $key => $listeners) {
list($scope, $method) = explode('::', $key, 2);
foreach ($listeners as $listener) {
$emitter->listen($scope, $method, $listener);
}
}
}
}
......@@ -59,4 +59,16 @@ class ForwardingEmitter extends BasicEmitter {
$baseEmitter1->emit('Test', 'test2');
$this->assertEquals(2, $hookCalled);
}
public function testForwardExistingHooks() {
$baseEmitter = new PublicEmitter();
$forwardingEmitter = new DummyForwardingEmitter();
$hookCalled = false;
$forwardingEmitter->listen('Test', 'test', function () use (&$hookCalled) {
$hookCalled = true;
});
$forwardingEmitter->forward($baseEmitter);
$baseEmitter->emit('Test', 'test');
$this->assertTrue($hookCalled);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment