|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* An event. |
|
5
|
|
|
* In fact a container that holds a list of classes to be called when an event |
|
6
|
|
|
* is triggered |
|
7
|
|
|
* |
|
8
|
|
|
* @name ElkArte Forum |
|
9
|
|
|
* @copyright ElkArte Forum contributors |
|
10
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
|
11
|
|
|
* |
|
12
|
|
|
* @version 1.1 |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* An event. |
|
18
|
|
|
* In fact a container that holds a list of classes to be called when an event |
|
19
|
|
|
* is triggered |
|
20
|
|
|
*/ |
|
21
|
|
|
class Event |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* If the classes have already been sorted by priority. |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $_sorted = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* List of classes. |
|
31
|
|
|
* @var string[] |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_events = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The priority object. |
|
37
|
|
|
* @var object |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $_priority = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Initialize the class. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Priority $priority the object that handles priorities |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($priority) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->_priority = $priority; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Add a new event with a certain priority. |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed[] $event An array describing the event we want to trigger: |
|
55
|
|
|
* array( |
|
56
|
|
|
* 0 => string - the position at which the event will be triggered |
|
57
|
|
|
* 1 => string[] - the class and method we want to call: |
|
58
|
|
|
* array( |
|
59
|
|
|
* 0 => string - name of the class to instantiate |
|
60
|
|
|
* 1 => string - name of the method to call |
|
61
|
|
|
* ) |
|
62
|
|
|
* 2 => null|string[] - an array of dependencies in the form of strings representing the |
|
63
|
|
|
* name of the variables the method requires. |
|
64
|
|
|
* The variables can be from: |
|
65
|
|
|
* - the default list of variables passed to the trigger |
|
66
|
|
|
* - properties (private, protected, or public) of the object that |
|
67
|
|
|
* instantiate the Event_Manager (i.e. the controller) |
|
68
|
|
|
* - globals |
|
69
|
|
|
* ) |
|
70
|
|
|
* @param int $priority A value that defines the relative priority at which |
|
71
|
|
|
* the event should be triggered. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function add($event, $priority) |
|
74
|
|
|
{ |
|
75
|
|
|
if (is_array($event[1])) |
|
76
|
|
|
$name = $event[1][0]; |
|
77
|
|
|
else |
|
78
|
|
|
$name = $event[1]; |
|
79
|
|
|
|
|
80
|
|
|
$this->_priority->add($name, $priority); |
|
|
|
|
|
|
81
|
|
|
$this->_events[$name] = $event; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Determines if there are events added or not. |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
public function hasEvents() |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->_sorted === false) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$this->_doSorting(); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->_priority->hasEntities(); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function _doSorting() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->_priority->sort(); |
|
|
|
|
|
|
102
|
|
|
$this->_sorted = true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the list of sorted events to be triggered. |
|
107
|
|
|
* |
|
108
|
|
|
* @return mixed[] |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getEvents() |
|
111
|
|
|
{ |
|
112
|
|
|
$return = array(); |
|
113
|
|
|
if ($this->_sorted === false) |
|
|
|
|
|
|
114
|
|
|
$this->_doSorting(); |
|
115
|
|
|
|
|
116
|
|
|
foreach ($this->_priority->getSortedEntities() as $value) |
|
|
|
|
|
|
117
|
|
|
$return[] = $this->_events[$value]; |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
return $return; |
|
120
|
|
|
} |
|
121
|
|
|
} |