From 925d09cb0e3aed2ab89a11d636111ba699cc403f Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 8 Jul 2013 16:45:19 +0200 Subject: add forwarding emitter for agregating multiple emitters --- lib/hooks/forwardingemitter.php | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/hooks/forwardingemitter.php (limited to 'lib') diff --git a/lib/hooks/forwardingemitter.php b/lib/hooks/forwardingemitter.php new file mode 100644 index 00000000000..518641ac7cf --- /dev/null +++ b/lib/hooks/forwardingemitter.php @@ -0,0 +1,42 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Hooks; + +/** + * Class ForwardingEmitter + * + * allows forwarding all listen calls to other emitters + * + * @package OC\Hooks + */ +abstract class ForwardingEmitter extends BasicEmitter { + /** + * @var \OC\Hooks\Emitter[] array + */ + private $forwardEmitters = array(); + + /** + * @param string $scope + * @param string $method + * @param callable $callback + */ + public function listen($scope, $method, $callback) { + parent::listen($scope, $method, $callback); + foreach ($this->forwardEmitters as $emitter) { + $emitter->listen($scope, $method, $callback); + } + } + + /** + * @param \OC\Hooks\Emitter $emitter + */ + protected function forward($emitter) { + $this->forwardEmitters[] = $emitter; + } +} -- cgit v1.2.3 From 4a4e139c8391986ce54585f3292f3c2e40dd624d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 8 Jul 2013 16:54:26 +0200 Subject: forward previously registerd hooks --- lib/hooks/basicemitter.php | 2 +- lib/hooks/forwardingemitter.php | 8 ++++++++ tests/lib/hooks/forwardingemitter.php | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/hooks/basicemitter.php b/lib/hooks/basicemitter.php index e615a58cfe8..9ffe1af2314 100644 --- a/lib/hooks/basicemitter.php +++ b/lib/hooks/basicemitter.php @@ -13,7 +13,7 @@ abstract class BasicEmitter implements Emitter { /** * @var (callable[])[] $listeners */ - private $listeners = array(); + protected $listeners = array(); /** * @param string $scope diff --git a/lib/hooks/forwardingemitter.php b/lib/hooks/forwardingemitter.php index 518641ac7cf..1aacc4012e0 100644 --- a/lib/hooks/forwardingemitter.php +++ b/lib/hooks/forwardingemitter.php @@ -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); + } + } } } diff --git a/tests/lib/hooks/forwardingemitter.php b/tests/lib/hooks/forwardingemitter.php index 0686ebece9f..decf6bb354c 100644 --- a/tests/lib/hooks/forwardingemitter.php +++ b/tests/lib/hooks/forwardingemitter.php @@ -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); + } } -- cgit v1.2.3