|
@@ -96,13 +96,28 @@ class RegisterCprController
|
|
|
$result = BashExecutor::run($command, 60);
|
|
$result = BashExecutor::run($command, 60);
|
|
|
|
|
|
|
|
if (($result['exitCode'] ?? 1) !== 0) {
|
|
if (($result['exitCode'] ?? 1) !== 0) {
|
|
|
|
|
+ $this->logCliResult($result, 'genial-cli non-zero exit');
|
|
|
$message = $result['error'] ?: $result['output'] ?: 'Unknown error';
|
|
$message = $result['error'] ?: $result['output'] ?: 'Unknown error';
|
|
|
throw new \RuntimeException($message);
|
|
throw new \RuntimeException($message);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$output = $result['output'] ?? '';
|
|
$output = $result['output'] ?? '';
|
|
|
- $itemId = $this->extractCliValue('itemId', $output);
|
|
|
|
|
- $qrcodeUrl = $this->extractCliValue('qrcodeURL', $output);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $parsed = $this->decodeCliOutput($output);
|
|
|
|
|
+ } catch (\Throwable $e) {
|
|
|
|
|
+ $this->logCliResult($result, 'genial-cli parse failure');
|
|
|
|
|
+ throw $e;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $items = $parsed['data']['items'] ?? null;
|
|
|
|
|
+ if (!is_array($items) || empty($items) || !is_array($items[0])) {
|
|
|
|
|
+ throw new \RuntimeException('genial-cli output is missing items array');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $firstItem = $items[0];
|
|
|
|
|
+ $itemId = $firstItem['itemId'] ?? null;
|
|
|
|
|
+ $qrcodeUrl = $firstItem['data']['qrcodeURL'] ?? null;
|
|
|
|
|
|
|
|
if (!$itemId || !$qrcodeUrl) {
|
|
if (!$itemId || !$qrcodeUrl) {
|
|
|
throw new \RuntimeException('Unable to parse itemId or qrcodeURL from genial-cli output');
|
|
throw new \RuntimeException('Unable to parse itemId or qrcodeURL from genial-cli output');
|
|
@@ -114,17 +129,53 @@ class RegisterCprController
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private function extractCliValue(string $key, string $content): ?string
|
|
|
|
|
|
|
+ private function decodeCliOutput(string $content): array
|
|
|
{
|
|
{
|
|
|
- $pattern = sprintf('#%s\s*[:=]\s*(?:"([^"]+)"|\' . "'" . '([^\' . "'" . ']+)\' . "'" . '|([^\s,{}]+))#i', preg_quote($key, '#'));
|
|
|
|
|
- if (preg_match($pattern, $content, $matches)) {
|
|
|
|
|
- foreach (array_slice($matches, 1) as $match) {
|
|
|
|
|
- if ($match !== '' && $match !== null) {
|
|
|
|
|
- return trim($match);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $clean = trim($content);
|
|
|
|
|
+ if ($clean === '') {
|
|
|
|
|
+ throw new \RuntimeException('genial-cli returned empty output');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Strip ANSI escape codes, just in case
|
|
|
|
|
+ $clean = preg_replace('/\e\[[\d;]*m/', '', $clean);
|
|
|
|
|
+
|
|
|
|
|
+ // Convert single-quoted strings to JSON-compatible double-quoted ones
|
|
|
|
|
+ $clean = preg_replace_callback(
|
|
|
|
|
+ "/'([^'\\]*(?:\\.[^'\\]*)*)'/",
|
|
|
|
|
+ static function (array $matches): string {
|
|
|
|
|
+ $inner = str_replace(['\\', '"'], ['\\\\', '\\"'], $matches[1]);
|
|
|
|
|
+ return '"' . $inner . '"';
|
|
|
|
|
+ },
|
|
|
|
|
+ $clean
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // Quote object keys so json_decode can understand them
|
|
|
|
|
+ $clean = preg_replace(
|
|
|
|
|
+ '/([\\{\\[,,]\s*|\n\s*)([A-Za-z_][A-Za-z0-9_]*)\s*:/',
|
|
|
|
|
+ '$1"$2":',
|
|
|
|
|
+ $clean
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ $decoded = json_decode($clean, true);
|
|
|
|
|
+ if (json_last_error() !== JSON_ERROR_NONE) {
|
|
|
|
|
+ throw new \RuntimeException('Failed to decode genial-cli output: ' . json_last_error_msg());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return null;
|
|
|
|
|
|
|
+ return $decoded;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function logCliResult(array $result, string $context): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $exitCode = $result['exitCode'] ?? 'null';
|
|
|
|
|
+ $stdout = trim($result['output'] ?? '');
|
|
|
|
|
+ $stderr = trim($result['error'] ?? '');
|
|
|
|
|
+
|
|
|
|
|
+ error_log(sprintf(
|
|
|
|
|
+ '[RegisterCprController] %s | exitCode: %s | stdout: %s | stderr: %s',
|
|
|
|
|
+ $context,
|
|
|
|
|
+ (string)$exitCode,
|
|
|
|
|
+ $stdout === '' ? '<empty>' : $stdout,
|
|
|
|
|
+ $stderr === '' ? '<empty>' : $stderr
|
|
|
|
|
+ ));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|