You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.7 KiB

3 years ago
  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. trait EmitterTrait {
  26. /**
  27. * @var callable[][] $listeners
  28. */
  29. protected $listeners = [];
  30. /**
  31. * @param string $scope
  32. * @param string $method
  33. * @param callable $callback
  34. */
  35. public function listen($scope, $method, callable $callback) {
  36. $eventName = $scope . '::' . $method;
  37. if (!isset($this->listeners[$eventName])) {
  38. $this->listeners[$eventName] = [];
  39. }
  40. if (array_search($callback, $this->listeners[$eventName], true) === false) {
  41. $this->listeners[$eventName][] = $callback;
  42. }
  43. }
  44. /**
  45. * @param string $scope optional
  46. * @param string $method optional
  47. * @param callable $callback optional
  48. */
  49. public function removeListener($scope = null, $method = null, callable $callback = null) {
  50. $names = [];
  51. $allNames = array_keys($this->listeners);
  52. if ($scope and $method) {
  53. $name = $scope . '::' . $method;
  54. if (isset($this->listeners[$name])) {
  55. $names[] = $name;
  56. }
  57. } elseif ($scope) {
  58. foreach ($allNames as $name) {
  59. $parts = explode('::', $name, 2);
  60. if ($parts[0] == $scope) {
  61. $names[] = $name;
  62. }
  63. }
  64. } elseif ($method) {
  65. foreach ($allNames as $name) {
  66. $parts = explode('::', $name, 2);
  67. if ($parts[1] == $method) {
  68. $names[] = $name;
  69. }
  70. }
  71. } else {
  72. $names = $allNames;
  73. }
  74. foreach ($names as $name) {
  75. if ($callback) {
  76. $index = array_search($callback, $this->listeners[$name], true);
  77. if ($index !== false) {
  78. unset($this->listeners[$name][$index]);
  79. }
  80. } else {
  81. $this->listeners[$name] = [];
  82. }
  83. }
  84. }
  85. /**
  86. * @param string $scope
  87. * @param string $method
  88. * @param array $arguments optional
  89. */
  90. protected function emit($scope, $method, array $arguments = []) {
  91. $eventName = $scope . '::' . $method;
  92. if (isset($this->listeners[$eventName])) {
  93. foreach ($this->listeners[$eventName] as $callback) {
  94. call_user_func_array($callback, $arguments);
  95. }
  96. }
  97. }
  98. }