1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gandung\Promise; |
4
|
|
|
|
5
|
|
|
use TaskQueue\TaskQueue; |
6
|
|
|
|
7
|
|
|
class Promise implements PromiseInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var integer |
11
|
|
|
*/ |
12
|
|
|
private $state = self::STATE_PENDING; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var mixed |
16
|
|
|
*/ |
17
|
|
|
private $current; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $context = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Closure |
26
|
|
|
*/ |
27
|
|
|
private $waitCallback; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Closure |
31
|
|
|
*/ |
32
|
|
|
private $cancelCallback; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var boolean |
36
|
|
|
*/ |
37
|
|
|
private $isStateTransformed; |
38
|
|
|
|
39
|
|
|
public function then($onFulfilled = null, $onRejected = null) |
40
|
|
|
{ |
41
|
|
|
if ($this->state === self::STATE_PENDING) { |
42
|
|
|
$q = new Promise(); |
43
|
|
|
$this->context[] = [$q, $onFulfilled, $onRejected]; |
44
|
|
|
|
45
|
|
|
return $q; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($this->state === self::STATE_FULFILLED) { |
49
|
|
|
return $onFulfilled |
50
|
|
|
? (new FulfilledPromise($this->current))->then($onFulfilled, null) |
51
|
|
|
: new FulfilledPromise($this->current); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $onRejected |
55
|
|
|
? (new RejectedPromise($this->current))->then(null, $onRejected) |
56
|
|
|
: new RejectedPromise($this->current); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setWaitCallback(\Closure $callback = null) |
60
|
|
|
{ |
61
|
|
|
if (!($callback instanceof \Closure)) { |
62
|
|
|
throw new \InvalidArgumentException( |
63
|
|
|
sprintf("Parameter 1 of %s must be a valid callback.", __METHOD__) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->waitCallback = $callback; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setCancelCallback(\Closure $callback = null) |
71
|
|
|
{ |
72
|
|
|
if (!($callback instanceof \Closure)) { |
73
|
|
|
throw new \InvalidArgumentException( |
74
|
|
|
sprintf("Parameter 1 of %s must be a valid callback.", __METHOD__) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->cancelCallback = $callback; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function validateState($value, $state) |
82
|
|
|
{ |
83
|
|
|
if ($this->state !== self::STATE_PENDING) { |
84
|
|
|
if ($value === $this->current && $state === $this->state) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$prevStatus = $this->state; |
89
|
|
|
$currentStatus = $state; |
90
|
|
|
|
91
|
|
|
throw $this->state === $state |
92
|
|
|
? new \LogicException( |
93
|
|
|
sprintf("State of the promise is already %s", $prevStatus == 4 ? 'fulfilled' : 'rejected') |
94
|
|
|
) |
95
|
|
|
: new \LogicException( |
96
|
|
|
sprintf( |
97
|
|
|
"Unable to change %s promise to %s", |
98
|
|
|
$prevStatus == 4 ? 'fulfilled' : 'rejected', |
99
|
|
|
$currentStatus == 4 ? 'fulfilled' : 'rejected' |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($value === $this) { |
105
|
|
|
throw new \LogicException( |
106
|
|
|
"Unable to fulfill or reject a promise with itself." |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function resolve($value) |
114
|
|
|
{ |
115
|
|
|
$this->isStateTransformed = $this->validateState($value, self::STATE_FULFILLED); |
116
|
|
|
|
117
|
|
|
if ($this->isStateTransformed) { |
118
|
|
|
$this->setState(self::STATE_FULFILLED); |
119
|
|
|
$this->trigger($value); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function reject($reason) |
124
|
|
|
{ |
125
|
|
|
$this->isStateTransformed = $this->validateState($reason, self::STATE_REJECTED); |
126
|
|
|
|
127
|
|
|
if ($this->isStateTransformed) { |
128
|
|
|
$this->setState(self::STATE_REJECTED); |
129
|
|
|
$this->trigger($reason); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function wait() |
134
|
|
|
{ |
135
|
|
|
$this->waitInPendingState(); |
136
|
|
|
|
137
|
|
|
$q = $this->current instanceof PromiseInterface |
138
|
|
|
? $this->current->wait() |
|
|
|
|
139
|
|
|
: $this->current; |
140
|
|
|
|
141
|
|
|
if ($this->current instanceof PromiseInterface || $this->currentState() === self::STATE_FULFILLED) { |
142
|
|
|
return $q; |
143
|
|
|
} |
144
|
|
|
else { |
145
|
|
|
throw $q instanceof \Exception |
146
|
|
|
? $q |
147
|
|
|
: new \Exception($q); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function cancel($callback = null) |
152
|
|
|
{ |
153
|
|
|
if ($this->cancelCallback === null) { |
154
|
|
|
if (!($callback instanceof \Closure)) { |
155
|
|
|
throw new \LogicException( |
156
|
|
|
sprintf( |
157
|
|
|
"Default cancellation callback resolver is not set. " . |
158
|
|
|
"Cancellation mechanism is impossible to invoked because %s is called " . |
159
|
|
|
"without callback resolver.", __METHOD__ |
160
|
|
|
) |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->cancelCallback = $callback; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($this->currentState() !== self::STATE_PENDING) { |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($this->cancelCallback) { |
172
|
|
|
$cancelCallback = $this->cancelCallback; |
173
|
|
|
$this->cancelCallback = null; |
174
|
|
|
|
175
|
|
|
try { |
176
|
|
|
$cancelCallback(); |
177
|
|
|
} catch (\Exception $e) { |
178
|
|
|
$this->reject($e); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if ($this->currentState() === self::STATE_PENDING) { |
183
|
|
|
$this->reject(new \Exception('Promise has been cancelled.')); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$e = $this->current instanceof PromiseInterface |
187
|
|
|
? $this->current->cancel() |
|
|
|
|
188
|
|
|
: $this->current; |
189
|
|
|
|
190
|
|
|
if ($this->current instanceof PromiseInterface || $this->currentState() === self::STATE_REJECTED) { |
191
|
|
|
return $e; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function currentState() |
196
|
|
|
{ |
197
|
|
|
return $this->state; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function trigger($value) |
201
|
|
|
{ |
202
|
|
|
$context = $this->context; |
203
|
|
|
$this->context = null; |
|
|
|
|
204
|
|
|
$this->current = $value; |
205
|
|
|
$this->waitCallback = null; |
206
|
|
|
$this->cancelCallback = null; |
207
|
|
|
|
208
|
|
|
if (!method_exists($value, 'then')) { |
209
|
|
|
$index = $this->state === self::STATE_FULFILLED ? 1 : 2; |
210
|
|
|
|
211
|
|
|
Context\ContextStack::create(new TaskQueue)->store( |
212
|
|
|
static function () use ($index, $context, $value) { |
213
|
|
|
foreach ($context as $c) { |
214
|
|
|
self::invokeContext($c, $index, $value); |
215
|
|
|
} |
216
|
|
|
}); |
217
|
|
|
|
218
|
|
|
Context\ContextStack::getQueueHandler()->run(); |
|
|
|
|
219
|
|
|
} elseif ($value instanceof Promise && $value->currentState() === self::STATE_PENDING) { |
220
|
|
|
$value->context = array_merge($value->context, $context); |
221
|
|
|
} else { |
222
|
|
|
$value->then( |
223
|
|
|
static function ($value) use ($context) { |
224
|
|
|
foreach ($context as $c) { |
225
|
|
|
self::invokeContext($c, 1, $value); |
226
|
|
|
} |
227
|
|
|
}, |
228
|
|
|
static function ($reason) use ($context) { |
229
|
|
|
foreach ($context as $c) { |
230
|
|
|
self::invokeContext($c, 2, $reason); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
private static function invokeContext($context, $index, $value) |
238
|
|
|
{ |
239
|
|
|
$promise = $context[0]; |
240
|
|
|
|
241
|
|
|
if ($promise->currentState() !== self::STATE_PENDING) { |
242
|
|
|
return; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
try { |
246
|
|
|
if (isset($context[$index])) { |
247
|
|
|
$promise->resolve($context[$index]($value)); |
248
|
|
|
} elseif ($index === 1) { |
249
|
|
|
$promise->resolve($value); |
250
|
|
|
} else { |
251
|
|
|
$promise->reject($value); |
252
|
|
|
} |
253
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
254
|
|
|
$promise->reject($e); |
255
|
|
|
} catch (\Exception $e) { |
256
|
|
|
$promise->reject($e); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
private function setState($state) |
261
|
|
|
{ |
262
|
|
|
if ($state !== self::STATE_PENDING && |
263
|
|
|
$state !== self::STATE_FULFILLED && |
264
|
|
|
$state !== self::STATE_REJECTED) { |
265
|
|
|
throw new \InvalidArgumentException( |
266
|
|
|
sprintf("Parameter 1 of %s must be a valid promise state.", __METHOD__) |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$this->state = $state; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
private function waitInPendingState() |
274
|
|
|
{ |
275
|
|
|
if ($this->currentState() !== self::STATE_PENDING) { |
276
|
|
|
return; |
277
|
|
|
} else if ($this->waitCallback) { |
278
|
|
|
try { |
279
|
|
|
$waitCallback = $this->waitCallback; |
280
|
|
|
$this->waitCallback = null; |
281
|
|
|
$waitCallback(); |
282
|
|
|
} catch (\Exception $e) { |
283
|
|
|
if ($this->currentState() === self::STATE_PENDING) { |
284
|
|
|
$this->reject($e); |
285
|
|
|
} else { |
286
|
|
|
throw $e; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
Context\ContextStack::getQueueHandler()->run(); |
|
|
|
|
292
|
|
|
|
293
|
|
|
if ($this->currentState() === self::STATE_PENDING) { |
294
|
|
|
$this->reject( |
295
|
|
|
'Invoking the synchronous wait callback resolver didn\'t resolve the current promise' |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: