|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\Core\Event; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use RuntimeException; |
|
18
|
|
|
use Symfony\Contracts\EventDispatcher\Event as SymfonyGenericEvent; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Event encapsulation class. |
|
22
|
|
|
* |
|
23
|
|
|
* Encapsulates events thus decoupling the observer from the subject they encapsulate. |
|
24
|
|
|
*/ |
|
25
|
|
|
class GenericEvent extends SymfonyGenericEvent implements \ArrayAccess, \IteratorAggregate |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var mixed |
|
29
|
|
|
*/ |
|
30
|
|
|
public $data; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Exception. |
|
34
|
|
|
* |
|
35
|
|
|
* @var Exception |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $exception; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $subject; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $args = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Encapsulate an event with $subject, $args, and $data. |
|
51
|
|
|
* |
|
52
|
|
|
* @param mixed $subject Usually an object or other PHP callable |
|
53
|
|
|
* @param array $args Arguments to store in the event |
|
54
|
|
|
* @param mixed $data Convenience argument of data for optional processing |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct($subject = null, array $args = [], $data = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->data = $data; |
|
59
|
|
|
$this->subject = $subject; |
|
60
|
|
|
$this->args = $args; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Sets the data |
|
65
|
|
|
* |
|
66
|
|
|
* @param mixed $data |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setData($data): self |
|
69
|
|
|
{ |
|
70
|
|
|
$this->data = $data; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Getter for Data property. |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed Data property |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getData() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->data; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get exception. |
|
87
|
|
|
* |
|
88
|
|
|
* @throws RuntimeException If no exception was set |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getException(): Exception |
|
91
|
|
|
{ |
|
92
|
|
|
if (!$this->hasException()) { |
|
93
|
|
|
throw new RuntimeException('No exception was set during this event notification.'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->exception; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set exception. |
|
101
|
|
|
* |
|
102
|
|
|
* Rather than throw an exception within an event handler, |
|
103
|
|
|
* instead you can store it here then stop() execution. |
|
104
|
|
|
* This can then be rethrown or handled politely. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setException(Exception $exception): void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->exception = $exception; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function hasException(): bool |
|
112
|
|
|
{ |
|
113
|
|
|
return (bool)$this->exception; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getSubject() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->subject; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function setSubject($subject): void |
|
122
|
|
|
{ |
|
123
|
|
|
$this->subject = $subject; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getArgs(): array |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->args; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function setArgs(array $args): void |
|
132
|
|
|
{ |
|
133
|
|
|
$this->args = $args; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getArguments(): array |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->args; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function setArguments(array $args): void |
|
142
|
|
|
{ |
|
143
|
|
|
$this->args = $args; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function setArgument(string $key, $val) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->args[$key] = $val; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function hasArgument($key) |
|
152
|
|
|
{ |
|
153
|
|
|
return \array_key_exists($key, $this->args); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function getArgument(string $key) |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->args[$key]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function offsetExists($offset) |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->hasArgument($offset); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function offsetGet($offset) |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->getArgument($offset); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function offsetSet($offset, $value) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->setArgument($offset, $value); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function offsetUnset($offset) |
|
177
|
|
|
{ |
|
178
|
|
|
if ($this->hasArgument($offset)) { |
|
179
|
|
|
unset($this->args[$offset]); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function getIterator() |
|
184
|
|
|
{ |
|
185
|
|
|
return new \ArrayIterator($this->args); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|