EventHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
  3. namespace dokuwiki\Extension;
  4. /**
  5. * Controls the registration and execution of all events,
  6. */
  7. class EventHandler
  8. {
  9. // public properties: none
  10. // private properties
  11. protected $hooks = array(); // array of events and their registered handlers
  12. /**
  13. * event_handler
  14. *
  15. * constructor, loads all action plugins and calls their register() method giving them
  16. * an opportunity to register any hooks they require
  17. */
  18. public function __construct()
  19. {
  20. // load action plugins
  21. /** @var ActionPlugin $plugin */
  22. $plugin = null;
  23. $pluginlist = plugin_list('action');
  24. foreach ($pluginlist as $plugin_name) {
  25. $plugin = plugin_load('action', $plugin_name);
  26. if ($plugin !== null) $plugin->register($this);
  27. }
  28. }
  29. /**
  30. * register_hook
  31. *
  32. * register a hook for an event
  33. *
  34. * @param string $event name used by the event
  35. * @param string $advise BEFORE|AFTER
  36. * @param object $obj scope for the method be executed on, NULL for global function or callable
  37. * @param string|callable $method event handler function
  38. * @param mixed $param data passed to the event handler
  39. * @param int $seq sequence number for ordering hook execution (ascending)
  40. */
  41. public function register_hook($event, $advise, $obj, $method, $param = null, $seq = 0)
  42. {
  43. $seq = (int)$seq;
  44. $doSort = !isset($this->hooks[$event . '_' . $advise][$seq]);
  45. $this->hooks[$event . '_' . $advise][$seq][] = array($obj, $method, $param);
  46. if ($doSort) {
  47. ksort($this->hooks[$event . '_' . $advise]);
  48. }
  49. }
  50. /**
  51. * process the before/after event
  52. *
  53. * @param Event $event
  54. * @param string $advise BEFORE or AFTER
  55. */
  56. public function process_event($event, $advise = '')
  57. {
  58. $evt_name = $event->name . ($advise ? '_' . $advise : '_BEFORE');
  59. if (!empty($this->hooks[$evt_name])) {
  60. foreach ($this->hooks[$evt_name] as $sequenced_hooks) {
  61. foreach ($sequenced_hooks as $hook) {
  62. list($obj, $method, $param) = $hook;
  63. if ($obj === null) {
  64. $method($event, $param);
  65. } else {
  66. $obj->$method($event, $param);
  67. }
  68. if (!$event->mayPropagate()) return;
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * Check if an event has any registered handlers
  75. *
  76. * When $advise is empty, both BEFORE and AFTER events will be considered,
  77. * otherwise only the given advisory is checked
  78. *
  79. * @param string $name Name of the event
  80. * @param string $advise BEFORE, AFTER or empty
  81. * @return bool
  82. */
  83. public function hasHandlerForEvent($name, $advise = '')
  84. {
  85. if ($advise) {
  86. return isset($this->hooks[$name . '_' . $advise]);
  87. }
  88. return isset($this->hooks[$name . '_BEFORE']) || isset($this->hooks[$name . '_AFTER']);
  89. }
  90. /**
  91. * Get all hooks and their currently registered handlers
  92. *
  93. * The handlers are sorted by sequence, then by register time
  94. *
  95. * @return array
  96. */
  97. public function getEventHandlers()
  98. {
  99. return $this->hooks;
  100. }
  101. }