1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Async\Promise; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Promise class. |
16
|
|
|
* |
17
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Promise extends AbstractPromise |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var DeferredInterface |
23
|
|
|
*/ |
24
|
|
|
private $deferred; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param callable $exportResolve |
28
|
|
|
* @param callable $exportReject |
29
|
|
|
* @param callable $exportNotify |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
callable $exportResolve, |
33
|
|
|
callable $exportReject, |
34
|
|
|
callable $exportNotify |
35
|
|
|
) { |
36
|
|
|
$this->deferred = new PromiseDeferred(); |
37
|
|
|
|
38
|
|
|
$exportResolve(function ($value = null) { |
39
|
|
|
return $this->resolve($value); |
40
|
|
|
}); |
41
|
|
|
$exportReject(function ($reason = null) { |
42
|
|
|
return $this->reject($reason); |
43
|
|
|
}); |
44
|
|
|
$exportNotify(function ($state = null) { |
45
|
|
|
return $this->notify($state); |
46
|
|
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null) |
53
|
|
|
{ |
54
|
|
|
return $this->deferred->promise()->then($onFulfilled, $onRejected, $onNotify); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null) |
61
|
|
|
{ |
62
|
|
|
$this->deferred->promise()->done($onFulfilled, $onRejected, $onNotify); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function state() |
69
|
|
|
{ |
70
|
|
|
return $this->deferred->promise()->state(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $value |
75
|
|
|
*/ |
76
|
|
|
protected function resolve($value = null) |
77
|
|
|
{ |
78
|
|
|
$this->deferred->resolve($value); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $reason |
83
|
|
|
*/ |
84
|
|
|
protected function reject($reason = null) |
85
|
|
|
{ |
86
|
|
|
$this->deferred->reject($reason); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $state |
91
|
|
|
*/ |
92
|
|
|
protected function notify($state = null) |
93
|
|
|
{ |
94
|
|
|
$this->deferred->notify($state); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|