Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix view traces #189

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Views/ViewExceptionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Spatie\LaravelIgnition\Views;

use Exception;
use Illuminate\Contracts\View\Engine;
use Illuminate\Foundation\Application;
use Illuminate\Support\Arr;
Expand All @@ -19,7 +18,9 @@
class ViewExceptionMapper
{
protected Engine $compilerEngine;

protected BladeSourceMapCompiler $bladeSourceMapCompiler;

protected array $knownPaths;

public function __construct(BladeSourceMapCompiler $bladeSourceMapCompiler)
Expand Down Expand Up @@ -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 && str_ends_with($exception->getFile(), '.blade.php'),
fn (Collection $trace) => $trace->slice($viewIndex + 1) // Remove all traces before the view
)
->toArray();

$traceProperty = new ReflectionProperty('Exception', 'trace');
$traceProperty->setAccessible(true);
Expand Down
Loading