EmitterTrait.php 2.7 KB

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