|
| 1 | +<?php |
| 2 | + |
| 3 | +use CodeIgniter\CodeIgniter; |
| 4 | +use CodeIgniter\HTTP\IncomingRequest; |
| 5 | +use CodeIgniter\HTTP\URI; |
| 6 | + |
| 7 | +$reportMessage = str_replace(["\r\n", "\r"], "\n", $message); |
| 8 | +$reportTitle = trim($reportMessage); |
| 9 | +$reportTitle = $reportTitle === '' ? $title : explode("\n", $reportTitle, 2)[0]; |
| 10 | +$messageLines = str_contains($reportMessage, "\n"); |
| 11 | + |
| 12 | +$reportResponse = service('response', null, false); |
| 13 | +$reportResponse->setStatusCode($code); |
| 14 | + |
| 15 | +$report = [ |
| 16 | + '# ' . $reportTitle, |
| 17 | + '', |
| 18 | + '## Exception', |
| 19 | + '', |
| 20 | + '- Type: ' . $type, |
| 21 | + '- Status Code: ' . $code, |
| 22 | + '- Status: ' . $reportResponse->getReasonPhrase(), |
| 23 | + $messageLines ? '- Message:' : '- Message: ' . $reportMessage, |
| 24 | +]; |
| 25 | + |
| 26 | +if ($messageLines) { |
| 27 | + $report[] = ''; |
| 28 | + $report[] = '```text'; |
| 29 | + $report[] = $reportMessage; |
| 30 | + $report[] = '```'; |
| 31 | +} |
| 32 | + |
| 33 | +$report[] = ''; |
| 34 | +$report[] = '## Environment'; |
| 35 | +$report[] = ''; |
| 36 | +$report[] = '- PHP: ' . PHP_VERSION; |
| 37 | +$report[] = '- CodeIgniter: ' . CodeIgniter::CI_VERSION; |
| 38 | +$report[] = '- Environment: ' . ENVIRONMENT; |
| 39 | +$report[] = '- SAPI: ' . PHP_SAPI; |
| 40 | +$report[] = '- Time: ' . date('Y-m-d H:i:s e'); |
| 41 | +$report[] = '- Memory Usage: ' . number_format(memory_get_usage(true) / 1024 / 1024, 2) . ' MB'; |
| 42 | + |
| 43 | +$reportRequest = service('request'); |
| 44 | + |
| 45 | +if ($reportRequest instanceof IncomingRequest) { |
| 46 | + $reportPath = '/' . ltrim($reportRequest->getPath(), '/'); |
| 47 | + $reportUri = $reportRequest->getUri(); |
| 48 | + $reportUrl = $reportPath; |
| 49 | + |
| 50 | + if ($reportUri->getHost() !== '') { |
| 51 | + $reportUrl = URI::createURIString( |
| 52 | + $reportUri->getScheme(), |
| 53 | + $reportUri->getHost() . ($reportUri->getPort() === null ? '' : ':' . $reportUri->getPort()), |
| 54 | + $reportPath, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + $report[] = ''; |
| 59 | + $report[] = '## Request'; |
| 60 | + $report[] = ''; |
| 61 | + $report[] = '- Method: ' . $reportRequest->getMethod(); |
| 62 | + $report[] = '- Path: ' . $reportPath; |
| 63 | + $report[] = '- URL: ' . $reportUrl; |
| 64 | + $report[] = '- User Agent: ' . $reportRequest->getUserAgent()->getAgentString(); |
| 65 | +} |
| 66 | + |
| 67 | +$report[] = ''; |
| 68 | +$report[] = '## Source'; |
| 69 | +$report[] = ''; |
| 70 | +$report[] = '`' . clean_path($file) . ':' . $line . '`'; |
| 71 | + |
| 72 | +if (is_file($file) && is_readable($file)) { |
| 73 | + $sourceLines = file($file, FILE_IGNORE_NEW_LINES); |
| 74 | + |
| 75 | + if ($sourceLines !== false) { |
| 76 | + $startLine = max($line - 5, 1); |
| 77 | + $endLine = min($line + 5, count($sourceLines)); |
| 78 | + |
| 79 | + $report[] = ''; |
| 80 | + $report[] = '```php'; |
| 81 | + |
| 82 | + for ($sourceLine = $startLine; $sourceLine <= $endLine; $sourceLine++) { |
| 83 | + $report[] = sprintf( |
| 84 | + '%s%4d %s', |
| 85 | + $sourceLine === $line ? '>' : ' ', |
| 86 | + $sourceLine, |
| 87 | + $sourceLines[$sourceLine - 1], |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + $report[] = '```'; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +$previousException = $exception->getPrevious(); |
| 96 | + |
| 97 | +if ($previousException instanceof Throwable) { |
| 98 | + $report[] = ''; |
| 99 | + $report[] = '## Previous Exceptions'; |
| 100 | + |
| 101 | + while ($previousException instanceof Throwable) { |
| 102 | + $report[] = '* ' . $previousException::class . ' - ' . $previousException->getMessage(); |
| 103 | + $report[] = ' ' . clean_path($previousException->getFile()) . ':' . $previousException->getLine(); |
| 104 | + |
| 105 | + $previousException = $previousException->getPrevious(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +if ($trace !== []) { |
| 110 | + $report[] = ''; |
| 111 | + $report[] = '## Stack Trace'; |
| 112 | + $report[] = ''; |
| 113 | + $report[] = '```text'; |
| 114 | + |
| 115 | + foreach (array_slice($trace, 0, 50) as $reportIndex => $reportRow) { |
| 116 | + $reportLocation = isset($reportRow['file'], $reportRow['line']) |
| 117 | + ? clean_path($reportRow['file']) . ':' . $reportRow['line'] |
| 118 | + : '{PHP internal code}'; |
| 119 | + $reportCall = ($reportRow['class'] ?? '') . ($reportRow['type'] ?? '') . ($reportRow['function'] ?? ''); |
| 120 | + |
| 121 | + $report[] = $reportIndex . ' ' . $reportLocation . ($reportCall === '' ? '' : ' ' . $reportCall . '()'); |
| 122 | + } |
| 123 | + |
| 124 | + $report[] = '```'; |
| 125 | +} |
| 126 | + |
| 127 | +echo esc(implode("\n", $report)) . "\n"; |
0 commit comments