Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -0,0 +1,15 @@
<?php
namespace Spatie\LaravelIgnition\Support\Composer;
interface Composer
{
/** @return array<string, mixed> */
public function getClassMap(): array;
/** @return array<string, mixed> */
public function getPrefixes(): array;
/** @return array<string, mixed> */
public function getPrefixesPsr4(): array;
}

View File

@@ -0,0 +1,127 @@
<?php
namespace Spatie\LaravelIgnition\Support\Composer;
use function app_path;
use function base_path;
use Illuminate\Support\Str;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class ComposerClassMap
{
/** @var \Spatie\LaravelIgnition\Support\Composer\Composer */
protected object $composer;
protected string $basePath;
public function __construct(?string $autoloaderPath = null)
{
$autoloaderPath = $autoloaderPath ?? base_path('/vendor/autoload.php');
$this->composer = file_exists($autoloaderPath)
? require $autoloaderPath
: new FakeComposer();
$this->basePath = app_path();
}
/** @return array<string, string> */
public function listClasses(): array
{
$classes = $this->composer->getClassMap();
return array_merge($classes, $this->listClassesInPsrMaps());
}
public function searchClassMap(string $missingClass): ?string
{
foreach ($this->composer->getClassMap() as $fqcn => $file) {
$basename = basename($file, '.php');
if ($basename === $missingClass) {
return $fqcn;
}
}
return null;
}
/** @return array<string, mixed> */
public function listClassesInPsrMaps(): array
{
// TODO: This is incorrect. Doesnt list all fqcns. Need to parse namespace? e.g. App\LoginController is wrong
$prefixes = array_merge(
$this->composer->getPrefixes(),
$this->composer->getPrefixesPsr4()
);
$classes = [];
foreach ($prefixes as $namespace => $directories) {
foreach ($directories as $directory) {
if (file_exists($directory)) {
$files = (new Finder)
->in($directory)
->files()
->name('*.php');
foreach ($files as $file) {
if ($file instanceof SplFileInfo) {
$fqcn = $this->getFullyQualifiedClassNameFromFile($namespace, $file);
$classes[$fqcn] = $file->getRelativePathname();
}
}
}
}
}
return $classes;
}
public function searchPsrMaps(string $missingClass): ?string
{
$prefixes = array_merge(
$this->composer->getPrefixes(),
$this->composer->getPrefixesPsr4()
);
foreach ($prefixes as $namespace => $directories) {
foreach ($directories as $directory) {
if (file_exists($directory)) {
$files = (new Finder)
->in($directory)
->files()
->name('*.php');
foreach ($files as $file) {
if ($file instanceof SplFileInfo) {
$basename = basename($file->getRelativePathname(), '.php');
if ($basename === $missingClass) {
return $namespace . basename($file->getRelativePathname(), '.php');
}
}
}
}
}
}
return null;
}
protected function getFullyQualifiedClassNameFromFile(string $rootNamespace, SplFileInfo $file): string
{
$class = trim(str_replace($this->basePath, '', (string)$file->getRealPath()), DIRECTORY_SEPARATOR);
$class = str_replace(
[DIRECTORY_SEPARATOR, 'App\\'],
['\\', app()->getNamespace()],
ucfirst(Str::replaceLast('.php', '', $class))
);
return $rootNamespace . $class;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Spatie\LaravelIgnition\Support\Composer;
class FakeComposer implements Composer
{
/** @return array<string, mixed> */
public function getClassMap(): array
{
return [];
}
/** @return array<string, mixed> */
public function getPrefixes(): array
{
return [];
}
/** @return array<string, mixed> */
public function getPrefixesPsr4(): array
{
return [];
}
}

View File

@@ -0,0 +1,103 @@
<?php
namespace Spatie\LaravelIgnition\Support;
use InvalidArgumentException;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Spatie\FlareClient\Flare;
use Spatie\FlareClient\Report;
use Throwable;
class FlareLogHandler extends AbstractProcessingHandler
{
protected Flare $flare;
protected SentReports $sentReports;
protected int $minimumReportLogLevel = Logger::ERROR;
public function __construct(Flare $flare, SentReports $sentReports, $level = Logger::DEBUG, $bubble = true)
{
$this->flare = $flare;
$this->sentReports = $sentReports;
parent::__construct($level, $bubble);
}
public function setMinimumReportLogLevel(int $level): void
{
if (! in_array($level, Logger::getLevels())) {
throw new InvalidArgumentException('The given minimum log level is not supported.');
}
$this->minimumReportLogLevel = $level;
}
protected function write(array $record): void
{
if (! $this->shouldReport($record)) {
return;
}
if ($this->hasException($record)) {
$report = $this->flare->report($record['context']['exception']);
if ($report) {
$this->sentReports->add($report);
}
return;
}
if (config('flare.send_logs_as_events')) {
if ($this->hasValidLogLevel($record)) {
$this->flare->reportMessage(
$record['message'],
'Log ' . Logger::getLevelName($record['level']),
function (Report $flareReport) use ($record) {
foreach ($record['context'] as $key => $value) {
$flareReport->context($key, $value);
}
}
);
}
}
}
/**
* @param array<string, mixed> $report
*
* @return bool
*/
protected function shouldReport(array $report): bool
{
if (! config('flare.key')) {
return false;
}
return $this->hasException($report) || $this->hasValidLogLevel($report);
}
/**
* @param array<string, mixed> $report
*
* @return bool
*/
protected function hasException(array $report): bool
{
$context = $report['context'];
return isset($context['exception']) && $context['exception'] instanceof Throwable;
}
/**
* @param array<string, mixed> $report
*
* @return bool
*/
protected function hasValidLogLevel(array $report): bool
{
return $report['level'] >= $this->minimumReportLogLevel;
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Spatie\LaravelIgnition\Support;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\LaravelIgnition\Exceptions\ViewException;
use Throwable;
class LaravelDocumentationLinkFinder
{
public function findLinkForThrowable(Throwable $throwable): ?string
{
if ($throwable instanceof ViewException) {
$throwable = $throwable->getPrevious();
}
$majorVersion = LaravelVersion::major();
if (str_contains($throwable->getMessage(), Collection::class)) {
return "https://laravel.com/docs/{$majorVersion}.x/collections#available-methods";
}
$type = $this->getType($throwable);
if (! $type) {
return null;
}
return match ($type) {
'Auth' => "https://laravel.com/docs/{$majorVersion}.x/authentication",
'Broadcasting' => "https://laravel.com/docs/{$majorVersion}.x/broadcasting",
'Container' => "https://laravel.com/docs/{$majorVersion}.x/container",
'Database' => "https://laravel.com/docs/{$majorVersion}.x/eloquent",
'Pagination' => "https://laravel.com/docs/{$majorVersion}.x/pagination",
'Queue' => "https://laravel.com/docs/{$majorVersion}.x/queues",
'Routing' => "https://laravel.com/docs/{$majorVersion}.x/routing",
'Session' => "https://laravel.com/docs/{$majorVersion}.x/session",
'Validation' => "https://laravel.com/docs/{$majorVersion}.x/validation",
'View' => "https://laravel.com/docs/{$majorVersion}.x/views",
default => null,
};
}
protected function getType(?Throwable $throwable): ?string
{
if (! $throwable) {
return null;
}
if (str_contains($throwable::class, 'Illuminate')) {
return Str::between($throwable::class, 'Illuminate\\', '\\');
}
if (str_contains($throwable->getMessage(), 'Illuminate')) {
return explode('\\', Str::between($throwable->getMessage(), 'Illuminate\\', '\\'))[0];
}
return null;
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace Spatie\LaravelIgnition\Support;
class LaravelVersion
{
public static function major(): string
{
return explode('.', app()->version())[0];
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace Spatie\LaravelIgnition\Support;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Livewire\LivewireManager;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
class LivewireComponentParser
{
protected string $componentClass;
protected ReflectionClass $reflectionClass;
public static function create(string $componentAlias): self
{
return new self($componentAlias);
}
public function __construct(protected string $componentAlias)
{
$this->componentClass = app(LivewireManager::class)->getClass($this->componentAlias);
$this->reflectionClass = new ReflectionClass($this->componentClass);
}
public function getComponentClass(): string
{
return $this->componentClass;
}
public function getPropertyNamesLike(string $similar): Collection
{
$properties = collect($this->reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC))
// @phpstan-ignore-next-line
->reject(fn (ReflectionProperty $reflectionProperty) => $reflectionProperty->class !== $this->reflectionClass->name)
->map(fn (ReflectionProperty $reflectionProperty) => $reflectionProperty->name);
$computedProperties = collect($this->reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC))
// @phpstan-ignore-next-line
->reject(fn (ReflectionMethod $reflectionMethod) => $reflectionMethod->class !== $this->reflectionClass->name)
->filter(fn (ReflectionMethod $reflectionMethod) => str_starts_with($reflectionMethod->name, 'get') && str_ends_with($reflectionMethod->name, 'Property'))
->map(fn (ReflectionMethod $reflectionMethod) => lcfirst(Str::of($reflectionMethod->name)->after('get')->before('Property')));
return $this->filterItemsBySimilarity(
$properties->merge($computedProperties),
$similar
);
}
public function getMethodNamesLike(string $similar): Collection
{
$methods = collect($this->reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC))
// @phpstan-ignore-next-line
->reject(fn (ReflectionMethod $reflectionMethod) => $reflectionMethod->class !== $this->reflectionClass->name)
->map(fn (ReflectionMethod $reflectionMethod) => $reflectionMethod->name);
return $this->filterItemsBySimilarity($methods, $similar);
}
protected function filterItemsBySimilarity(Collection $items, string $similar): Collection
{
return $items
->map(function (string $name) use ($similar) {
similar_text($similar, $name, $percentage);
return ['match' => $percentage, 'value' => $name];
})
->sortByDesc('match')
->filter(function (array $item) {
return $item['match'] > 40;
})
->map(function (array $item) {
return $item['value'];
})
->values();
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Spatie\LaravelIgnition\Support;
class RunnableSolutionsGuard
{
/**
* Check if runnable solutions are allowed based on the current
* environment and config.
*
* @return bool
*/
public static function check(): bool
{
if (! config('app.debug')) {
// Never run solutions in when debug mode is not enabled.
return false;
}
if (config('ignition.enable_runnable_solutions') !== null) {
// Allow enabling or disabling runnable solutions regardless of environment
// if the IGNITION_ENABLE_RUNNABLE_SOLUTIONS env var is explicitly set.
return config('ignition.enable_runnable_solutions');
}
if (! app()->environment('local') && ! app()->environment('development')) {
// Never run solutions on non-local environments. This avoids exposing
// applications that are somehow APP_ENV=production with APP_DEBUG=true.
return false;
}
return config('app.debug');
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Spatie\LaravelIgnition\Support;
use Illuminate\Support\Arr;
use Spatie\FlareClient\Report;
class SentReports
{
/** @var array<int, Report> */
protected array $reports = [];
public function add(Report $report): self
{
$this->reports[] = $report;
return $this;
}
/** @return array<int, Report> */
public function all(): array
{
return $this->reports;
}
/** @return array<int, string> */
public function uuids(): array
{
return array_map(fn (Report $report) => $report->trackingUuid(), $this->reports);
}
/** @return array<int, string> */
public function urls(): array
{
return array_map(function (string $trackingUuid) {
return "https://flareapp.io/tracked-occurrence/{$trackingUuid}";
}, $this->uuids());
}
public function latestUuid(): ?string
{
return Arr::last($this->reports)?->trackingUuid();
}
public function latestUrl(): ?string
{
return Arr::last($this->urls());
}
public function clear(): void
{
$this->reports = [];
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace Spatie\LaravelIgnition\Support;
use Illuminate\Support\Collection;
class StringComparator
{
/**
* @param array<int|string, string> $strings
* @param string $input
* @param int $sensitivity
*
* @return string|null
*/
public static function findClosestMatch(array $strings, string $input, int $sensitivity = 4): ?string
{
$closestDistance = -1;
$closestMatch = null;
foreach ($strings as $string) {
$levenshteinDistance = levenshtein($input, $string);
if ($levenshteinDistance === 0) {
$closestMatch = $string;
$closestDistance = 0;
break;
}
if ($levenshteinDistance <= $closestDistance || $closestDistance < 0) {
$closestMatch = $string;
$closestDistance = $levenshteinDistance;
}
}
if ($closestDistance <= $sensitivity) {
return $closestMatch;
}
return null;
}
/**
* @param array<int, string> $strings
* @param string $input
*
* @return string|null
*/
public static function findSimilarText(array $strings, string $input): ?string
{
if (empty($strings)) {
return null;
}
return Collection::make($strings)
->sortByDesc(function (string $string) use ($input) {
similar_text($input, $string, $percentage);
return $percentage;
})
->first();
}
}