1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Promise; |
4
|
|
|
|
5
|
|
|
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException; |
6
|
|
|
use Dazzle\Throwable\Exception\Runtime\CancellationException; |
7
|
|
|
use Error; |
8
|
|
|
use Exception; |
9
|
|
|
|
10
|
|
|
class PromiseCancelled implements PromiseInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Error|Exception|string|null |
14
|
|
|
*/ |
15
|
|
|
protected $reason; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Error|Exception|string|null $reason |
19
|
|
|
* @throws InvalidArgumentException |
20
|
|
|
*/ |
21
|
140 |
|
public function __construct($reason = null) |
22
|
|
|
{ |
23
|
140 |
|
if ($reason instanceof PromiseInterface) |
24
|
|
|
{ |
25
|
1 |
|
throw new InvalidArgumentException( |
26
|
1 |
|
'You cannot create PromiseCancelled with a promise. Use Promise::doCancel($promiseOrValue) instead.' |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
140 |
|
$this->reason = $reason; |
31
|
140 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
*/ |
36
|
134 |
|
public function __destruct() |
37
|
|
|
{ |
38
|
134 |
|
unset($this->reason); |
39
|
134 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @override |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
82 |
|
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
46
|
|
|
{ |
47
|
82 |
|
if (null === $onCancel) |
48
|
|
|
{ |
49
|
31 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
try |
53
|
|
|
{ |
54
|
81 |
|
return Promise::doResolve($onCancel($this->getReason())) |
55
|
81 |
|
->then( |
56
|
|
|
function() { |
57
|
81 |
|
return Promise::doCancel($this->getReason()); |
58
|
81 |
|
}, |
59
|
|
|
function() { |
60
|
6 |
|
return Promise::doCancel($this->getReason()); |
61
|
81 |
|
} |
62
|
|
|
); |
63
|
|
|
} |
64
|
5 |
|
catch (Error $ex) |
|
|
|
|
65
|
|
|
{} |
66
|
5 |
|
catch (Exception $ex) |
|
|
|
|
67
|
|
|
{} |
68
|
|
|
|
69
|
5 |
|
return Promise::doCancel($this->getReason()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @override |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
65 |
View Code Duplication |
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
|
|
|
|
77
|
|
|
{ |
78
|
65 |
|
if (null === $onCancel) |
79
|
|
|
{ |
80
|
6 |
|
$this->throwError($this->getReason()); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
59 |
|
$result = $onCancel($this->getReason()); |
84
|
|
|
|
85
|
54 |
|
if ($result instanceof self) |
86
|
|
|
{ |
87
|
11 |
|
$this->throwError($result->getReason()); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
43 |
|
if ($result instanceof PromiseInterface) |
91
|
|
|
{ |
92
|
|
|
$result->done(); |
93
|
|
|
} |
94
|
43 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @override |
98
|
|
|
* @inheritDoc |
99
|
|
|
*/ |
100
|
2 |
|
public function spread(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
101
|
|
|
{ |
102
|
2 |
|
return $this->then( |
103
|
2 |
|
null, |
104
|
2 |
|
null, |
105
|
|
|
function($reasons) use($onCancel) { |
106
|
2 |
|
return $onCancel(...((array) $reasons)); |
107
|
2 |
|
} |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @override |
113
|
|
|
* @inheritDoc |
114
|
|
|
*/ |
115
|
1 |
|
public function success(callable $onSuccess) |
116
|
|
|
{ |
117
|
1 |
|
return $this->then($onSuccess); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @override |
122
|
|
|
* @inheritDoc |
123
|
|
|
*/ |
124
|
1 |
|
public function failure(callable $onFailure) |
125
|
|
|
{ |
126
|
1 |
|
return $this->then(null, $onFailure); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @override |
131
|
|
|
* @inheritDoc |
132
|
|
|
*/ |
133
|
1 |
|
public function abort(callable $onCancel) |
134
|
|
|
{ |
135
|
1 |
|
return $this->then(null, null, $onCancel); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @override |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
5 |
View Code Duplication |
public function always(callable $onFulfilledOrRejected) |
|
|
|
|
143
|
|
|
{ |
144
|
5 |
|
return $this->then( |
145
|
5 |
|
null, |
146
|
5 |
|
null, |
147
|
|
|
function($reason) use($onFulfilledOrRejected) { |
148
|
4 |
|
return Promise::doResolve($onFulfilledOrRejected())->then(function() use($reason) { |
149
|
3 |
|
return new static($reason); |
150
|
4 |
|
}); |
151
|
5 |
|
} |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @override |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
1 |
|
public function isPending() |
160
|
|
|
{ |
161
|
1 |
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @override |
166
|
|
|
* @inheritDoc |
167
|
|
|
*/ |
168
|
3 |
|
public function isFulfilled() |
169
|
|
|
{ |
170
|
3 |
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @override |
175
|
|
|
* @inheritDoc |
176
|
|
|
*/ |
177
|
3 |
|
public function isRejected() |
178
|
|
|
{ |
179
|
3 |
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @override |
184
|
|
|
* @inheritDoc |
185
|
|
|
*/ |
186
|
5 |
|
public function isCancelled() |
187
|
|
|
{ |
188
|
5 |
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @override |
193
|
|
|
* @inheritDoc |
194
|
|
|
*/ |
195
|
|
|
public function getPromise() |
196
|
|
|
{ |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @override |
202
|
|
|
* @inheritDoc |
203
|
|
|
*/ |
204
|
1 |
|
public function resolve($value = null) |
205
|
|
|
{ |
206
|
1 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @override |
211
|
|
|
* @inheritDoc |
212
|
|
|
*/ |
213
|
1 |
|
public function reject($reason = null) |
214
|
|
|
{ |
215
|
1 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @override |
220
|
|
|
* @inheritDoc |
221
|
|
|
*/ |
222
|
1 |
|
public function cancel($reason = null) |
223
|
|
|
{ |
224
|
1 |
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @see Promise::getValue |
229
|
|
|
*/ |
230
|
|
|
protected function getValue() |
231
|
|
|
{ |
232
|
|
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @see Promise::getReason |
237
|
|
|
*/ |
238
|
112 |
|
protected function getReason() |
239
|
|
|
{ |
240
|
112 |
|
return $this->reason; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param Error|Exception|string $reason |
245
|
|
|
* @throws Error|Exception |
246
|
|
|
*/ |
247
|
17 |
|
protected function throwError($reason) |
248
|
|
|
{ |
249
|
17 |
|
if ($reason instanceof Error || $reason instanceof Exception) |
250
|
|
|
{ |
251
|
6 |
|
throw $reason; |
252
|
|
|
} |
253
|
|
|
|
254
|
11 |
|
throw new CancellationException($reason); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|