-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal.php
More file actions
303 lines (261 loc) · 8.07 KB
/
internal.php
File metadata and controls
303 lines (261 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
declare(strict_types=1);
namespace ShopmonCli\Internal;
use Composer\InstalledVersions;
use PharData;
use RuntimeException;
use function array_map;
use function chmod;
use function curl_error;
use function curl_exec;
use function curl_getinfo;
use function curl_init;
use function curl_setopt;
use function escapeshellarg;
use function escapeshellcmd;
use function extension_loaded;
use function fclose;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function fopen;
use function fprintf;
use function fwrite;
use function implode;
use function ini_get;
use function is_dir;
use function is_resource;
use function mkdir;
use function number_format;
use function php_uname;
use function proc_close;
use function proc_get_status;
use function proc_open;
use function strtolower;
use function unlink;
use const CURLINFO_HTTP_CODE;
use const CURLOPT_FILE;
use const CURLOPT_FOLLOWLOCATION;
use const CURLOPT_NOPROGRESS;
use const CURLOPT_PROGRESSFUNCTION;
use const STDERR;
/**
* Get the installed shopmon-cli version from Composer metadata.
*
* @throws RuntimeException If the version cannot be determined.
*
* @return string The package version (e.g., "0.0.4").
*/
function get_version(): string
{
$version = InstalledVersions::getPrettyVersion('frosh/shopmon-cli');
if ($version === null) {
throw new RuntimeException('Could not determine shopmon-cli package version.');
}
return $version;
}
/**
* Detect the CPU architecture from the system.
*
* @throws RuntimeException If the architecture is not supported.
*
* @return string Normalized architecture (e.g., "amd64", "arm64").
*/
function detect_architecture(): string
{
$raw = strtolower(php_uname('m'));
return match ($raw) {
'x86_64', 'amd64' => 'amd64',
'arm64', 'aarch64' => 'arm64',
default => throw new RuntimeException(
"Unsupported architecture: {$raw}. Pre-built binaries are available for amd64 and arm64.",
),
};
}
/**
* Detect the operating system and return platform metadata.
*
* @param string $architecture Normalized architecture name.
*
* @throws RuntimeException If the OS is not supported.
*
* @return array{os: string, arch: string}
*/
function detect_platform(string $architecture): array
{
$os = strtolower(php_uname('s'));
return match ($os) {
'linux' => [
'os' => 'linux',
'arch' => $architecture,
],
'darwin' => [
'os' => 'darwin',
'arch' => $architecture,
],
default => throw new RuntimeException(
"Unsupported operating system: {$os}. Pre-built binaries are available for Linux and macOS.",
),
};
}
/**
* Build the archive filename for a given version and platform.
*
* @return string Archive name (e.g., "shopmon-cli_0.0.4_linux_amd64").
*/
function build_archive_name(string $version, string $os, string $arch): string
{
return "shopmon-cli_{$version}_{$os}_{$arch}";
}
/**
* Build the GitHub release download URL.
*
* @return string Full download URL.
*/
function build_download_url(string $version, string $archiveName): string
{
return "https://github.com/FriendsOfShopware/shopmon-cli/releases/download/{$version}/{$archiveName}.tar.gz";
}
/**
* Download a file from a URL using the best available method.
*
* @throws RuntimeException If the download fails or no download method is available.
*/
function download(string $url, string $destination): void
{
if (extension_loaded('curl')) {
namespace\download_with_curl($url, $destination);
return;
}
if (ini_get('allow_url_fopen')) {
namespace\download_with_fopen($url, $destination);
return;
}
throw new RuntimeException(
'Unable to download shopmon-cli binary. Either install the PHP curl extension or set allow_url_fopen=1 in php.ini.',
);
}
/**
* Download a file using the curl extension with a progress bar.
*
* @throws RuntimeException If the download fails or the server returns an error status.
*/
function download_with_curl(string $url, string $destination): void
{
$ch = curl_init($url);
$fh = fopen($destination, 'w');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function (mixed $_resource, int $dlSize, int $dlNow): int {
if ($dlSize > 0) {
$pct = (int) (($dlNow / $dlSize) * 100);
$dlMb = number_format($dlNow / 1_048_576, 1);
$totalMb = number_format($dlSize / 1_048_576, 1);
fprintf(STDERR, "\r %s / %s MB (%d%%)", $dlMb, $totalMb, $pct);
}
return 0;
});
$success = curl_exec($ch);
/** @var int<100, 599> */
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
fclose($fh);
if (!$success || $statusCode >= 400) {
unlink($destination);
throw new RuntimeException("Failed to download shopmon-cli binary (HTTP {$statusCode}): {$error}\nURL: {$url}");
}
fprintf(STDERR, "\n");
}
/**
* Download a file using `file_get_contents` (requires `allow_url_fopen`).
*
* @throws RuntimeException If the download fails.
*/
function download_with_fopen(string $url, string $destination): void
{
$contents = file_get_contents($url);
if ($contents === false) {
throw new RuntimeException("Failed to download shopmon-cli binary.\nURL: {$url}");
}
file_put_contents($destination, $contents);
}
/**
* Extract a tar.gz archive to a destination directory.
*
* @throws RuntimeException If the archive cannot be extracted.
*/
function extract_archive(string $archiveFile, string $destination): void
{
$phar = new PharData($archiveFile);
$phar->extractTo($destination);
unlink($archiveFile);
}
/**
* Ensure the shopmon-cli binary is available, downloading it if necessary.
*
* @param string $version Package version.
* @param string $archiveName Archive name without extension.
* @param string $binDir Base directory for storing binaries.
*
* @throws RuntimeException If the download, extraction, or binary verification fails.
*
* @return string Path to the shopmon-cli executable.
*/
function ensure_binary(string $version, string $archiveName, string $binDir): string
{
$releaseDir = "{$binDir}/{$version}";
$executablePath = "{$releaseDir}/shopmon-cli";
if (file_exists($executablePath)) {
return $executablePath;
}
$archiveFile = "{$releaseDir}/{$archiveName}.tar.gz";
$url = namespace\build_download_url($version, $archiveName);
if (!is_dir($releaseDir)) {
mkdir($releaseDir, 0o755, true);
}
fprintf(STDERR, "Downloading shopmon-cli %s for %s...\n", $version, $archiveName);
namespace\download($url, $archiveFile);
fprintf(STDERR, "Downloaded.\n");
namespace\extract_archive($archiveFile, $releaseDir);
if (!file_exists($executablePath)) {
throw new RuntimeException("Expected binary not found after extraction at {$executablePath}");
}
chmod($executablePath, 0o755);
return $executablePath;
}
/**
* Execute the shopmon-cli binary, forwarding stdin/stdout/stderr.
*
* @param string $executablePath Path to the shopmon-cli binary.
* @param list<string> $args Command-line arguments to pass.
*
* @return never
*/
function execute(string $executablePath, array $args): never
{
$command = escapeshellcmd($executablePath);
if ($args !== []) {
$command .= ' ' . implode(' ', array_map(escapeshellarg(...), $args));
}
$pipes = [];
$process = @proc_open(
$command,
[
0 => ['file', 'php://stdin', 'r'],
1 => ['file', 'php://stdout', 'w'],
2 => ['file', 'php://stderr', 'w'],
],
$pipes,
);
if (!is_resource($process)) {
fwrite(STDERR, "Error: Unable to start shopmon-cli process.\n");
exit(1);
}
do {
$status = proc_get_status($process);
} while ($status['running']);
$exitCode = $status['exitcode'];
proc_close($process);
exit($exitCode);
}