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.

hook.php 4.4KB

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