aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Extension/EventHandler.php
blob: 7bed0fe6f7b7f219966d2aa562c43c137fad3088 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps

namespace dokuwiki\Extension;

/**
 * Controls the registration and execution of all events,
 */
class EventHandler
{

    // public properties:  none

    // private properties
    protected $hooks = array();          // array of events and their registered handlers

    /**
     * event_handler
     *
     * constructor, loads all action plugins and calls their register() method giving them
     * an opportunity to register any hooks they require
     */
    public function __construct()
    {

        // load action plugins
        /** @var ActionPlugin $plugin */
        $plugin = null;
        $pluginlist = plugin_list('action');

        foreach ($pluginlist as $plugin_name) {
            $plugin = plugin_load('action', $plugin_name);

            if ($plugin !== null) $plugin->register($this);
        }
    }

    /**
     * register_hook
     *
     * register a hook for an event
     *
     * @param  string $event string   name used by the event, (incl '_before' or '_after' for triggers)
     * @param  string $advise
     * @param  object $obj object in whose scope method is to be executed,
     *                             if NULL, method is assumed to be a globally available function
     * @param  string $method event handler function
     * @param  mixed $param data passed to the event handler
     * @param  int $seq sequence number for ordering hook execution (ascending)
     */
    public function register_hook($event, $advise, $obj, $method, $param = null, $seq = 0)
    {
        $seq = (int)$seq;
        $doSort = !isset($this->hooks[$event . '_' . $advise][$seq]);
        $this->hooks[$event . '_' . $advise][$seq][] = array($obj, $method, $param);

        if ($doSort) {
            ksort($this->hooks[$event . '_' . $advise]);
        }
    }

    /**
     * process the before/after event
     *
     * @param Event $event
     * @param string $advise BEFORE or AFTER
     */
    public function process_event($event, $advise = '')
    {

        $evt_name = $event->name . ($advise ? '_' . $advise : '_BEFORE');

        if (!empty($this->hooks[$evt_name])) {
            foreach ($this->hooks[$evt_name] as $sequenced_hooks) {
                foreach ($sequenced_hooks as $hook) {
                    list($obj, $method, $param) = $hook;

                    if ($obj === null) {
                        $method($event, $param);
                    } else {
                        $obj->$method($event, $param);
                    }

                    if (!$event->mayPropagate()) return;
                }
            }
        }
    }

    /**
     * Check if an event has any registered handlers
     *
     * When $advise is empty, both BEFORE and AFTER events will be considered,
     * otherwise only the given advisory is checked
     *
     * @param string $name Name of the event
     * @param string $advise BEFORE, AFTER or empty
     * @return bool
     */
    public function hasHandlerForEvent($name, $advise = '')
    {
        if ($advise) {
            return isset($this->hooks[$name . '_' . $advise]);
        }

        return isset($this->hooks[$name . '_BEFORE']) || isset($this->hooks[$name . '_AFTER']);
    }
}