1
0

hook.php 4.2 KB

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