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
|
|
|
namespace PHPinnacle\Ensign; |
12
|
|
|
|
13
|
|
|
use Amp\Coroutine; |
14
|
|
|
use Amp\LazyPromise; |
15
|
|
|
use PHPinnacle\Ensign\Exception\BadActionCall; |
16
|
|
|
use PHPinnacle\Identity\UUID; |
17
|
|
|
|
18
|
|
|
final class Kernel |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Processor |
22
|
|
|
*/ |
23
|
|
|
private $processor; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var callable[] |
27
|
|
|
*/ |
28
|
|
|
private $interruptions = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Processor $processor |
32
|
|
|
*/ |
33
|
8 |
|
public function __construct(Processor $processor = null) |
34
|
|
|
{ |
35
|
8 |
|
$this->processor = $processor ?: new Processor\SimpleProcessor(); |
36
|
8 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $interrupt |
40
|
|
|
* @param callable $handler |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
5 |
|
public function interrupt(string $interrupt, callable $handler): void |
45
|
|
|
{ |
46
|
5 |
|
$this->interruptions[$interrupt] = $handler; |
47
|
5 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param callable $handler |
51
|
|
|
* @param array $arguments |
52
|
|
|
* |
53
|
|
|
* @return Action |
54
|
|
|
*/ |
55
|
8 |
|
public function execute(callable $handler, array $arguments): Action |
56
|
|
|
{ |
57
|
8 |
|
$id = UUID::random(); |
58
|
|
|
|
59
|
8 |
|
$token = new Token($id); |
60
|
8 |
|
$promise = new LazyPromise(function () use ($handler, $arguments, $token) { |
61
|
8 |
|
return $this->adapt($this->processor->execute($handler, $arguments), $token); |
62
|
8 |
|
}); |
63
|
|
|
|
64
|
8 |
|
return new Action($id, $promise, $token); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param mixed $value |
69
|
|
|
* @param Token $token |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
7 |
|
private function adapt($value, Token $token) |
74
|
|
|
{ |
75
|
7 |
|
return $value instanceof \Generator ? new Coroutine($this->recoil($value, $token)) : $value; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param \Generator $generator |
80
|
|
|
* @param Token $token |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
3 |
|
private function recoil(\Generator $generator, Token $token) |
85
|
|
|
{ |
86
|
3 |
|
$step = 1; |
87
|
|
|
|
88
|
3 |
|
while ($generator->valid()) { |
89
|
3 |
|
$token->guard(); |
90
|
|
|
|
91
|
|
|
try { |
92
|
3 |
|
$value = $this->intercept($generator->key(), $generator->current()); |
93
|
|
|
|
94
|
3 |
|
$generator->send(yield $this->adapt($value, $token)); |
95
|
2 |
|
} catch (\Exception $error) { |
96
|
1 |
|
$this->throw($generator, $error, $step); |
97
|
2 |
|
} finally { |
98
|
3 |
|
$step++; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
return $this->adapt($generator->getReturn(), $token); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \Generator $generator |
107
|
|
|
* @param \Exception $error |
108
|
|
|
* @param int $step |
109
|
|
|
*/ |
110
|
1 |
|
private function throw(\Generator $generator, \Exception $error, int $step) |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
1 |
|
$generator->throw($error); |
|
|
|
|
114
|
|
|
} catch (\Throwable $error) { |
115
|
|
|
throw new BadActionCall($step, $error); |
116
|
|
|
} |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param int|string $key |
121
|
|
|
* @param mixed $value |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
3 |
|
private function intercept($key, $value) |
126
|
|
|
{ |
127
|
3 |
|
$interrupt = \is_string($key) ? $key : $value; |
128
|
|
|
|
129
|
3 |
|
if (\is_object($interrupt)) { |
130
|
2 |
|
$interrupt = \get_class($interrupt); |
131
|
|
|
} |
132
|
|
|
|
133
|
3 |
|
if (!\is_string($interrupt) || !isset($this->interruptions[$interrupt])) { |
134
|
2 |
|
return $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
$value = \is_array($value) ? $value : [$value]; |
138
|
|
|
|
139
|
1 |
|
return $this->processor->execute($this->interruptions[$interrupt], $value); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|