EmitterTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Hooks;
  25. /**
  26. * @deprecated 18.0.0 use events and the \OCP\EventDispatcher\IEventDispatcher service
  27. */
  28. trait EmitterTrait {
  29. /**
  30. * @var callable[][] $listeners
  31. */
  32. protected $listeners = [];
  33. /**
  34. * @param string $scope
  35. * @param string $method
  36. * @param callable $callback
  37. * @deprecated 18.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener
  38. */
  39. public function listen($scope, $method, callable $callback) {
  40. $eventName = $scope . '::' . $method;
  41. if (!isset($this->listeners[$eventName])) {
  42. $this->listeners[$eventName] = [];
  43. }
  44. if (array_search($callback, $this->listeners[$eventName], true) === false) {
  45. $this->listeners[$eventName][] = $callback;
  46. }
  47. }
  48. /**
  49. * @param string $scope optional
  50. * @param string $method optional
  51. * @param callable $callback optional
  52. * @deprecated 18.0.0 use \OCP\EventDispatcher\IEventDispatcher::removeListener
  53. */
  54. public function removeListener($scope = null, $method = null, callable $callback = null) {
  55. $names = [];
  56. $allNames = array_keys($this->listeners);
  57. if ($scope and $method) {
  58. $name = $scope . '::' . $method;
  59. if (isset($this->listeners[$name])) {
  60. $names[] = $name;
  61. }
  62. } elseif ($scope) {
  63. foreach ($allNames as $name) {
  64. $parts = explode('::', $name, 2);
  65. if ($parts[0] == $scope) {
  66. $names[] = $name;
  67. }
  68. }
  69. } elseif ($method) {
  70. foreach ($allNames as $name) {
  71. $parts = explode('::', $name, 2);
  72. if ($parts[1] == $method) {
  73. $names[] = $name;
  74. }
  75. }
  76. } else {
  77. $names = $allNames;
  78. }
  79. foreach ($names as $name) {
  80. if ($callback) {
  81. $index = array_search($callback, $this->listeners[$name], true);
  82. if ($index !== false) {
  83. unset($this->listeners[$name][$index]);
  84. }
  85. } else {
  86. $this->listeners[$name] = [];
  87. }
  88. }
  89. }
  90. /**
  91. * @param string $scope
  92. * @param string $method
  93. * @param array $arguments optional
  94. * @deprecated 18.0.0 use \OCP\EventDispatcher\IEventDispatcher::dispatchTyped
  95. */
  96. protected function emit($scope, $method, array $arguments = []) {
  97. $eventName = $scope . '::' . $method;
  98. if (isset($this->listeners[$eventName])) {
  99. foreach ($this->listeners[$eventName] as $callback) {
  100. call_user_func_array($callback, $arguments);
  101. }
  102. }
  103. }
  104. }