1 | <?php |
||
14 | class Doku_Event { |
||
15 | |||
16 | // public properties |
||
17 | public $name = ''; // READONLY event name, objects must register against this name to see the event |
||
18 | public $data = null; // READWRITE data relevant to the event, no standardised format (YET!) |
||
19 | public $result = null; // READWRITE the results of the event action, only relevant in "_AFTER" advise |
||
20 | // event handlers may modify this if they are preventing the default action |
||
21 | // to provide the after event handlers with event results |
||
22 | public $canPreventDefault = true; // READONLY if true, event handlers can prevent the events default action |
||
23 | |||
24 | // private properties, event handlers can effect these through the provided methods |
||
25 | protected $_default = true; // whether or not to carry out the default action associated with the event |
||
26 | protected $_continue = true; // whether or not to continue propagating the event to other handlers |
||
27 | |||
28 | /** |
||
29 | * event constructor |
||
30 | * |
||
31 | * @param string $name |
||
32 | * @param mixed $data |
||
33 | */ |
||
34 | function __construct($name, &$data) { |
||
40 | |||
41 | /** |
||
42 | * @return string |
||
43 | */ |
||
44 | function __toString() { |
||
47 | |||
48 | /** |
||
49 | * advise functions |
||
50 | * |
||
51 | * advise all registered handlers of this event |
||
52 | * |
||
53 | * if these methods are used by functions outside of this object, they must |
||
54 | * properly handle correct processing of any default action and issue an |
||
55 | * advise_after() signal. e.g. |
||
56 | * $evt = new Doku_Event(name, data); |
||
57 | * if ($evt->advise_before(canPreventDefault) { |
||
58 | * // default action code block |
||
59 | * } |
||
60 | * $evt->advise_after(); |
||
61 | * unset($evt); |
||
62 | * |
||
63 | * @param bool $enablePreventDefault |
||
64 | * @return bool results of processing the event, usually $this->_default |
||
65 | */ |
||
66 | function advise_before($enablePreventDefault=true) { |
||
74 | |||
75 | function advise_after() { |
||
81 | |||
82 | /** |
||
83 | * trigger |
||
84 | * |
||
85 | * - advise all registered (<event>_BEFORE) handlers that this event is about to take place |
||
86 | * - carry out the default action using $this->data based on $enablePrevent and |
||
87 | * $this->_default, all of which may have been modified by the event handlers. |
||
88 | * - advise all registered (<event>_AFTER) handlers that the event has taken place |
||
89 | * |
||
90 | * @param null|callable $action |
||
91 | * @param bool $enablePrevent |
||
92 | * @return mixed $event->results |
||
93 | * the value set by any <event>_before or <event> handlers if the default action is prevented |
||
94 | * or the results of the default action (as modified by <event>_after handlers) |
||
95 | * or NULL no action took place and no handler modified the value |
||
96 | */ |
||
97 | function trigger($action=null, $enablePrevent=true) { |
||
119 | |||
120 | /** |
||
121 | * stopPropagation |
||
122 | * |
||
123 | * stop any further processing of the event by event handlers |
||
124 | * this function does not prevent the default action taking place |
||
125 | */ |
||
126 | public function stopPropagation() { |
||
129 | |||
130 | /** |
||
131 | * may the event propagate to the next handler? |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function mayPropgate() { |
||
138 | |||
139 | /** |
||
140 | * preventDefault |
||
141 | * |
||
142 | * prevent the default action taking place |
||
143 | */ |
||
144 | public function preventDefault() { |
||
147 | |||
148 | /** |
||
149 | * should the default action be executed? |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function mayRunDefault() { |
||
156 | } |
||
157 | |||
258 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.