1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TEventSubscription classes |
4
|
|
|
* |
5
|
|
|
* @author Brad Anderson <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @link https://github.com/pradosoft/prado |
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Prado; |
12
|
|
|
|
13
|
|
|
use Prado\Exceptions\TInvalidOperationException; |
14
|
|
|
|
15
|
|
|
use ArrayAccess; |
16
|
|
|
use WeakReference; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* TEventSubscription class. |
20
|
|
|
* |
21
|
|
|
* This class subscribes an event handler to an event for a limited period of time. |
22
|
|
|
* |
23
|
|
|
* ```php |
24
|
|
|
* { |
25
|
|
|
* $exitLoop = false; |
26
|
|
|
* $subscription = new TEventSubscription($dispatcher, 'fxSignalInterrupt', |
27
|
|
|
* function ($sender, $param) use (&$exitLoop){$exitLoop = true;}); |
28
|
|
|
* ... |
29
|
|
|
* } // dereference unsubscribes |
30
|
|
|
* ``` |
31
|
|
|
* |
32
|
|
|
* @author Brad Anderson <[email protected]> |
33
|
|
|
* @since 4.2.3 |
34
|
|
|
*/ |
35
|
|
|
class TEventSubscription extends \Prado\Collections\TCollectionSubscription |
36
|
|
|
{ |
37
|
|
|
/** @var ?WeakReference The component with the event being subscribed. */ |
38
|
|
|
private ?WeakReference $_component = null; |
39
|
|
|
|
40
|
|
|
/** @var ?string The event being subscribed. */ |
41
|
|
|
private ?string $_event = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* @param TComponent $component The component with the event being subscribed. |
46
|
|
|
* @param mixed $event The event of the component being subscribed. |
47
|
|
|
* @param mixed $handler The handler being added to the event. |
48
|
|
|
* @param null|float|int $priority The priority of the handler in the event. |
49
|
|
|
* @param ?bool $autoSubscribe |
50
|
|
|
* @param mixed $index |
51
|
|
|
*/ |
52
|
|
|
public function __construct(?TComponent $component = null, mixed $event = null, mixed $handler = null, null|int|float $priority = null, ?bool $autoSubscribe = null, mixed $index = null) |
53
|
|
|
{ |
54
|
|
|
if ($component) { |
55
|
|
|
$this->setComponent($component); |
56
|
|
|
} |
57
|
|
|
if ($event) { |
58
|
|
|
$this->setEvent($event); |
59
|
|
|
} |
60
|
|
|
parent::__construct(null, $index, $handler, $priority, false, $autoSubscribe); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets the component event handler. |
65
|
|
|
* @param bool $weak |
66
|
|
|
* @return null|array|ArrayAccess|WeakReference The Event Handler from the component. |
67
|
|
|
*/ |
68
|
|
|
public function &getArray(bool $weak = false): array|ArrayAccess|WeakReference|null |
69
|
|
|
{ |
70
|
|
|
if ($handlers = parent::getArray($weak)) { |
71
|
|
|
return $handlers; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$component = $this->getComponent(); |
75
|
|
|
$event = $this->getEvent(); |
76
|
|
|
|
77
|
|
|
if ($event !== null && strncasecmp($event, 'fx', 2) === 0) { |
78
|
|
|
$component ??= Prado::getApplication(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!$component || !$event) { |
82
|
|
|
$return = null; |
83
|
|
|
return $return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!$component->hasEvent($event)) { |
87
|
|
|
$return = null; |
88
|
|
|
return $return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$handlers = $component->getEventHandlers($event); |
92
|
|
|
|
93
|
|
|
parent::setArray($handlers); |
94
|
|
|
|
95
|
|
|
return $handlers; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* The Array for a TEventSubscription cannot be set |
100
|
|
|
* @param null|array|ArrayAccess $value |
101
|
|
|
* @throws TInvalidOperationException This property cannot be set directly. |
102
|
|
|
*/ |
103
|
|
|
public function setArray(null|array|ArrayAccess &$value): static |
104
|
|
|
{ |
105
|
|
|
if ($value !== null) { |
|
|
|
|
106
|
|
|
throw new TInvalidOperationException('eventsubscription_no_setarray'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param bool $weak Return the WeakReference of the component, default false. |
114
|
|
|
* @return mixed The component with the event being subscribed. |
115
|
|
|
*/ |
116
|
|
|
public function getComponent(bool $weak = false): mixed |
117
|
|
|
{ |
118
|
|
|
if (!$weak && $this->_component) { |
119
|
|
|
return $this->_component->get(); |
120
|
|
|
} |
121
|
|
|
return $this->_component; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ?TComponent $value The component with the event being subscribed. |
126
|
|
|
* @throws TInvalidOperationException If the item is already subscribed. |
127
|
|
|
* @return static The current object. |
128
|
|
|
*/ |
129
|
|
|
public function setComponent(?TComponent $value): static |
130
|
|
|
{ |
131
|
|
|
if ($this->getIsSubscribed()) { |
132
|
|
|
throw new TInvalidOperationException('eventsubscription_no_change', 'Component'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($value !== null) { |
136
|
|
|
$value = WeakReference::create($value); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$events = null; |
140
|
|
|
parent::setArray($events); |
141
|
|
|
|
142
|
|
|
$this->_component = $value; |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return mixed The event being subscribed. |
149
|
|
|
*/ |
150
|
|
|
public function getEvent(): mixed |
151
|
|
|
{ |
152
|
|
|
return $this->_event; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param mixed $value The event being subscribed. |
157
|
|
|
* @throws TInvalidOperationException If the item is already subscribed. |
158
|
|
|
* @return static The current object. |
159
|
|
|
*/ |
160
|
|
|
public function setEvent(mixed $value): static |
161
|
|
|
{ |
162
|
|
|
if ($this->getIsSubscribed()) { |
163
|
|
|
throw new TInvalidOperationException('eventsubscription_no_change', 'Event'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$events = null; |
167
|
|
|
parent::setArray($events); |
168
|
|
|
|
169
|
|
|
if (is_string($value)) { |
170
|
|
|
$value = strtolower($value); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->_event = $value; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|