|
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
|
|
|
* Then Resolver class. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ThenResolver extends ObservableResolver implements PromisorInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var DeferredInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $deferred; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param callable $onFulfilled |
|
28
|
|
|
* @param callable $onRejected |
|
29
|
|
|
* @param callable $onNotify |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
callable $onFulfilled = null, |
|
33
|
|
|
callable $onRejected = null, |
|
34
|
|
|
callable $onNotify = null |
|
35
|
|
|
) { |
|
36
|
|
|
parent::__construct($onFulfilled, $onRejected, $onNotify); |
|
37
|
|
|
$this->deferred = new Deferred(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function reject($reason = null) |
|
44
|
|
|
{ |
|
45
|
|
|
try { |
|
46
|
|
|
parent::reject($reason); |
|
47
|
|
|
} catch (\Exception $e) { |
|
48
|
|
|
$reason = $e; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->deferred->reject($reason); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function promise() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->deferred->promise(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function onResolve($value = null) |
|
66
|
|
|
{ |
|
67
|
|
|
$value = $this->callResolveCallback($value); |
|
68
|
|
|
if ($value instanceof PromiseInterface) { |
|
69
|
|
|
$value->then(function ($actual) { |
|
70
|
|
|
$this->deferred->resolve($actual); |
|
71
|
|
|
}, function ($reason) { |
|
72
|
|
|
$this->deferred->reject($reason); |
|
73
|
|
|
}, function ($state) { |
|
74
|
|
|
$this->deferred->notify($state); |
|
75
|
|
|
}); |
|
76
|
|
|
} else { |
|
77
|
|
|
$this->deferred->resolve($value); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|