|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHPinnacle/Ensign. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) PHPinnacle Team <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace PHPinnacle\Ensign; |
|
14
|
|
|
|
|
15
|
|
|
use Amp\Coroutine; |
|
16
|
|
|
use Amp\Promise; |
|
17
|
|
|
|
|
18
|
|
|
final class Subroutine implements Promise |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Executor |
|
22
|
|
|
*/ |
|
23
|
|
|
private $generator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var callable |
|
27
|
|
|
*/ |
|
28
|
|
|
private $resolver; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param \Generator $generator |
|
32
|
|
|
* @param callable $resolver |
|
33
|
|
|
*/ |
|
34
|
4 |
|
public function __construct(\Generator $generator, callable $resolver) |
|
35
|
|
|
{ |
|
36
|
4 |
|
$this->generator = $generator; |
|
|
|
|
|
|
37
|
4 |
|
$this->resolver = $resolver; |
|
38
|
4 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function onResolve(callable $handler) |
|
44
|
|
|
{ |
|
45
|
4 |
|
$coroutine = new Coroutine($this->recoil()); |
|
46
|
4 |
|
$coroutine->onResolve($handler); |
|
47
|
4 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return \Generator |
|
51
|
|
|
*/ |
|
52
|
4 |
|
private function recoil(): \Generator |
|
53
|
|
|
{ |
|
54
|
4 |
|
$step = 0; |
|
55
|
|
|
|
|
56
|
4 |
|
while ($this->generator->valid()) { |
|
|
|
|
|
|
57
|
|
|
try { |
|
58
|
|
|
// yield new Promise |
|
59
|
|
|
// yield [$promiseOne, $promiseTwo] |
|
60
|
|
|
|
|
61
|
|
|
// yield new Signal |
|
62
|
|
|
// yield new Signal => 'arg' |
|
63
|
|
|
// yield new Signal => ['arg', 'two'] |
|
64
|
|
|
// yield Signal::class => [new Signal, 'arg', 'two'] |
|
65
|
|
|
// yield 'signal' |
|
66
|
|
|
// yield 'signal' => 'arg' |
|
67
|
|
|
// yield 'signal' => ['arg', 'two'] |
|
68
|
4 |
|
$current = $this->generator->current(); |
|
|
|
|
|
|
69
|
4 |
|
$key = $this->generator->key(); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
4 |
|
if ($key === $step) { |
|
72
|
4 |
|
$interrupt = $current; |
|
73
|
4 |
|
$arguments = []; |
|
74
|
|
|
} else { |
|
75
|
2 |
|
$interrupt = $key; |
|
76
|
2 |
|
$arguments = \is_array($current) ? $current : [$current]; |
|
77
|
|
|
|
|
78
|
2 |
|
$step--; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
if (\is_array($interrupt)) { |
|
82
|
|
|
$interrupt = Promise\all($interrupt); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
if (!$interrupt instanceof Promise) { |
|
86
|
3 |
|
$interrupt = $this->intercept($interrupt, $arguments); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
$this->generator->send(yield $interrupt); |
|
|
|
|
|
|
90
|
2 |
|
} catch (\Throwable $error) { |
|
91
|
2 |
|
$this->throw($error, $step); |
|
92
|
3 |
|
} finally { |
|
93
|
4 |
|
$step++; |
|
94
|
|
|
} |
|
95
|
|
|
}; |
|
96
|
|
|
|
|
97
|
3 |
|
return $this->generator->getReturn(); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param \Throwable $error |
|
102
|
|
|
* @param int $step |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
2 |
|
private function throw(\Throwable $error, int $step): void |
|
107
|
|
|
{ |
|
108
|
|
|
try { |
|
109
|
2 |
|
$this->generator->throw($error); |
|
|
|
|
|
|
110
|
1 |
|
} catch (\Throwable $error) { |
|
111
|
1 |
|
throw new Exception\BadActionCall($step, $error); |
|
112
|
|
|
} |
|
113
|
1 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param mixed $interrupt |
|
117
|
|
|
* @param array $arguments |
|
118
|
|
|
* |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
3 |
|
private function intercept($interrupt, array $arguments) |
|
122
|
|
|
{ |
|
123
|
3 |
|
if (\is_object($interrupt)) { |
|
124
|
2 |
|
\array_unshift($arguments, $interrupt); |
|
125
|
|
|
|
|
126
|
2 |
|
$interrupt = \get_class($interrupt); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
3 |
|
return \call_user_func($this->resolver, $interrupt, $arguments); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..