ljoaquim 2 недель назад
Родитель
Сommit
c74ff1e7b0
2 измененных файлов с 27 добавлено и 15 удалено
  1. 25 14
      controllers/RegisterCprController.php
  2. 2 1
      public/index.php

+ 25 - 14
controllers/RegisterCprController.php

@@ -136,32 +136,43 @@ class RegisterCprController
             throw new \RuntimeException('genial-cli returned empty output');
         }
 
+        $decoded = json_decode($clean, true);
+        if (json_last_error() === JSON_ERROR_NONE) {
+            return $decoded;
+        }
+
+        $normalized = $this->normalizeCliOutput($clean);
+        $decoded = json_decode($normalized, true);
+        if (json_last_error() !== JSON_ERROR_NONE) {
+            throw new \RuntimeException('Failed to decode genial-cli output: ' . json_last_error_msg());
+        }
+
+        return $decoded;
+    }
+
+    private function normalizeCliOutput(string $content): string
+    {
         // Strip ANSI escape codes, just in case
-        $clean = preg_replace('/\e\[[\d;]*m/', '', $clean);
+        $normalized = preg_replace('/\e\[[\d;]*m/', '', $content);
 
         // Convert single-quoted strings to JSON-compatible double-quoted ones
-        $clean = preg_replace_callback(
-            "/'([^'\\]*(?:\\.[^'\\]*)*)'/",
+        $normalized = preg_replace_callback(
+            "/'([^'\\\\]*(?:\\\\.[^'\\\\]*)*)'/",
             static function (array $matches): string {
                 $inner = str_replace(['\\', '"'], ['\\\\', '\\"'], $matches[1]);
                 return '"' . $inner . '"';
             },
-            $clean
+            $normalized
         );
 
         // 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
+        $normalized = preg_replace(
+            '/(?<=\{|\[|,|\n)\s*([A-Za-z_][A-Za-z0-9_]*)\s*:/',
+            '"$1":',
+            $normalized
         );
 
-        $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 $decoded;
+        return $normalized;
     }
 
     private function logCliResult(array $result, string $context): void

+ 2 - 1
public/index.php

@@ -5,7 +5,8 @@ require __DIR__ . '/../vendor/autoload.php';
 use FrameworkX\App;
 use Middlewares\JwtAuthMiddleware;
 
-$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
+$requestUri = $_SERVER['REQUEST_URI'] ?? null;
+$path = $requestUri !== null ? parse_url($requestUri, PHP_URL_PATH) : '/';
 $file = __DIR__ . $path;
 if (php_sapi_name() === 'cli-server' && is_file($file)) {
     return false;