OC_Hook.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Jakob Sack <mail@jakobsack.de>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Sam Tuke <mail@samtuke.com>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <vincent@nextcloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. class OC_Hook {
  32. public static $thrownExceptions = [];
  33. private static $registered = [];
  34. /**
  35. * connects a function to a hook
  36. *
  37. * @param string $signalClass class name of emitter
  38. * @param string $signalName name of signal
  39. * @param string|object $slotClass class name of slot
  40. * @param string $slotName name of slot
  41. * @return bool
  42. *
  43. * This function makes it very easy to connect to use hooks.
  44. *
  45. * TODO: write example
  46. */
  47. public static function connect($signalClass, $signalName, $slotClass, $slotName) {
  48. // If we're trying to connect to an emitting class that isn't
  49. // yet registered, register it
  50. if (!array_key_exists($signalClass, self::$registered)) {
  51. self::$registered[$signalClass] = [];
  52. }
  53. // If we're trying to connect to an emitting method that isn't
  54. // yet registered, register it with the emitting class
  55. if (!array_key_exists($signalName, self::$registered[$signalClass])) {
  56. self::$registered[$signalClass][$signalName] = [];
  57. }
  58. // don't connect hooks twice
  59. foreach (self::$registered[$signalClass][$signalName] as $hook) {
  60. if ($hook['class'] === $slotClass and $hook['name'] === $slotName) {
  61. return false;
  62. }
  63. }
  64. // Connect the hook handler to the requested emitter
  65. self::$registered[$signalClass][$signalName][] = [
  66. "class" => $slotClass,
  67. "name" => $slotName
  68. ];
  69. // No chance for failure ;-)
  70. return true;
  71. }
  72. /**
  73. * emits a signal
  74. *
  75. * @param string $signalClass class name of emitter
  76. * @param string $signalName name of signal
  77. * @param mixed $params default: array() array with additional data
  78. * @return bool true if slots exists or false if not
  79. * @throws \OCP\HintException
  80. * @throws \OC\ServerNotAvailableException Emits a signal. To get data from the slot use references!
  81. *
  82. * TODO: write example
  83. */
  84. public static function emit($signalClass, $signalName, $params = []) {
  85. // Return false if no hook handlers are listening to this
  86. // emitting class
  87. if (!array_key_exists($signalClass, self::$registered)) {
  88. return false;
  89. }
  90. // Return false if no hook handlers are listening to this
  91. // emitting method
  92. if (!array_key_exists($signalName, self::$registered[$signalClass])) {
  93. return false;
  94. }
  95. // Call all slots
  96. foreach (self::$registered[$signalClass][$signalName] as $i) {
  97. try {
  98. call_user_func([ $i["class"], $i["name"] ], $params);
  99. } catch (Exception $e) {
  100. self::$thrownExceptions[] = $e;
  101. \OC::$server->getLogger()->logException($e);
  102. if ($e instanceof \OCP\HintException) {
  103. throw $e;
  104. }
  105. if ($e instanceof \OC\ServerNotAvailableException) {
  106. throw $e;
  107. }
  108. }
  109. }
  110. return true;
  111. }
  112. /**
  113. * clear hooks
  114. * @param string $signalClass
  115. * @param string $signalName
  116. */
  117. public static function clear($signalClass = '', $signalName = '') {
  118. if ($signalClass) {
  119. if ($signalName) {
  120. self::$registered[$signalClass][$signalName] = [];
  121. } else {
  122. self::$registered[$signalClass] = [];
  123. }
  124. } else {
  125. self::$registered = [];
  126. }
  127. }
  128. /**
  129. * DO NOT USE!
  130. * For unit tests ONLY!
  131. */
  132. public static function getHooks() {
  133. return self::$registered;
  134. }
  135. }