|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\Travis; |
|
4
|
|
|
|
|
5
|
|
|
use React\Promise\RejectedPromise; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class LazyPromise transformed into a trait |
|
9
|
|
|
* @source https://github.com/reactphp/promise/blob/fe0d75d660486677791fde20e82d263ab9d1c936/src/LazyPromise.php |
|
10
|
|
|
* @package WyriHaximus\Travis |
|
11
|
|
|
*/ |
|
12
|
|
|
trait LazyPromiseTrait |
|
13
|
|
|
{ |
|
14
|
|
|
private $factory; |
|
15
|
|
|
private $promise; |
|
16
|
|
|
|
|
17
|
|
|
public function setFactory(callable $factory) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->factory = $factory; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->promise()->then($onFulfilled, $onRejected, $onProgress); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->promise()->done($onFulfilled, $onRejected, $onProgress); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function otherwise(callable $onRejected) |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->promise()->otherwise($onRejected); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function always(callable $onFulfilledOrRejected) |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
return $this->promise()->always($onFulfilledOrRejected); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function progress(callable $onProgress) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->promise()->progress($onProgress); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function cancel() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->promise()->cancel(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function promise() |
|
53
|
|
|
{ |
|
54
|
|
|
if (null === $this->promise) { |
|
55
|
|
|
try { |
|
56
|
|
|
$this->promise = \React\Promise\resolve(call_user_func($this->factory)); |
|
57
|
|
|
} catch (\Exception $exception) { |
|
58
|
|
|
$this->promise = new RejectedPromise($exception); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this->promise; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.