ForwardingEmitter.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Hooks;
  24. /**
  25. * Class ForwardingEmitter
  26. *
  27. * allows forwarding all listen calls to other emitters
  28. *
  29. * @package OC\Hooks
  30. */
  31. abstract class ForwardingEmitter extends BasicEmitter {
  32. /**
  33. * @var \OC\Hooks\Emitter[] array
  34. */
  35. private $forwardEmitters = array();
  36. /**
  37. * @param string $scope
  38. * @param string $method
  39. * @param callable $callback
  40. */
  41. public function listen($scope, $method, callable $callback) {
  42. parent::listen($scope, $method, $callback);
  43. foreach ($this->forwardEmitters as $emitter) {
  44. $emitter->listen($scope, $method, $callback);
  45. }
  46. }
  47. /**
  48. * @param \OC\Hooks\Emitter $emitter
  49. */
  50. protected function forward(Emitter $emitter) {
  51. $this->forwardEmitters[] = $emitter;
  52. //forward all previously connected hooks
  53. foreach ($this->listeners as $key => $listeners) {
  54. list($scope, $method) = explode('::', $key, 2);
  55. foreach ($listeners as $listener) {
  56. $emitter->listen($scope, $method, $listener);
  57. }
  58. }
  59. }
  60. }