1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheAentMachine\Aenthill; |
5
|
|
|
|
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
use TheAentMachine\ReplyAggregator; |
8
|
|
|
|
9
|
|
|
class Aenthill |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Updates current aent in the manifest. |
13
|
|
|
* |
14
|
|
|
* @param null|array<string,string> $metadata |
15
|
|
|
* @param null|string[] $events |
16
|
|
|
*/ |
17
|
|
|
public static function update(?array $metadata = null, ?array $events = null): void |
18
|
|
|
{ |
19
|
|
|
$command = ['aenthill', 'update']; |
20
|
|
|
if (!empty($metadata)) { |
21
|
|
|
foreach ($metadata as $key => $value) { |
22
|
|
|
$command[] = '-m'; |
23
|
|
|
$command[] = $key . '=' . $value; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
if (!empty($events)) { |
27
|
|
|
foreach ($events as $event) { |
28
|
|
|
$command[] = '-e'; |
29
|
|
|
$command[] = $event; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
$process = new Process($command); |
33
|
|
|
$process->enableOutput(); |
34
|
|
|
$process->setTty(true); |
35
|
|
|
$process->mustRun(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Adds a dependency in manifest to current aent. |
40
|
|
|
* |
41
|
|
|
* @param string $image |
42
|
|
|
* @param string $key |
43
|
|
|
* @param null|array<string,string> $metadata |
44
|
|
|
*/ |
45
|
|
|
public static function register(string $image, string $key, ?array $metadata = null): void |
46
|
|
|
{ |
47
|
|
|
$command = ['aenthill', 'register', $image, $key]; |
48
|
|
|
if (!empty($metadata)) { |
49
|
|
|
foreach ($metadata as $k => $value) { |
50
|
|
|
$command[] = '-m'; |
51
|
|
|
$command[] = $k . '=' . $value; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
$process = new Process($command); |
55
|
|
|
$process->enableOutput(); |
56
|
|
|
$process->setTty(true); |
57
|
|
|
$process->mustRun(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Starts an aent. |
63
|
|
|
* |
64
|
|
|
* @param string $target the image name or a key from the manifest. |
65
|
|
|
* @param string $event |
66
|
|
|
* @param null|string $payload |
67
|
|
|
*/ |
68
|
|
|
public static function run(string $target, string $event, ?string $payload = null): void |
69
|
|
|
{ |
70
|
|
|
$command = ['aenthill', 'run', $target, $event]; |
71
|
|
|
if (!empty($payload)) { |
72
|
|
|
$command[] = $payload; |
73
|
|
|
} |
74
|
|
|
$process = new Process($command); |
75
|
|
|
$process->enableOutput(); |
76
|
|
|
$process->setTty(true); |
77
|
|
|
$process->mustRun(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $target |
82
|
|
|
* @param string $event |
83
|
|
|
* @param mixed[] $payload |
84
|
|
|
*/ |
85
|
|
|
public static function runJson(string $target, string $event, array $payload): void |
86
|
|
|
{ |
87
|
|
|
self::run($target, $event, \GuzzleHttp\json_encode($payload)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Dispatches an event to all aents from the manifest which can handle it. |
92
|
|
|
* |
93
|
|
|
* @param string $event |
94
|
|
|
* @param null|string $payload |
95
|
|
|
* @return string[] the array of replies received from all aents that replied. |
96
|
|
|
*/ |
97
|
|
|
public static function dispatch(string $event, ?string $payload = null): array |
98
|
|
|
{ |
99
|
|
|
$replyAggregator = new ReplyAggregator(); |
100
|
|
|
$replyAggregator->clear(); |
101
|
|
|
$command = ['aenthill', 'dispatch', $event]; |
102
|
|
|
if (!empty($payload)) { |
103
|
|
|
$command[] = $payload; |
104
|
|
|
} |
105
|
|
|
$process = new Process($command); |
106
|
|
|
$process->enableOutput(); |
107
|
|
|
$process->setTty(true); |
108
|
|
|
$process->mustRun(); |
109
|
|
|
return $replyAggregator->getReplies(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param mixed[]|object $payload |
114
|
|
|
* @return mixed[] |
115
|
|
|
*/ |
116
|
|
|
public static function dispatchJson(string $event, $payload): array |
117
|
|
|
{ |
118
|
|
|
if (\is_object($payload) && !$payload instanceof \JsonSerializable) { |
119
|
|
|
throw new \RuntimeException('Payload object should implement JsonSerializable. Got an instance of ' . \get_class($payload)); |
120
|
|
|
} |
121
|
|
|
$replies = self::dispatch($event, \GuzzleHttp\json_encode($payload)); |
122
|
|
|
return \array_map(function (string $reply) { |
123
|
|
|
return \GuzzleHttp\json_decode($reply, true); |
124
|
|
|
}, $replies); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Replies to the aent which started this aent. |
129
|
|
|
* |
130
|
|
|
* @param string $event |
131
|
|
|
* @param null|string $payload |
132
|
|
|
*/ |
133
|
|
|
public static function reply(string $event, ?string $payload = null): void |
134
|
|
|
{ |
135
|
|
|
$command = ['aenthill', 'reply', $event]; |
136
|
|
|
if (!empty($payload)) { |
137
|
|
|
$command[] = $payload; |
138
|
|
|
} |
139
|
|
|
$process = new Process($command); |
140
|
|
|
$process->enableOutput(); |
141
|
|
|
$process->setTty(true); |
142
|
|
|
$process->mustRun(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $event |
147
|
|
|
* @param mixed[] $payload |
148
|
|
|
*/ |
149
|
|
|
public static function replyJson(string $event, array $payload): void |
150
|
|
|
{ |
151
|
|
|
self::reply($event, \GuzzleHttp\json_encode($payload)); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|