|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CraftCms\Cms\Twig; |
| 6 | + |
| 7 | +use Craft; |
| 8 | +use Illuminate\Support\Collection; |
| 9 | +use ReflectionProperty; |
| 10 | +use Throwable; |
| 11 | +use Twig\Error\RuntimeError; |
| 12 | +use Twig\Template; |
| 13 | + |
| 14 | +final readonly class TwigMapper |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Maps an exception and replaces all references to compiled Twig |
| 18 | + * templates in the stack trace with references to the original source. |
| 19 | + */ |
| 20 | + public function map(Throwable $exception): Throwable |
| 21 | + { |
| 22 | + if ($exception instanceof RuntimeError) { |
| 23 | + /** |
| 24 | + * When we get a Twig runtime error, we need the previous exception to be shown. |
| 25 | + */ |
| 26 | + $exception = $exception->getPrevious() ?? $exception; |
| 27 | + } |
| 28 | + |
| 29 | + $viewIndex = null; |
| 30 | + |
| 31 | + $trace = collect($exception->getTrace()) |
| 32 | + ->map(function (array $frame, int $index) use (&$viewIndex) { |
| 33 | + $templateInfo = $this->resolveTemplatePathAndLine($frame['file'] ?? '', $frame['line']); |
| 34 | + |
| 35 | + if ($templateInfo !== false) { |
| 36 | + [$frame['file'], $frame['line']] = $templateInfo; |
| 37 | + |
| 38 | + $viewIndex ??= $index; |
| 39 | + } |
| 40 | + |
| 41 | + return $frame; |
| 42 | + }) |
| 43 | + ->when( |
| 44 | + $viewIndex !== null && str_ends_with($exception->getFile(), '.twig'), |
| 45 | + fn (Collection $trace) => $trace->slice($viewIndex + 1) // Remove all traces before the view |
| 46 | + ) |
| 47 | + ->all(); |
| 48 | + |
| 49 | + $traceProperty = new ReflectionProperty('Exception', 'trace'); |
| 50 | + $traceProperty->setValue($exception, $trace); |
| 51 | + |
| 52 | + return $exception; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Attempts to resolve a compiled template file path and line number to its source template path and line number. |
| 57 | + * |
| 58 | + * @param string $path The compiled template path |
| 59 | + * @param int|null $line The line number from the compiled template |
| 60 | + * @return array|false The resolved template path and line number, or `false` if the path couldn’t be determined. |
| 61 | + * If a template path could be determined but not the template line number, the line number will be null. |
| 62 | + */ |
| 63 | + public function resolveTemplatePathAndLine(string $path, ?int $line): array|false |
| 64 | + { |
| 65 | + if (! str_contains($path, 'compiled_templates')) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + $contents = file_get_contents($path); |
| 70 | + |
| 71 | + if (! preg_match('/^class (\w+)/m', $contents, $match)) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + $class = $match[1]; |
| 76 | + if (! class_exists($class, false) || ! is_subclass_of($class, Template::class)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + $template = new $class(Craft::$app->getView()->getTwig()); |
| 81 | + $src = $template->getSourceContext(); |
| 82 | + $templatePath = $src->getPath() ?: null; |
| 83 | + $templateLine = null; |
| 84 | + |
| 85 | + if ($line !== null) { |
| 86 | + foreach ($template->getDebugInfo() as $codeLine => $thisTemplateLine) { |
| 87 | + if ($codeLine <= $line) { |
| 88 | + $templateLine = $thisTemplateLine; |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return [$templatePath, $templateLine]; |
| 95 | + } |
| 96 | +} |
0 commit comments