1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
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 Overblog\GraphQLBundle\Executor\Promise\Adapter; |
13
|
|
|
|
14
|
|
|
use GraphQL\Executor\ExecutionResult; |
15
|
|
|
use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter as BaseReactPromiseAdapter; |
16
|
|
|
use GraphQL\Executor\Promise\Promise; |
17
|
|
|
use Overblog\GraphQLBundle\Executor\Promise\PromiseAdapterInterface; |
18
|
|
|
|
19
|
|
|
class ReactPromiseAdapter extends BaseReactPromiseAdapter implements PromiseAdapterInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
10 |
|
public function isThenable($value) |
25
|
|
|
{ |
26
|
10 |
|
return parent::isThenable($value instanceof Promise ? $value->adoptedPromise : $value); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
9 |
|
public function convertThenable($thenable) |
33
|
|
|
{ |
34
|
9 |
|
if ($thenable instanceof Promise) { |
35
|
9 |
|
return $thenable; |
36
|
|
|
} |
37
|
|
|
|
38
|
9 |
|
return parent::convertThenable($thenable); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Synchronously wait when promise completes. |
43
|
|
|
* |
44
|
|
|
* @param Promise $promise |
45
|
|
|
* |
46
|
|
|
* @return ExecutionResult |
47
|
|
|
* |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
10 |
|
public function wait(Promise $promise) |
51
|
|
|
{ |
52
|
10 |
|
if (!$this->isThenable($promise)) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
10 |
|
$wait = true; |
56
|
10 |
|
$resolvedValue = null; |
57
|
10 |
|
$exception = null; |
58
|
|
|
/** @var \React\Promise\PromiseInterface $reactPromise */ |
59
|
10 |
|
$reactPromise = $promise->adoptedPromise; |
60
|
|
|
|
61
|
|
|
$reactPromise->then(function ($values) use (&$resolvedValue, &$wait) { |
62
|
10 |
|
$resolvedValue = $values; |
63
|
10 |
|
$wait = false; |
64
|
10 |
|
}, function ($reason) use (&$exception, &$wait) { |
65
|
|
|
$exception = $reason; |
66
|
|
|
$wait = false; |
67
|
10 |
|
}); |
68
|
|
|
|
69
|
|
|
// wait until promise resolution |
70
|
10 |
|
while ($wait) { |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @var \Exception|null $exception */ |
74
|
10 |
|
if (null !== $exception) { |
75
|
|
|
throw $exception; |
76
|
|
|
} |
77
|
|
|
|
78
|
10 |
|
return $resolvedValue; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check looks for
while
loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.Consider removing the loop.