1
0

OC_Hook.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. class OC_Hook {
  8. public static $thrownExceptions = [];
  9. private static $registered = [];
  10. /**
  11. * connects a function to a hook
  12. *
  13. * @param string $signalClass class name of emitter
  14. * @param string $signalName name of signal
  15. * @param string|object $slotClass class name of slot
  16. * @param string $slotName name of slot
  17. * @return bool
  18. *
  19. * This function makes it very easy to connect to use hooks.
  20. *
  21. * TODO: write example
  22. */
  23. public static function connect($signalClass, $signalName, $slotClass, $slotName) {
  24. // If we're trying to connect to an emitting class that isn't
  25. // yet registered, register it
  26. if (!array_key_exists($signalClass, self::$registered)) {
  27. self::$registered[$signalClass] = [];
  28. }
  29. // If we're trying to connect to an emitting method that isn't
  30. // yet registered, register it with the emitting class
  31. if (!array_key_exists($signalName, self::$registered[$signalClass])) {
  32. self::$registered[$signalClass][$signalName] = [];
  33. }
  34. // don't connect hooks twice
  35. foreach (self::$registered[$signalClass][$signalName] as $hook) {
  36. if ($hook['class'] === $slotClass and $hook['name'] === $slotName) {
  37. return false;
  38. }
  39. }
  40. // Connect the hook handler to the requested emitter
  41. self::$registered[$signalClass][$signalName][] = [
  42. "class" => $slotClass,
  43. "name" => $slotName
  44. ];
  45. // No chance for failure ;-)
  46. return true;
  47. }
  48. /**
  49. * emits a signal
  50. *
  51. * @param string $signalClass class name of emitter
  52. * @param string $signalName name of signal
  53. * @param mixed $params default: array() array with additional data
  54. * @return bool true if slots exists or false if not
  55. * @throws \OCP\HintException
  56. * @throws \OC\ServerNotAvailableException Emits a signal. To get data from the slot use references!
  57. *
  58. * TODO: write example
  59. */
  60. public static function emit($signalClass, $signalName, $params = []) {
  61. // Return false if no hook handlers are listening to this
  62. // emitting class
  63. if (!array_key_exists($signalClass, self::$registered)) {
  64. return false;
  65. }
  66. // Return false if no hook handlers are listening to this
  67. // emitting method
  68. if (!array_key_exists($signalName, self::$registered[$signalClass])) {
  69. return false;
  70. }
  71. // Call all slots
  72. foreach (self::$registered[$signalClass][$signalName] as $i) {
  73. try {
  74. call_user_func([ $i["class"], $i["name"] ], $params);
  75. } catch (Exception $e) {
  76. self::$thrownExceptions[] = $e;
  77. \OC::$server->getLogger()->logException($e);
  78. if ($e instanceof \OCP\HintException) {
  79. throw $e;
  80. }
  81. if ($e instanceof \OC\ServerNotAvailableException) {
  82. throw $e;
  83. }
  84. }
  85. }
  86. return true;
  87. }
  88. /**
  89. * clear hooks
  90. * @param string $signalClass
  91. * @param string $signalName
  92. */
  93. public static function clear($signalClass = '', $signalName = '') {
  94. if ($signalClass) {
  95. if ($signalName) {
  96. self::$registered[$signalClass][$signalName] = [];
  97. } else {
  98. self::$registered[$signalClass] = [];
  99. }
  100. } else {
  101. self::$registered = [];
  102. }
  103. }
  104. /**
  105. * DO NOT USE!
  106. * For unit tests ONLY!
  107. */
  108. public static function getHooks() {
  109. return self::$registered;
  110. }
  111. }