|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQL\Executor\Promise\Adapter; |
|
6
|
|
|
|
|
7
|
|
|
use Amp\Deferred; |
|
8
|
|
|
use Amp\Failure; |
|
9
|
|
|
use Amp\Promise as AmpPromise; |
|
10
|
|
|
use Amp\Success; |
|
11
|
|
|
use GraphQL\Executor\Promise\Promise; |
|
12
|
|
|
use GraphQL\Executor\Promise\PromiseAdapter; |
|
13
|
|
|
use Throwable; |
|
14
|
|
|
use function Amp\Promise\all; |
|
15
|
|
|
use function array_replace; |
|
16
|
|
|
|
|
17
|
|
|
class AmpPromiseAdapter implements PromiseAdapter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritdoc |
|
21
|
|
|
*/ |
|
22
|
|
|
public function isThenable($value) : bool |
|
23
|
|
|
{ |
|
24
|
|
|
return $value instanceof AmpPromise; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritdoc |
|
29
|
|
|
*/ |
|
30
|
|
|
public function convertThenable($thenable) : Promise |
|
31
|
|
|
{ |
|
32
|
|
|
return new Promise($thenable, $this); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritdoc |
|
37
|
|
|
*/ |
|
38
|
|
|
public function then(Promise $promise, ?callable $onFulfilled = null, ?callable $onRejected = null) : Promise |
|
39
|
|
|
{ |
|
40
|
|
|
$deferred = new Deferred(); |
|
41
|
|
|
$onResolve = static function (?Throwable $reason, $value) use ($onFulfilled, $onRejected, $deferred) : void { |
|
42
|
|
|
if ($reason === null && $onFulfilled !== null) { |
|
43
|
|
|
self::resolveWithCallable($deferred, $onFulfilled, $value); |
|
44
|
|
|
} elseif ($reason === null) { |
|
45
|
|
|
$deferred->resolve($value); |
|
46
|
|
|
} elseif ($onRejected !== null) { |
|
47
|
|
|
self::resolveWithCallable($deferred, $onRejected, $reason); |
|
48
|
|
|
} else { |
|
49
|
|
|
$deferred->fail($reason); |
|
50
|
|
|
} |
|
51
|
|
|
}; |
|
52
|
|
|
|
|
53
|
|
|
/** @var AmpPromise $adoptedPromise */ |
|
54
|
|
|
$adoptedPromise = $promise->adoptedPromise; |
|
55
|
|
|
$adoptedPromise->onResolve($onResolve); |
|
56
|
|
|
|
|
57
|
|
|
return new Promise($deferred->promise(), $this); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
*/ |
|
63
|
|
|
public function create(callable $resolver) : Promise |
|
64
|
|
|
{ |
|
65
|
|
|
$deferred = new Deferred(); |
|
66
|
|
|
|
|
67
|
|
|
$resolver( |
|
68
|
|
|
static function ($value) use ($deferred) : void { |
|
69
|
|
|
$deferred->resolve($value); |
|
70
|
|
|
}, |
|
71
|
|
|
static function (Throwable $exception) use ($deferred) : void { |
|
72
|
|
|
$deferred->fail($exception); |
|
73
|
|
|
} |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
return new Promise($deferred->promise(), $this); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
|
|
public function createFulfilled($value = null) : Promise |
|
83
|
|
|
{ |
|
84
|
|
|
$promise = new Success($value); |
|
85
|
|
|
|
|
86
|
|
|
return new Promise($promise, $this); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @inheritdoc |
|
91
|
|
|
*/ |
|
92
|
|
|
public function createRejected($reason) : Promise |
|
93
|
|
|
{ |
|
94
|
|
|
$promise = new Failure($reason); |
|
95
|
|
|
|
|
96
|
|
|
return new Promise($promise, $this); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritdoc |
|
101
|
|
|
*/ |
|
102
|
|
|
public function all(array $promisesOrValues) : Promise |
|
103
|
|
|
{ |
|
104
|
|
|
/** @var AmpPromise[] $promises */ |
|
105
|
|
|
$promises = []; |
|
106
|
|
|
foreach ($promisesOrValues as $key => $item) { |
|
107
|
|
|
if ($item instanceof Promise) { |
|
108
|
|
|
$promises[$key] = $item->adoptedPromise; |
|
109
|
|
|
} elseif ($item instanceof AmpPromise) { |
|
110
|
|
|
$promises[$key] = $item; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$deferred = new Deferred(); |
|
115
|
|
|
|
|
116
|
|
|
$onResolve = static function (?Throwable $reason, ?array $values) use ($promisesOrValues, $deferred) : void { |
|
117
|
|
|
if ($reason === null) { |
|
118
|
|
|
$deferred->resolve(array_replace($promisesOrValues, $values)); |
|
119
|
|
|
|
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$deferred->fail($reason); |
|
124
|
|
|
}; |
|
125
|
|
|
|
|
126
|
|
|
all($promises)->onResolve($onResolve); |
|
127
|
|
|
|
|
128
|
|
|
return new Promise($deferred->promise(), $this); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
private static function resolveWithCallable(Deferred $deferred, callable $callback, $argument) : void |
|
132
|
|
|
{ |
|
133
|
|
|
try { |
|
134
|
|
|
$result = $callback($argument); |
|
135
|
|
|
} catch (Throwable $exception) { |
|
136
|
|
|
$deferred->fail($exception); |
|
137
|
|
|
|
|
138
|
|
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($result instanceof Promise) { |
|
142
|
|
|
$result = $result->adoptedPromise; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$deferred->resolve($result); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|