Upgrade framework
This commit is contained in:
145
vendor/spatie/laravel-ignition/src/Views/BladeSourceMapCompiler.php
vendored
Normal file
145
vendor/spatie/laravel-ignition/src/Views/BladeSourceMapCompiler.php
vendored
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\LaravelIgnition\Views;
|
||||
|
||||
use Illuminate\View\Compilers\BladeCompiler;
|
||||
use Throwable;
|
||||
|
||||
class BladeSourceMapCompiler
|
||||
{
|
||||
protected BladeCompiler $bladeCompiler;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->bladeCompiler = app('blade.compiler');
|
||||
}
|
||||
|
||||
public function detectLineNumber(string $filename, int $compiledLineNumber): int
|
||||
{
|
||||
$map = $this->compileSourcemap((string)file_get_contents($filename));
|
||||
|
||||
return $this->findClosestLineNumberMapping($map, $compiledLineNumber);
|
||||
}
|
||||
|
||||
protected function compileSourcemap(string $value): string
|
||||
{
|
||||
try {
|
||||
$value = $this->addEchoLineNumbers($value);
|
||||
|
||||
$value = $this->addStatementLineNumbers($value);
|
||||
|
||||
$value = $this->addBladeComponentLineNumbers($value);
|
||||
|
||||
$value = $this->bladeCompiler->compileString($value);
|
||||
|
||||
return $this->trimEmptyLines($value);
|
||||
} catch (Throwable $e) {
|
||||
report($e);
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
protected function addEchoLineNumbers(string $value): string
|
||||
{
|
||||
$echoPairs = [['{{', '}}'], ['{{{', '}}}'], ['{!!', '!!}']];
|
||||
|
||||
foreach ($echoPairs as $pair) {
|
||||
// Matches {{ $value }}, {!! $value !!} and {{{ $value }}} depending on $pair
|
||||
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $pair[0], $pair[1]);
|
||||
|
||||
if (preg_match_all($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
|
||||
foreach (array_reverse($matches[0]) as $match) {
|
||||
$position = mb_strlen(substr($value, 0, $match[1]));
|
||||
|
||||
$value = $this->insertLineNumberAtPosition($position, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function addStatementLineNumbers(string $value): string
|
||||
{
|
||||
// Matches @bladeStatements() like @if, @component(...), @etc;
|
||||
$shouldInsertLineNumbers = preg_match_all(
|
||||
'/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x',
|
||||
$value,
|
||||
$matches,
|
||||
PREG_OFFSET_CAPTURE
|
||||
);
|
||||
|
||||
if ($shouldInsertLineNumbers) {
|
||||
foreach (array_reverse($matches[0]) as $match) {
|
||||
$position = mb_strlen(substr($value, 0, $match[1]));
|
||||
|
||||
$value = $this->insertLineNumberAtPosition($position, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function addBladeComponentLineNumbers(string $value): string
|
||||
{
|
||||
// Matches the start of `<x-blade-component`
|
||||
$shouldInsertLineNumbers = preg_match_all(
|
||||
'/<\s*x[-:]([\w\-:.]*)/mx',
|
||||
$value,
|
||||
$matches,
|
||||
PREG_OFFSET_CAPTURE
|
||||
);
|
||||
|
||||
if ($shouldInsertLineNumbers) {
|
||||
foreach (array_reverse($matches[0]) as $match) {
|
||||
$position = mb_strlen(substr($value, 0, $match[1]));
|
||||
|
||||
$value = $this->insertLineNumberAtPosition($position, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function insertLineNumberAtPosition(int $position, string $value): string
|
||||
{
|
||||
$before = mb_substr($value, 0, $position);
|
||||
$lineNumber = count(explode("\n", $before));
|
||||
|
||||
return mb_substr($value, 0, $position)."|---LINE:{$lineNumber}---|".mb_substr($value, $position);
|
||||
}
|
||||
|
||||
protected function trimEmptyLines(string $value): string
|
||||
{
|
||||
$value = preg_replace('/^\|---LINE:([0-9]+)---\|$/m', '', $value);
|
||||
|
||||
return ltrim((string)$value, PHP_EOL);
|
||||
}
|
||||
|
||||
protected function findClosestLineNumberMapping(string $map, int $compiledLineNumber): int
|
||||
{
|
||||
$map = explode("\n", $map);
|
||||
|
||||
// Max 20 lines between compiled and source line number.
|
||||
// Blade components can span multiple lines and the compiled line number is often
|
||||
// a couple lines below the source-mapped `<x-component>` code.
|
||||
$maxDistance = 20;
|
||||
|
||||
$pattern = '/\|---LINE:(?P<line>[0-9]+)---\|/m';
|
||||
$lineNumberToCheck = $compiledLineNumber - 1;
|
||||
|
||||
while (true) {
|
||||
if ($lineNumberToCheck < $compiledLineNumber - $maxDistance) {
|
||||
// Something wrong. Return the $compiledLineNumber (unless it's out of range)
|
||||
return min($compiledLineNumber, count($map));
|
||||
}
|
||||
|
||||
if (preg_match($pattern, $map[$lineNumberToCheck] ?? '', $matches)) {
|
||||
return (int)$matches['line'];
|
||||
}
|
||||
|
||||
$lineNumberToCheck--;
|
||||
}
|
||||
}
|
||||
}
|
||||
177
vendor/spatie/laravel-ignition/src/Views/ViewExceptionMapper.php
vendored
Normal file
177
vendor/spatie/laravel-ignition/src/Views/ViewExceptionMapper.php
vendored
Normal file
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\LaravelIgnition\Views;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Contracts\View\Engine;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\View\Engines\PhpEngine;
|
||||
use Illuminate\View\ViewException;
|
||||
use ReflectionClass;
|
||||
use ReflectionProperty;
|
||||
use Spatie\Ignition\Contracts\ProvidesSolution;
|
||||
use Spatie\LaravelIgnition\Exceptions\ViewException as IgnitionViewException;
|
||||
use Spatie\LaravelIgnition\Exceptions\ViewExceptionWithSolution;
|
||||
use Throwable;
|
||||
|
||||
class ViewExceptionMapper
|
||||
{
|
||||
protected Engine $compilerEngine;
|
||||
protected BladeSourceMapCompiler $bladeSourceMapCompiler;
|
||||
protected array $knownPaths;
|
||||
|
||||
public function __construct(BladeSourceMapCompiler $bladeSourceMapCompiler)
|
||||
{
|
||||
$resolver = app('view.engine.resolver');
|
||||
|
||||
$this->compilerEngine = $resolver->resolve('blade');
|
||||
|
||||
$this->bladeSourceMapCompiler = $bladeSourceMapCompiler;
|
||||
}
|
||||
|
||||
public function map(ViewException $viewException): IgnitionViewException
|
||||
{
|
||||
$baseException = $this->getRealException($viewException);
|
||||
|
||||
if ($baseException instanceof IgnitionViewException) {
|
||||
return $baseException;
|
||||
}
|
||||
|
||||
preg_match('/\(View: (?P<path>.*?)\)/', $viewException->getMessage(), $matches);
|
||||
|
||||
$compiledViewPath = $matches['path'];
|
||||
|
||||
$exception = $this->createException($baseException);
|
||||
|
||||
if ($baseException instanceof ProvidesSolution) {
|
||||
/** @var ViewExceptionWithSolution $exception */
|
||||
$exception->setSolution($baseException->getSolution());
|
||||
}
|
||||
|
||||
$this->modifyViewsInTrace($exception);
|
||||
|
||||
$exception->setView($compiledViewPath);
|
||||
$exception->setViewData($this->getViewData($exception));
|
||||
|
||||
return $exception;
|
||||
}
|
||||
|
||||
protected function createException(Throwable $baseException): IgnitionViewException
|
||||
{
|
||||
$viewExceptionClass = $baseException instanceof ProvidesSolution
|
||||
? ViewExceptionWithSolution::class
|
||||
: IgnitionViewException::class;
|
||||
|
||||
$viewFile = $this->findCompiledView($baseException->getFile());
|
||||
$file = $viewFile ?? $baseException->getFile();
|
||||
$line = $viewFile ? $this->getBladeLineNumber($file, $baseException->getLine()) : $baseException->getLine();
|
||||
|
||||
return new $viewExceptionClass(
|
||||
$baseException->getMessage(),
|
||||
0,
|
||||
1,
|
||||
$file,
|
||||
$line,
|
||||
$baseException
|
||||
);
|
||||
}
|
||||
|
||||
protected function modifyViewsInTrace(IgnitionViewException $exception): void
|
||||
{
|
||||
$trace = Collection::make($exception->getPrevious()->getTrace())
|
||||
->map(function ($trace) {
|
||||
if ($originalPath = $this->findCompiledView(Arr::get($trace, 'file', ''))) {
|
||||
$trace['file'] = $originalPath;
|
||||
$trace['line'] = $this->getBladeLineNumber($trace['file'], $trace['line']);
|
||||
}
|
||||
|
||||
return $trace;
|
||||
})->toArray();
|
||||
|
||||
$traceProperty = new ReflectionProperty('Exception', 'trace');
|
||||
$traceProperty->setAccessible(true);
|
||||
$traceProperty->setValue($exception, $trace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Look at the previous exceptions to find the original exception.
|
||||
* This is usually the first Exception that is not a ViewException.
|
||||
*/
|
||||
protected function getRealException(Throwable $exception): Throwable
|
||||
{
|
||||
$rootException = $exception->getPrevious() ?? $exception;
|
||||
|
||||
while ($rootException instanceof ViewException && $rootException->getPrevious()) {
|
||||
$rootException = $rootException->getPrevious();
|
||||
}
|
||||
|
||||
return $rootException;
|
||||
}
|
||||
|
||||
protected function findCompiledView(string $compiledPath): ?string
|
||||
{
|
||||
$this->knownPaths ??= $this->getKnownPaths();
|
||||
|
||||
return $this->knownPaths[$compiledPath] ?? null;
|
||||
}
|
||||
|
||||
protected function getKnownPaths(): array
|
||||
{
|
||||
$compilerEngineReflection = new ReflectionClass($this->compilerEngine);
|
||||
|
||||
if (! $compilerEngineReflection->hasProperty('lastCompiled') && $compilerEngineReflection->hasProperty('engine')) {
|
||||
$compilerEngine = $compilerEngineReflection->getProperty('engine');
|
||||
$compilerEngine->setAccessible(true);
|
||||
$compilerEngine = $compilerEngine->getValue($this->compilerEngine);
|
||||
$lastCompiled = new ReflectionProperty($compilerEngine, 'lastCompiled');
|
||||
$lastCompiled->setAccessible(true);
|
||||
$lastCompiled = $lastCompiled->getValue($compilerEngine);
|
||||
} else {
|
||||
$lastCompiled = $compilerEngineReflection->getProperty('lastCompiled');
|
||||
$lastCompiled->setAccessible(true);
|
||||
$lastCompiled = $lastCompiled->getValue($this->compilerEngine);
|
||||
}
|
||||
|
||||
$knownPaths = [];
|
||||
foreach ($lastCompiled as $lastCompiledPath) {
|
||||
$compiledPath = $this->compilerEngine->getCompiler()->getCompiledPath($lastCompiledPath);
|
||||
|
||||
$knownPaths[$compiledPath ?? $lastCompiledPath] = $lastCompiledPath;
|
||||
}
|
||||
|
||||
return $knownPaths;
|
||||
}
|
||||
|
||||
protected function getBladeLineNumber(string $view, int $compiledLineNumber): int
|
||||
{
|
||||
return $this->bladeSourceMapCompiler->detectLineNumber($view, $compiledLineNumber);
|
||||
}
|
||||
|
||||
protected function getViewData(Throwable $exception): array
|
||||
{
|
||||
foreach ($exception->getTrace() as $frame) {
|
||||
if (Arr::get($frame, 'class') === PhpEngine::class) {
|
||||
$data = Arr::get($frame, 'args.1', []);
|
||||
|
||||
return $this->filterViewData($data);
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function filterViewData(array $data): array
|
||||
{
|
||||
// By default, Laravel views get two data keys:
|
||||
// __env and app. We try to filter them out.
|
||||
return array_filter($data, function ($value, $key) {
|
||||
if ($key === 'app') {
|
||||
return ! $value instanceof Application;
|
||||
}
|
||||
|
||||
return $key !== '__env';
|
||||
}, ARRAY_FILTER_USE_BOTH);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user