hook.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * This class manages the hooks. It basically provides two functions: adding
  4. * slots and emitting signals.
  5. */
  6. class OC_Hook{
  7. static private $registered = array();
  8. /**
  9. * @brief connects a function to a hook
  10. * @param string $signalclass class name of emitter
  11. * @param string $signalname name of signal
  12. * @param string $slotclass class name of slot
  13. * @param string $slotname name of slot
  14. * @return bool
  15. *
  16. * This function makes it very easy to connect to use hooks.
  17. *
  18. * TODO: write example
  19. */
  20. static public function connect( $signalclass, $signalname, $slotclass, $slotname ) {
  21. // If we're trying to connect to an emitting class that isn't
  22. // yet registered, register it
  23. if( !array_key_exists( $signalclass, self::$registered )) {
  24. self::$registered[$signalclass] = array();
  25. }
  26. // If we're trying to connect to an emitting method that isn't
  27. // yet registered, register it with the emitting class
  28. if( !array_key_exists( $signalname, self::$registered[$signalclass] )) {
  29. self::$registered[$signalclass][$signalname] = array();
  30. }
  31. // Connect the hook handler to the requested emitter
  32. self::$registered[$signalclass][$signalname][] = array(
  33. "class" => $slotclass,
  34. "name" => $slotname
  35. );
  36. // No chance for failure ;-)
  37. return true;
  38. }
  39. /**
  40. * @brief emits a signal
  41. * @param string $signalclass class name of emitter
  42. * @param string $signalname name of signal
  43. * @param array $params defautl: array() array with additional data
  44. * @return bool, true if slots exists or false if not
  45. *
  46. * Emits a signal. To get data from the slot use references!
  47. *
  48. * TODO: write example
  49. */
  50. static public function emit( $signalclass, $signalname, $params = array()) {
  51. // Return false if no hook handlers are listening to this
  52. // emitting class
  53. if( !array_key_exists( $signalclass, self::$registered )) {
  54. return false;
  55. }
  56. // Return false if no hook handlers are listening to this
  57. // emitting method
  58. if( !array_key_exists( $signalname, self::$registered[$signalclass] )) {
  59. return false;
  60. }
  61. // Call all slots
  62. foreach( self::$registered[$signalclass][$signalname] as $i ) {
  63. try {
  64. call_user_func( array( $i["class"], $i["name"] ), $params );
  65. } catch (Exception $e){
  66. OC_Log::write('hook',
  67. 'error while running hook (' . $i["class"] . '::' . $i["name"] . '): '.$e->getMessage(),
  68. OC_Log::ERROR);
  69. }
  70. }
  71. // return true
  72. return true;
  73. }
  74. /**
  75. * clear hooks
  76. * @param string $signalclass
  77. * @param string $signalname
  78. */
  79. static public function clear($signalclass='', $signalname='') {
  80. if($signalclass) {
  81. if($signalname) {
  82. self::$registered[$signalclass][$signalname]=array();
  83. }else{
  84. self::$registered[$signalclass]=array();
  85. }
  86. }else{
  87. self::$registered=array();
  88. }
  89. }
  90. }