vendor and env first commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backtrace\CodeSnippets;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class CodeSnippet
|
||||
{
|
||||
/** @var int */
|
||||
protected $surroundingLine = 1;
|
||||
|
||||
/** @var int */
|
||||
protected $snippetLineCount = 9;
|
||||
|
||||
public function surroundingLine(int $surroundingLine): self
|
||||
{
|
||||
$this->surroundingLine = $surroundingLine;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function snippetLineCount(int $snippetLineCount): self
|
||||
{
|
||||
$this->snippetLineCount = $snippetLineCount;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get(SnippetProvider $provider): array
|
||||
{
|
||||
try {
|
||||
[$startLineNumber, $endLineNumber] = $this->getBounds($provider->numberOfLines());
|
||||
|
||||
$code = [];
|
||||
|
||||
$line = $provider->getLine($startLineNumber);
|
||||
|
||||
$currentLineNumber = $startLineNumber;
|
||||
|
||||
while ($currentLineNumber <= $endLineNumber) {
|
||||
$code[$currentLineNumber] = rtrim(substr($line, 0, 250));
|
||||
|
||||
$line = $provider->getNextLine();
|
||||
$currentLineNumber++;
|
||||
}
|
||||
|
||||
return $code;
|
||||
} catch (RuntimeException $exception) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public function getAsString(SnippetProvider $provider): string
|
||||
{
|
||||
$snippet = $this->get($provider);
|
||||
|
||||
$snippetStrings = array_map(function (string $line, string $number) {
|
||||
return "{$number} {$line}";
|
||||
}, $snippet, array_keys($snippet));
|
||||
|
||||
return implode(PHP_EOL, $snippetStrings);
|
||||
}
|
||||
|
||||
protected function getBounds(int $totalNumberOfLineInFile): array
|
||||
{
|
||||
$startLine = max($this->surroundingLine - floor($this->snippetLineCount / 2), 1);
|
||||
|
||||
$endLine = $startLine + ($this->snippetLineCount - 1);
|
||||
|
||||
if ($endLine > $totalNumberOfLineInFile) {
|
||||
$endLine = $totalNumberOfLineInFile;
|
||||
$startLine = max($endLine - ($this->snippetLineCount - 1), 1);
|
||||
}
|
||||
|
||||
return [$startLine, $endLine];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backtrace\CodeSnippets;
|
||||
|
||||
use SplFileObject;
|
||||
|
||||
class FileSnippetProvider implements SnippetProvider
|
||||
{
|
||||
/** @var \SplFileObject */
|
||||
protected $file;
|
||||
|
||||
public function __construct(string $path)
|
||||
{
|
||||
$this->file = new SplFileObject($path);
|
||||
}
|
||||
|
||||
public function numberOfLines(): int
|
||||
{
|
||||
$this->file->seek(PHP_INT_MAX);
|
||||
|
||||
return $this->file->key() + 1;
|
||||
}
|
||||
|
||||
public function getLine(int $lineNumber = null): string
|
||||
{
|
||||
if (is_null($lineNumber)) {
|
||||
return $this->getNextLine();
|
||||
}
|
||||
|
||||
$this->file->seek($lineNumber - 1);
|
||||
|
||||
return $this->file->current();
|
||||
}
|
||||
|
||||
public function getNextLine(): string
|
||||
{
|
||||
$this->file->next();
|
||||
|
||||
return $this->file->current();
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backtrace\CodeSnippets;
|
||||
|
||||
class LaravelSerializableClosureSnippetProvider implements SnippetProvider
|
||||
{
|
||||
/** @var array<string> */
|
||||
protected $lines;
|
||||
|
||||
/** @var int */
|
||||
protected $counter = 0;
|
||||
|
||||
public function __construct(string $snippet)
|
||||
{
|
||||
$this->lines = preg_split("/\r\n|\n|\r/", $snippet);
|
||||
|
||||
$this->cleanupLines();
|
||||
}
|
||||
|
||||
public function numberOfLines(): int
|
||||
{
|
||||
return count($this->lines);
|
||||
}
|
||||
|
||||
public function getLine(int $lineNumber = null): string
|
||||
{
|
||||
if (is_null($lineNumber)) {
|
||||
return $this->getNextLine();
|
||||
}
|
||||
|
||||
$this->counter = $lineNumber - 1;
|
||||
|
||||
return $this->lines[$lineNumber - 1];
|
||||
}
|
||||
|
||||
public function getNextLine(): string
|
||||
{
|
||||
$this->counter++;
|
||||
|
||||
if ($this->counter >= count($this->lines)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->lines[$this->counter];
|
||||
}
|
||||
|
||||
protected function cleanupLines(): void
|
||||
{
|
||||
$spacesOrTabsToRemove = PHP_INT_MAX;
|
||||
|
||||
for ($i = 1; $i < count($this->lines); $i++) {
|
||||
if (empty($this->lines[$i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$spacesOrTabsToRemove = min(strspn($this->lines[$i], " \t"), $spacesOrTabsToRemove);
|
||||
}
|
||||
|
||||
if ($spacesOrTabsToRemove === PHP_INT_MAX) {
|
||||
$spacesOrTabsToRemove = 0;
|
||||
}
|
||||
|
||||
for ($i = 1; $i < count($this->lines); $i++) {
|
||||
$this->lines[$i] = substr($this->lines[$i], $spacesOrTabsToRemove);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backtrace\CodeSnippets;
|
||||
|
||||
class NullSnippetProvider implements SnippetProvider
|
||||
{
|
||||
public function numberOfLines(): int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
public function getLine(int $lineNumber = null): string
|
||||
{
|
||||
return $this->getNextLine();
|
||||
}
|
||||
|
||||
public function getNextLine(): string
|
||||
{
|
||||
return "File not found for code snippet";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backtrace\CodeSnippets;
|
||||
|
||||
interface SnippetProvider
|
||||
{
|
||||
public function numberOfLines(): int;
|
||||
|
||||
public function getLine(int $lineNumber = null): string;
|
||||
|
||||
public function getNextLine(): string;
|
||||
}
|
||||
Reference in New Issue
Block a user