hook.php 4.2 KB

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