|
19 | 19 |
|
20 | 20 | namespace FastForward\DevTools\Console\Logger\Processor; |
21 | 21 |
|
| 22 | +use JsonException; |
22 | 23 | use Symfony\Component\Console\Output\BufferedOutput; |
23 | 24 | use Symfony\Component\Console\Output\ConsoleOutputInterface; |
24 | 25 | use Symfony\Component\Console\Output\OutputInterface; |
25 | 26 |
|
| 27 | +use function Safe\json_decode; |
| 28 | + |
26 | 29 | /** |
27 | 30 | * Converts buffered command output objects into serializable context entries. |
| 31 | + * |
| 32 | + * JSON payloads are decoded eagerly so parent command envelopes can expose |
| 33 | + * nested structured output without re-encoding it as an escaped string. |
28 | 34 | */ |
29 | 35 | final class CommandOutputProcessor implements ContextProcessorInterface |
30 | 36 | { |
@@ -63,14 +69,154 @@ public function process(array $context): array |
63 | 69 | /** |
64 | 70 | * @param OutputInterface $output |
65 | 71 | * |
66 | | - * @return ?string |
| 72 | + * @return mixed |
67 | 73 | */ |
68 | | - private function extractBufferedOutput(OutputInterface $output): ?string |
| 74 | + private function extractBufferedOutput(OutputInterface $output): mixed |
69 | 75 | { |
70 | 76 | if (! $output instanceof BufferedOutput) { |
71 | 77 | return null; |
72 | 78 | } |
73 | 79 |
|
74 | | - return $output->fetch(); |
| 80 | + $content = $output->fetch(); |
| 81 | + |
| 82 | + return $this->decodeStructuredOutput($content); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Decodes a buffered output string when it contains JSON content. |
| 87 | + * |
| 88 | + * @param string $content the buffered output contents |
| 89 | + * |
| 90 | + * @return mixed the decoded JSON payload or the original string |
| 91 | + */ |
| 92 | + private function decodeStructuredOutput(string $content): mixed |
| 93 | + { |
| 94 | + $trimmedContent = trim($content); |
| 95 | + |
| 96 | + if ('' === $trimmedContent) { |
| 97 | + return $content; |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + return json_decode($trimmedContent, true); |
| 102 | + } catch (JsonException) { |
| 103 | + } |
| 104 | + |
| 105 | + $decodedDocuments = $this->decodeJsonDocumentStream($trimmedContent); |
| 106 | + |
| 107 | + if (null !== $decodedDocuments) { |
| 108 | + return $decodedDocuments; |
| 109 | + } |
| 110 | + |
| 111 | + return $content; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Decodes a stream that contains multiple JSON documents separated by whitespace. |
| 116 | + * |
| 117 | + * @param string $content the buffered output contents |
| 118 | + * |
| 119 | + * @return ?list<mixed> the decoded JSON documents when the stream is valid |
| 120 | + */ |
| 121 | + private function decodeJsonDocumentStream(string $content): ?array |
| 122 | + { |
| 123 | + $decodedDocuments = []; |
| 124 | + $offset = 0; |
| 125 | + $length = \strlen($content); |
| 126 | + |
| 127 | + while ($offset < $length) { |
| 128 | + while ($offset < $length && ctype_space($content[$offset])) { |
| 129 | + ++$offset; |
| 130 | + } |
| 131 | + |
| 132 | + if ($offset >= $length) { |
| 133 | + break; |
| 134 | + } |
| 135 | + |
| 136 | + $document = $this->consumeJsonDocument($content, $offset); |
| 137 | + |
| 138 | + if (null === $document) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + try { |
| 143 | + $decodedDocuments[] = json_decode($document, true); |
| 144 | + } catch (JsonException) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return \count($decodedDocuments) > 1 ? $decodedDocuments : null; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Consumes a single top-level JSON document from a multi-document stream. |
| 154 | + * |
| 155 | + * @param string $content the buffered output contents |
| 156 | + * @param int $offset the current stream offset, advanced past the document on success |
| 157 | + * |
| 158 | + * @return ?string the extracted JSON document |
| 159 | + */ |
| 160 | + private function consumeJsonDocument(string $content, int &$offset): ?string |
| 161 | + { |
| 162 | + $length = \strlen($content); |
| 163 | + $start = $offset; |
| 164 | + $openingToken = $content[$offset]; |
| 165 | + |
| 166 | + if ('{' !== $openingToken && '[' !== $openingToken) { |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + $depth = 0; |
| 171 | + $inString = false; |
| 172 | + $escaping = false; |
| 173 | + |
| 174 | + for (; $offset < $length; ++$offset) { |
| 175 | + $character = $content[$offset]; |
| 176 | + |
| 177 | + if ($inString) { |
| 178 | + if ($escaping) { |
| 179 | + $escaping = false; |
| 180 | + |
| 181 | + continue; |
| 182 | + } |
| 183 | + |
| 184 | + if ('\\' === $character) { |
| 185 | + $escaping = true; |
| 186 | + |
| 187 | + continue; |
| 188 | + } |
| 189 | + |
| 190 | + if ('"' === $character) { |
| 191 | + $inString = false; |
| 192 | + } |
| 193 | + |
| 194 | + continue; |
| 195 | + } |
| 196 | + |
| 197 | + if ('"' === $character) { |
| 198 | + $inString = true; |
| 199 | + |
| 200 | + continue; |
| 201 | + } |
| 202 | + |
| 203 | + if ('{' === $character || '[' === $character) { |
| 204 | + ++$depth; |
| 205 | + |
| 206 | + continue; |
| 207 | + } |
| 208 | + |
| 209 | + if ('}' === $character || ']' === $character) { |
| 210 | + --$depth; |
| 211 | + |
| 212 | + if (0 === $depth) { |
| 213 | + ++$offset; |
| 214 | + |
| 215 | + return substr($content, $start, $offset - $start); |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return null; |
75 | 221 | } |
76 | 222 | } |
0 commit comments