|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thruster\Component\Promise; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class LazyPromise |
|
7
|
|
|
* |
|
8
|
|
|
* @package Thruster\Component\Promise |
|
9
|
|
|
* @author Aurimas Niekis <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var callable |
|
15
|
|
|
*/ |
|
16
|
|
|
private $factory; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Promise |
|
20
|
|
|
*/ |
|
21
|
|
|
private $promise; |
|
22
|
|
|
|
|
23
|
118 |
|
public function __construct(callable $factory) |
|
24
|
|
|
{ |
|
25
|
118 |
|
$this->factory = $factory; |
|
26
|
118 |
|
} |
|
27
|
|
|
|
|
28
|
50 |
|
public function then( |
|
29
|
|
|
callable $onFulfilled = null, |
|
30
|
|
|
callable $onRejected = null, |
|
31
|
|
|
callable $onProgress = null |
|
32
|
|
|
) : PromiseInterface { |
|
33
|
50 |
|
return $this->promise()->then($onFulfilled, $onRejected, $onProgress); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
28 |
|
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
|
37
|
|
|
{ |
|
38
|
28 |
|
return $this->promise()->done($onFulfilled, $onRejected, $onProgress); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
7 |
|
public function otherwise(callable $onRejected) : ExtendedPromiseInterface |
|
42
|
|
|
{ |
|
43
|
7 |
|
return $this->promise()->otherwise($onRejected); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
22 |
|
public function always(callable $onFulfilledOrRejected) : ExtendedPromiseInterface |
|
47
|
|
|
{ |
|
48
|
22 |
|
return $this->promise()->always($onFulfilledOrRejected); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
public function progress(callable $onProgress) : ExtendedPromiseInterface |
|
52
|
|
|
{ |
|
53
|
2 |
|
return $this->promise()->progress($onProgress); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
15 |
|
public function cancel() |
|
57
|
|
|
{ |
|
58
|
15 |
|
return $this->promise()->cancel(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
117 |
|
private function promise() |
|
62
|
|
|
{ |
|
63
|
117 |
|
if (null === $this->promise) { |
|
64
|
|
|
try { |
|
65
|
117 |
|
$this->promise = resolve(call_user_func($this->factory)); |
|
66
|
1 |
|
} catch (\Exception $exception) { |
|
67
|
1 |
|
$this->promise = new RejectedPromise($exception); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
117 |
|
return $this->promise; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..