Primo Committ

This commit is contained in:
paoloar77
2024-05-07 12:17:25 +02:00
commit e73d0e5113
7204 changed files with 884387 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
<?php
namespace Facade\Ignition\Views\Compilers;
use ErrorException;
use Illuminate\View\Compilers\BladeCompiler;
class BladeSourceMapCompiler extends BladeCompiler
{
public function detectLineNumber(string $filename, int $exceptionLineNumber): int
{
try {
$map = $this->compileString(file_get_contents($filename));
} catch (ErrorException $e) {
return 1;
}
$map = explode("\n", $map);
$line = $map[$exceptionLineNumber - 1] ?? $exceptionLineNumber;
$pattern = '/\|---LINE:([0-9]+)---\|/m';
if (preg_match($pattern, (string)$line, $matches)) {
return (int)$matches[1];
}
return $exceptionLineNumber;
}
public function compileString($value)
{
try {
$value = $this->addEchoLineNumbers($value);
$value = $this->addStatementLineNumbers($value);
$value = parent::compileString($value);
return $this->trimEmptyLines($value);
} catch (\Exception $e) {
return $value;
}
}
protected function addEchoLineNumbers(string $value)
{
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[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)
{
$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 insertLineNumberAtPosition(int $position, string $value)
{
$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)
{
$value = preg_replace('/^\|---LINE:([0-9]+)---\|$/m', '', $value);
return ltrim($value, PHP_EOL);
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Facade\Ignition\Views\Concerns;
use Illuminate\Foundation\Application;
use Illuminate\Support\Collection;
use Illuminate\View\Engines\CompilerEngine;
trait CollectsViewExceptions
{
protected $lastCompiledData = [];
public function collectViewData($path, array $data): void
{
$this->lastCompiledData[] = [
'path' => $path,
'compiledPath' => $this->getCompiledPath($path),
'data' => $this->filterViewData($data),
];
}
public function filterViewData(array $data): array
{
// By default, Laravel views get two shared 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);
}
public function getCompiledViewData($compiledPath): array
{
$compiledView = $this->findCompiledView($compiledPath);
return $compiledView['data'] ?? [];
}
public function getCompiledViewName($compiledPath): string
{
$compiledView = $this->findCompiledView($compiledPath);
return $compiledView['path'] ?? $compiledPath;
}
protected function findCompiledView($compiledPath): ?array
{
return Collection::make($this->lastCompiledData)
->first(function ($compiledData) use ($compiledPath) {
$comparePath = $compiledData['compiledPath'];
return realpath(dirname($comparePath)).DIRECTORY_SEPARATOR.basename($comparePath) === $compiledPath;
});
}
protected function getCompiledPath($path): string
{
if ($this instanceof CompilerEngine) {
return $this->getCompiler()->getCompiledPath($path);
}
return $path;
}
}

View File

@@ -0,0 +1,117 @@
<?php
namespace Facade\Ignition\Views\Engines;
use Exception;
use Facade\Ignition\Exceptions\ViewException;
use Facade\Ignition\Exceptions\ViewExceptionWithSolution;
use Facade\Ignition\Views\Compilers\BladeSourceMapCompiler;
use Facade\Ignition\Views\Concerns\CollectsViewExceptions;
use Facade\IgnitionContracts\ProvidesSolution;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use ReflectionProperty;
use Throwable;
class CompilerEngine extends \Illuminate\View\Engines\CompilerEngine
{
use CollectsViewExceptions;
protected $currentPath = null;
/**
* Get the evaluated contents of the view.
*
* @param string $path
* @param array $data
*
* @return string
*/
public function get($path, array $data = [])
{
$this->currentPath = $path;
$this->collectViewData($path, $data);
return parent::get($path, $data);
}
/**
* Handle a view exception.
*
* @param \Throwable $baseException
* @param int $obLevel
*
* @return void
*
* @throws \Throwable
*/
protected function handleViewException(Throwable $baseException, $obLevel)
{
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
if ($baseException instanceof ViewException) {
throw $baseException;
}
$viewExceptionClass = ViewException::class;
if ($baseException instanceof ProvidesSolution) {
$viewExceptionClass = ViewExceptionWithSolution::class;
}
$exception = new $viewExceptionClass(
$this->getMessage($baseException),
0,
1,
$this->getCompiledViewName($baseException->getFile()),
$this->getBladeLineNumber($baseException->getFile(), $baseException->getLine()),
$baseException
);
if ($baseException instanceof ProvidesSolution) {
$exception->setSolution($baseException->getSolution());
}
$this->modifyViewsInTrace($exception);
$exception->setView($this->getCompiledViewName($baseException->getFile()));
$exception->setViewData($this->getCompiledViewData($baseException->getFile()));
throw $exception;
}
protected function getBladeLineNumber(string $compiledPath, int $exceptionLineNumber): int
{
$viewPath = $this->getCompiledViewName($compiledPath);
if (! $viewPath) {
return $exceptionLineNumber;
}
$sourceMapCompiler = new BladeSourceMapCompiler(app(Filesystem::class), 'not-needed');
return $sourceMapCompiler->detectLineNumber($viewPath, $exceptionLineNumber);
}
protected function modifyViewsInTrace(ViewException $exception)
{
$trace = Collection::make($exception->getPrevious()->getTrace())
->map(function ($trace) {
if ($compiledData = $this->findCompiledView(Arr::get($trace, 'file', ''))) {
$trace['file'] = $compiledData['path'];
$trace['line'] = $this->getBladeLineNumber($trace['file'], $trace['line']);
}
return $trace;
})->toArray();
$traceProperty = new ReflectionProperty('Exception', 'trace');
$traceProperty->setAccessible(true);
$traceProperty->setValue($exception, $trace);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Facade\Ignition\Views\Engines;
use Exception;
use Facade\Ignition\Exceptions\ViewException;
use Facade\Ignition\Views\Concerns\CollectsViewExceptions;
use Throwable;
class PhpEngine extends \Illuminate\View\Engines\PhpEngine
{
use CollectsViewExceptions;
/**
* Get the evaluated contents of the view.
*
* @param string $path
* @param array $data
* @return string
*/
public function get($path, array $data = [])
{
$this->collectViewData($path, $data);
return parent::get($path, $data);
}
/**
* Handle a view exception.
*
* @param \Throwable $baseException
* @param int $obLevel
*
* @return void
*
* @throws \Throwable
*/
protected function handleViewException(Throwable $baseException, $obLevel)
{
$exception = new ViewException($baseException->getMessage(), 0, 1, $baseException->getFile(), $baseException->getLine(), $baseException);
$exception->setView($this->getCompiledViewName($baseException->getFile()));
$exception->setViewData($this->getCompiledViewData($baseException->getFile()));
parent::handleViewException($exception, $obLevel);
}
}