|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flowpack\DecoupledContentStore\Eel; |
| 6 | + |
| 7 | +use Neos\Eel\ProtectedContextAwareInterface; |
| 8 | +use Neos\Flow\Annotations as Flow; |
| 9 | + |
| 10 | +/** |
| 11 | + * @Flow\Scope("singleton") |
| 12 | + */ |
| 13 | +class ModuleHelper implements ProtectedContextAwareInterface |
| 14 | +{ |
| 15 | + public function formatStdOutput(string $stdOut): string |
| 16 | + { |
| 17 | + $escapedString = $stdOut; |
| 18 | + $lines = array_reverse(array_filter(preg_split("/\r\n|\n|\r/", $escapedString))); |
| 19 | + $lineCount = count($lines); |
| 20 | + |
| 21 | + $formattedLines = array_map(static function (string $line, int $index) use ($lineCount) { |
| 22 | + // Extract additional JSON data |
| 23 | + preg_match("/\{.*}/", $line, $jsonMatches); |
| 24 | + $jsonData = array_filter(array_map(static function (string $match) { |
| 25 | + $matchData = json_decode($match, true); |
| 26 | + return $matchData['message'] ?? $matchData; |
| 27 | + }, $jsonMatches)); |
| 28 | + if (count($jsonData) === 1) { |
| 29 | + $jsonData = array_shift($jsonData); |
| 30 | + } |
| 31 | + |
| 32 | + // Remove additional JSON data from log line |
| 33 | + $line = preg_replace("/\{.*}/", '', $line); |
| 34 | + |
| 35 | + // Add highlighting for log levels |
| 36 | + $line = preg_replace("/(DEBUG|WARNING|ERROR|INFO): (.*)/", "<span class=\"log-level-$1\">$1:</span> <span class=\"log-content-$1\">$2</span>", htmlSpecialChars($line)); |
| 37 | + |
| 38 | + // Add line numbers |
| 39 | + $line = ($lineCount - $index) . ': ' . $line; |
| 40 | + |
| 41 | + // Insert formatted JSON data |
| 42 | + if ($jsonData) { |
| 43 | + $isDebug = strpos($line, 'DEBUG:') !== false; |
| 44 | + $jsonString = "\n<span class=\"json\">" . json_encode($jsonData, JSON_PRETTY_PRINT) . "</span>"; |
| 45 | + $line = $isDebug ? '<details class="json-details">' . '<summary>' . $line . '</summary>' . $jsonString . '</details>' : $line . $jsonString; |
| 46 | + } |
| 47 | + |
| 48 | + // Wrap in <pre> tags |
| 49 | + return '<pre>' . $line . '</pre>'; |
| 50 | + }, $lines, range(1, $lineCount)); |
| 51 | + |
| 52 | + return implode("\n", $formattedLines); |
| 53 | + } |
| 54 | + |
| 55 | + public function allowsCallOfMethod($methodName): bool |
| 56 | + { |
| 57 | + return true; |
| 58 | + } |
| 59 | +} |
0 commit comments