EmitterTrait.php 2.6 KB

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