-
Notifications
You must be signed in to change notification settings - Fork 988
/
PluginEvent.php
201 lines (179 loc) · 4.94 KB
/
PluginEvent.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
Yii::import('application.helpers.Hash');
class PluginEvent
{
/**
* The name of this event
*
* @var string
*/
protected $_event = '';
/**
* This array holds the content blocks that plugins generate, idexed by plugin name
*
* @var array of PluginEventContent
*/
protected $_content = array();
/**
* The class who fired the event, or null when not set
*
* @var object
*/
protected $_sender = null;
/**
* When true it prevents delegating the event to other plugins.
*
* @var boolean
*/
protected $_stop = false;
/**
* Internal storage for event data. Can be used to communicate between sender
* and plugin or between different plugins handling the event.
*
* @var array
*/
protected $_parameters = array();
/**
* Constructor for the PluginEvent
*
* @param string $event Name of the event fired
* @param object $sender The object sending the event
* @return \PluginEvent
*/
public function __construct($event, $sender = null)
{
if (!is_null($sender) && is_object($sender))
{
$this->_sender = $sender;
}
$this->_event = $event;
return $this;
}
/**
* Get a value for the given key.
*
* When the value is not set, it will return the given default or null when
* no default was given.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key = null, $default = null)
{
return Hash::get($this->_parameters, $key);
}
/**
* Return an array of pluginname / PluginEventContent but only when it has content
*
* @return PluginEventContent[]
*/
public function getAllContent()
{
$output = array();
foreach($this->_content as $plugin => $content)
{
/* @var $content PluginEventContent */
if ($content->hasContent()) {
$output[$plugin] = $content;
}
}
return $output;
}
/**
* Returns content for the given plugin(name)
*
* When there is no content yet, it will return an empty content object.
*
* @param PluginBase|string $plugin The plugin we want content for or a string name
* @return PluginEventContent
*/
public function getContent($plugin) {
if (is_string($plugin)) {
$pluginName = $plugin;
} elseif ($plugin instanceof PluginBase) {
$pluginName = get_class($plugin);
}
if (array_key_exists($pluginName, $this->_content)) {
return $this->_content[$pluginName];
} else {
return $this->setContent($pluginName);
}
}
/**
* Return the name of the event
*
* @return string
*/
public function getEventName()
{
return $this->_event;
}
/**
* Return the sender of the event
*
* Normally the class that fired the event, but can return false when not set.
*
* @return object The object sending the event, or false when unknown
*/
public function getSender()
{
if (!is_null($this->_sender)) {
return $this->_sender;
} else {
return false;
}
}
/**
* Returns true when execution of this event was stopped using $this->stop()
*
* @return boolean
*/
public function isStopped()
{
return $this->_stop;
}
/**
* Set a key/value pair to be used by plugins hanlding this event.
*
* @param string $key
* @param mixed $value
* @return \PluginEvent Fluent interface
*/
public function set($key, $value)
{
$this->_parameters = Hash::insert($this->_parameters, $key, $value);
return $this;
}
/**
* Set content for $plugin, replacing any preexisting content
*
* @param PluginBase|string $plugin The plugin setting the context or a string name
* @param string $content
* @param string $cssClass
* @param string $id
* @return PluginEventContent
*/
public function setContent($plugin, $content = null, $cssClass = null, $id = null)
{
if (is_string($plugin)) {
$pluginName = $plugin;
} elseif ($plugin instanceof PluginBase) {
$pluginName = get_class($plugin);
}
$contentObject = new PluginEventContent($content, $cssClass, $id);
if (isset($pluginName)) {
$this->_content[$pluginName] = $contentObject;
} else {
$this->_content[] = $contentObject;
}
return $contentObject;
}
/**
* Halt execution of this event by other plugins
*/
public function stop()
{
$this->_stop = true;
}
}