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