From 217fe816010640b7e9b34ec064c0c2e595f31026 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Fri, 26 Apr 2024 09:23:51 +0200 Subject: [PATCH 1/2] PHP 8.1 awesomeness --- src/Views/ViewExceptionMapper.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Views/ViewExceptionMapper.php b/src/Views/ViewExceptionMapper.php index 420e8678..a1bb748e 100644 --- a/src/Views/ViewExceptionMapper.php +++ b/src/Views/ViewExceptionMapper.php @@ -2,7 +2,6 @@ namespace Spatie\LaravelIgnition\Views; -use Exception; use Illuminate\Contracts\View\Engine; use Illuminate\Foundation\Application; use Illuminate\Support\Arr; @@ -19,7 +18,9 @@ class ViewExceptionMapper { protected Engine $compilerEngine; + protected BladeSourceMapCompiler $bladeSourceMapCompiler; + protected array $knownPaths; public function __construct(BladeSourceMapCompiler $bladeSourceMapCompiler) @@ -80,15 +81,27 @@ protected function createException(Throwable $baseException): IgnitionViewExcept protected function modifyViewsInTrace(IgnitionViewException $exception): void { + $viewIndex = null; + + $trace = Collection::make($exception->getPrevious()->getTrace()) - ->map(function ($trace) { + ->map(function ($trace, $index) use (&$viewIndex) { if ($originalPath = $this->findCompiledView(Arr::get($trace, 'file', ''))) { $trace['file'] = $originalPath; $trace['line'] = $this->getBladeLineNumber($trace['file'], $trace['line']); + + if ($viewIndex === null) { + $viewIndex = $index; + } } return $trace; - })->toArray(); + }) + ->when( + $viewIndex !== null, + fn (Collection $trace) => $trace->slice($viewIndex + 1) // Remove all traces before the view + ) + ->toArray(); $traceProperty = new ReflectionProperty('Exception', 'trace'); $traceProperty->setAccessible(true); From 166ad8cdb36a5ce80aaa8a63bc9506cc3d1b5c3c Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Tue, 30 Apr 2024 10:43:45 +0200 Subject: [PATCH 2/2] Handle case where an external call was made from a view --- src/Views/ViewExceptionMapper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Views/ViewExceptionMapper.php b/src/Views/ViewExceptionMapper.php index a1bb748e..38727766 100644 --- a/src/Views/ViewExceptionMapper.php +++ b/src/Views/ViewExceptionMapper.php @@ -83,10 +83,10 @@ protected function modifyViewsInTrace(IgnitionViewException $exception): void { $viewIndex = null; - $trace = Collection::make($exception->getPrevious()->getTrace()) ->map(function ($trace, $index) use (&$viewIndex) { if ($originalPath = $this->findCompiledView(Arr::get($trace, 'file', ''))) { + $trace['file'] = $originalPath; $trace['line'] = $this->getBladeLineNumber($trace['file'], $trace['line']); @@ -98,7 +98,7 @@ protected function modifyViewsInTrace(IgnitionViewException $exception): void return $trace; }) ->when( - $viewIndex !== null, + $viewIndex !== null && str_ends_with($exception->getFile(), '.blade.php'), fn (Collection $trace) => $trace->slice($viewIndex + 1) // Remove all traces before the view ) ->toArray();