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\LazyPromise; |
16
|
|
|
use Amp\Coroutine; |
17
|
|
|
use PHPinnacle\Identity\UUID; |
18
|
|
|
|
19
|
|
|
final class Processor implements Contract\Processor |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var callable[] |
23
|
|
|
*/ |
24
|
|
|
private $interruptions = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
5 |
|
public function interrupt(string $interrupt, callable $handler): void |
30
|
|
|
{ |
31
|
5 |
|
$this->interruptions[$interrupt] = $handler; |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
8 |
|
public function execute(callable $handler, array $arguments): Task |
38
|
|
|
{ |
39
|
8 |
|
$id = UUID::random(); |
40
|
8 |
|
$token = new Token($id); |
41
|
|
|
|
42
|
8 |
|
return new Task($id, new LazyPromise(function () use ($handler, $arguments, $token) { |
43
|
8 |
|
return $this->adapt($handler(...$arguments), $token); |
44
|
8 |
|
}), $token); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $value |
49
|
|
|
* @param Token $token |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
7 |
|
private function adapt($value, Token $token) |
54
|
|
|
{ |
55
|
7 |
|
return $value instanceof \Generator ? new Coroutine($this->recoil($value, $token)) : $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param \Generator $generator |
60
|
|
|
* @param Token $token |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
3 |
|
private function recoil(\Generator $generator, Token $token) |
65
|
|
|
{ |
66
|
3 |
|
while ($generator->valid()) { |
67
|
3 |
|
$token->guard(); |
68
|
|
|
|
69
|
|
|
try { |
70
|
3 |
|
$value = $this->intercept($generator->key(), $generator->current()); |
71
|
|
|
|
72
|
3 |
|
$generator->send(yield $this->adapt($value, $token)); |
73
|
2 |
|
} catch (\Exception $error) { |
74
|
|
|
/** @scrutinizer ignore-call */ |
75
|
1 |
|
$generator->throw($error); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $this->adapt($generator->getReturn(), $token); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int|string $key |
84
|
|
|
* @param mixed $value |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
3 |
|
private function intercept($key, $value) |
89
|
|
|
{ |
90
|
3 |
|
$interrupt = \is_string($key) ? $key : $value; |
91
|
|
|
|
92
|
3 |
|
if (\is_object($interrupt)) { |
93
|
2 |
|
$interrupt = \get_class($interrupt); |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
if (!\is_string($interrupt) || !isset($this->interruptions[$interrupt])) { |
97
|
2 |
|
return $value; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$interceptor = $this->interruptions[$interrupt]; |
101
|
|
|
|
102
|
1 |
|
$value = \is_array($value) ? $value : [$value]; |
103
|
|
|
|
104
|
1 |
|
return $interceptor(...$value); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|