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.

150 lines
4.3 KiB

3 years ago
  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 <pvince81@owncloud.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. /**
  32. * @deprecated 18.0.0 use events and the \OCP\EventDispatcher\IEventDispatcher service
  33. */
  34. class OC_Hook {
  35. public static $thrownExceptions = [];
  36. private static $registered = [];
  37. /**
  38. * connects a function to a hook
  39. *
  40. * @param string $signalClass class name of emitter
  41. * @param string $signalName name of signal
  42. * @param string|object $slotClass class name of slot
  43. * @param string $slotName name of slot
  44. * @return bool
  45. *
  46. * This function makes it very easy to connect to use hooks.
  47. *
  48. * TODO: write example
  49. */
  50. public static function connect($signalClass, $signalName, $slotClass, $slotName) {
  51. // If we're trying to connect to an emitting class that isn't
  52. // yet registered, register it
  53. if (!array_key_exists($signalClass, self::$registered)) {
  54. self::$registered[$signalClass] = [];
  55. }
  56. // If we're trying to connect to an emitting method that isn't
  57. // yet registered, register it with the emitting class
  58. if (!array_key_exists($signalName, self::$registered[$signalClass])) {
  59. self::$registered[$signalClass][$signalName] = [];
  60. }
  61. // don't connect hooks twice
  62. foreach (self::$registered[$signalClass][$signalName] as $hook) {
  63. if ($hook['class'] === $slotClass and $hook['name'] === $slotName) {
  64. return false;
  65. }
  66. }
  67. // Connect the hook handler to the requested emitter
  68. self::$registered[$signalClass][$signalName][] = [
  69. "class" => $slotClass,
  70. "name" => $slotName
  71. ];
  72. // No chance for failure ;-)
  73. return true;
  74. }
  75. /**
  76. * emits a signal
  77. *
  78. * @param string $signalClass class name of emitter
  79. * @param string $signalName name of signal
  80. * @param mixed $params default: array() array with additional data
  81. * @return bool true if slots exists or false if not
  82. * @throws \OC\HintException
  83. * @throws \OC\ServerNotAvailableException Emits a signal. To get data from the slot use references!
  84. *
  85. * TODO: write example
  86. */
  87. public static function emit($signalClass, $signalName, $params = []) {
  88. // Return false if no hook handlers are listening to this
  89. // emitting class
  90. if (!array_key_exists($signalClass, self::$registered)) {
  91. return false;
  92. }
  93. // Return false if no hook handlers are listening to this
  94. // emitting method
  95. if (!array_key_exists($signalName, self::$registered[$signalClass])) {
  96. return false;
  97. }
  98. // Call all slots
  99. foreach (self::$registered[$signalClass][$signalName] as $i) {
  100. try {
  101. call_user_func([ $i["class"], $i["name"] ], $params);
  102. } catch (Exception $e) {
  103. self::$thrownExceptions[] = $e;
  104. \OC::$server->getLogger()->logException($e);
  105. if ($e instanceof \OC\HintException) {
  106. throw $e;
  107. }
  108. if ($e instanceof \OC\ServerNotAvailableException) {
  109. throw $e;
  110. }
  111. }
  112. }
  113. return true;
  114. }
  115. /**
  116. * clear hooks
  117. * @param string $signalClass
  118. * @param string $signalName
  119. */
  120. public static function clear($signalClass='', $signalName='') {
  121. if ($signalClass) {
  122. if ($signalName) {
  123. self::$registered[$signalClass][$signalName]=[];
  124. } else {
  125. self::$registered[$signalClass]=[];
  126. }
  127. } else {
  128. self::$registered=[];
  129. }
  130. }
  131. /**
  132. * DO NOT USE!
  133. * For unit tests ONLY!
  134. */
  135. public static function getHooks() {
  136. return self::$registered;
  137. }
  138. }