forwardingemitter.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Hooks;
  9. /**
  10. * Class ForwardingEmitter
  11. *
  12. * allows forwarding all listen calls to other emitters
  13. *
  14. * @package OC\Hooks
  15. */
  16. abstract class ForwardingEmitter extends BasicEmitter {
  17. /**
  18. * @var \OC\Hooks\Emitter[] array
  19. */
  20. private $forwardEmitters = array();
  21. /**
  22. * @param string $scope
  23. * @param string $method
  24. * @param callable $callback
  25. */
  26. public function listen($scope, $method, $callback) {
  27. parent::listen($scope, $method, $callback);
  28. foreach ($this->forwardEmitters as $emitter) {
  29. $emitter->listen($scope, $method, $callback);
  30. }
  31. }
  32. /**
  33. * @param \OC\Hooks\Emitter $emitter
  34. */
  35. protected function forward($emitter) {
  36. $this->forwardEmitters[] = $emitter;
  37. //forward all previously connected hooks
  38. foreach ($this->listeners as $key => $listeners) {
  39. list($scope, $method) = explode('::', $key, 2);
  40. foreach ($listeners as $listener) {
  41. $emitter->listen($scope, $method, $listener);
  42. }
  43. }
  44. }
  45. }