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,72 @@
<?php
namespace Facade\FlareClient\Stacktrace;
use RuntimeException;
class Codesnippet
{
/** @var int */
private $surroundingLine = 1;
/** @var int */
private $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(string $fileName): array
{
if (! file_exists($fileName)) {
return [];
}
try {
$file = new File($fileName);
[$startLineNumber, $endLineNumber] = $this->getBounds($file->numberOfLines());
$code = [];
$line = $file->getLine($startLineNumber);
$currentLineNumber = $startLineNumber;
while ($currentLineNumber <= $endLineNumber) {
$code[$currentLineNumber] = rtrim(substr($line, 0, 250));
$line = $file->getNextLine();
$currentLineNumber++;
}
return $code;
} catch (RuntimeException $exception) {
return [];
}
}
private function getBounds($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];
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Facade\FlareClient\Stacktrace;
use SplFileObject;
class File
{
/** @var \SplFileObject */
private $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();
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Facade\FlareClient\Stacktrace;
class Frame
{
/** @var string */
private $file;
/** @var int */
private $lineNumber;
/** @var string */
private $method;
/** @var string */
private $class;
/** @var bool */
private $isApplicationFrame;
public function __construct(
string $file,
int $lineNumber,
string $method = null,
string $class = null,
bool $isApplicationFrame = false
) {
$this->file = $file;
$this->lineNumber = $lineNumber;
$this->method = $method;
$this->class = $class;
$this->isApplicationFrame = $isApplicationFrame;
}
public function toArray(): array
{
$codeSnippet = (new Codesnippet())
->snippetLineCount(31)
->surroundingLine($this->lineNumber)
->get($this->file);
return [
'line_number' => $this->lineNumber,
'method' => $this->method,
'class' => $this->class,
'code_snippet' => $codeSnippet,
'file' => $this->file,
'is_application_frame' => $this->isApplicationFrame,
];
}
public function getFile(): string
{
return $this->file;
}
public function getLinenumber(): int
{
return $this->lineNumber;
}
public function isApplicationFrame()
{
return $this->isApplicationFrame;
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace Facade\FlareClient\Stacktrace;
use Throwable;
class Stacktrace
{
/** @var \Facade\FlareClient\Stacktrace\Frame[] */
private $frames;
/** @var string */
private $applicationPath;
public static function createForThrowable(Throwable $throwable, ?string $applicationPath = null): self
{
return new static($throwable->getTrace(), $applicationPath, $throwable->getFile(), $throwable->getLine());
}
public static function create(?string $applicationPath = null): self
{
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS & ~DEBUG_BACKTRACE_PROVIDE_OBJECT);
return new static($backtrace, $applicationPath);
}
public function __construct(array $backtrace, ?string $applicationPath = null, string $topmostFile = null, string $topmostLine = null)
{
$this->applicationPath = $applicationPath;
$currentFile = $topmostFile;
$currentLine = $topmostLine;
foreach ($backtrace as $rawFrame) {
if (! $this->frameFromFlare($rawFrame) && ! $this->fileIgnored($currentFile)) {
$this->frames[] = new Frame(
$currentFile,
$currentLine,
$rawFrame['function'] ?? null,
$rawFrame['class'] ?? null,
$this->frameFileFromApplication($currentFile)
);
}
$currentFile = $rawFrame['file'] ?? 'unknown';
$currentLine = $rawFrame['line'] ?? 0;
}
$this->frames[] = new Frame(
$currentFile,
$currentLine,
'[top]'
);
}
protected function frameFromFlare(array $rawFrame): bool
{
return isset($rawFrame['class']) && strpos($rawFrame['class'], 'Facade\\FlareClient\\') === 0;
}
protected function frameFileFromApplication(string $frameFilename): bool
{
$relativeFile = str_replace('\\', DIRECTORY_SEPARATOR, $frameFilename);
if (! empty($this->applicationPath)) {
$relativeFile = array_reverse(explode($this->applicationPath ?? '', $frameFilename, 2))[0];
}
if (strpos($relativeFile, DIRECTORY_SEPARATOR . 'vendor') === 0) {
return false;
}
return true;
}
protected function fileIgnored(string $currentFile): bool
{
$currentFile = str_replace('\\', DIRECTORY_SEPARATOR, $currentFile);
$ignoredFiles = [
'/ignition/src/helpers.php',
];
foreach ($ignoredFiles as $ignoredFile) {
if (strstr($currentFile, $ignoredFile) !== false) {
return true;
}
}
return false;
}
public function firstFrame(): Frame
{
return $this->frames[0];
}
public function toArray(): array
{
return array_map(function (Frame $frame) {
return $frame->toArray();
}, $this->frames);
}
public function firstApplicationFrame(): ?Frame
{
foreach ($this->frames as $index => $frame) {
if ($frame->isApplicationFrame()) {
return $frame;
}
}
return null;
}
public function firstApplicationFrameIndex(): ?int
{
foreach ($this->frames as $index => $frame) {
if ($frame->isApplicationFrame()) {
return $index;
}
}
return null;
}
}