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