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,9 @@
<?php
namespace Spatie\LaravelIgnition\ContextProviders;
use Spatie\FlareClient\Context\ConsoleContextProvider;
class LaravelConsoleContextProvider extends ConsoleContextProvider
{
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Spatie\LaravelIgnition\ContextProviders;
use Illuminate\Http\Request;
use Livewire\LivewireManager;
use Spatie\FlareClient\Context\ContextProvider;
use Spatie\FlareClient\Context\ContextProviderDetector;
class LaravelContextProviderDetector implements ContextProviderDetector
{
public function detectCurrentContext(): ContextProvider
{
if (app()->runningInConsole()) {
return new LaravelConsoleContextProvider($_SERVER['argv'] ?? []);
}
$request = app(Request::class);
if ($this->isRunningLiveWire($request)) {
return new LaravelLivewireRequestContextProvider($request, app(LivewireManager::class));
}
return new LaravelRequestContextProvider($request);
}
protected function isRunningLiveWire(Request $request): bool
{
return $request->hasHeader('x-livewire') && $request->hasHeader('referer');
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace Spatie\LaravelIgnition\ContextProviders;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Livewire\LivewireManager;
class LaravelLivewireRequestContextProvider extends LaravelRequestContextProvider
{
public function __construct(
Request $request,
protected LivewireManager $livewireManager
) {
parent::__construct($request);
}
/** @return array<string, string> */
public function getRequest(): array
{
$properties = parent::getRequest();
$properties['method'] = $this->livewireManager->originalMethod();
$properties['url'] = $this->livewireManager->originalUrl();
return $properties;
}
/** @return array<int|string, mixed> */
public function toArray(): array
{
$properties = parent::toArray();
$properties['livewire'] = $this->getLivewireInformation();
return $properties;
}
/** @return array<string, mixed> */
protected function getLivewireInformation(): array
{
/** @phpstan-ignore-next-line */
$componentId = $this->request->input('fingerprint.id');
/** @phpstan-ignore-next-line */
$componentAlias = $this->request->input('fingerprint.name');
if ($componentAlias === null) {
return [];
}
try {
$componentClass = $this->livewireManager->getClass($componentAlias);
} catch (Exception $e) {
$componentClass = null;
}
return [
'component_class' => $componentClass,
'component_alias' => $componentAlias,
'component_id' => $componentId,
'data' => $this->resolveData(),
'updates' => $this->resolveUpdates(),
];
}
/** @return array<string, mixed> */
protected function resolveData(): array
{
/** @phpstan-ignore-next-line */
$data = $this->request->input('serverMemo.data') ?? [];
/** @phpstan-ignore-next-line */
$dataMeta = $this->request->input('serverMemo.dataMeta') ?? [];
foreach ($dataMeta['modelCollections'] ?? [] as $key => $value) {
$data[$key] = array_merge($data[$key] ?? [], $value);
}
foreach ($dataMeta['models'] ?? [] as $key => $value) {
$data[$key] = array_merge($data[$key] ?? [], $value);
}
return $data;
}
/** @return array<string, mixed> */
protected function resolveUpdates(): array
{
/** @phpstan-ignore-next-line */
$updates = $this->request->input('updates') ?? [];
return array_map(function (array $update) {
$update['payload'] = Arr::except($update['payload'] ?? [], ['id']);
return $update;
}, $updates);
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace Spatie\LaravelIgnition\ContextProviders;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request as LaravelRequest;
use Spatie\FlareClient\Context\RequestContextProvider;
use Symfony\Component\HttpFoundation\Request as SymphonyRequest;
use Throwable;
class LaravelRequestContextProvider extends RequestContextProvider
{
protected null|SymphonyRequest|LaravelRequest $request;
public function __construct(LaravelRequest $request)
{
$this->request = $request;
}
/** @return null|array<string, mixed> */
public function getUser(): array|null
{
try {
/** @var object|null $user */
/** @phpstan-ignore-next-line */
$user = $this->request?->user();
if (! $user) {
return null;
}
} catch (Throwable) {
return null;
}
try {
if (method_exists($user, 'toFlare')) {
return $user->toFlare();
}
if (method_exists($user, 'toArray')) {
return $user->toArray();
}
} catch (Throwable $e) {
return null;
}
return null;
}
/** @return null|array<string, mixed> */
public function getRoute(): array|null
{
/**
* @phpstan-ignore-next-line
* @var \Illuminate\Routing\Route|null $route
*/
$route = $this->request->route();
if (! $route) {
return null;
}
return [
'route' => $route->getName(),
'routeParameters' => $this->getRouteParameters(),
'controllerAction' => $route->getActionName(),
'middleware' => array_values($route->gatherMiddleware() ?? []),
];
}
/** @return array<int, mixed> */
protected function getRouteParameters(): array
{
try {
/** @phpstan-ignore-next-line */
return collect(optional($this->request->route())->parameters ?? [])
->map(fn ($parameter) => $parameter instanceof Model ? $parameter->withoutRelations() : $parameter)
->map(function ($parameter) {
return method_exists($parameter, 'toFlare') ? $parameter->toFlare() : $parameter;
})
->toArray();
} catch (Throwable) {
return [];
}
}
/** @return array<int, mixed> */
public function toArray(): array
{
$properties = parent::toArray();
if ($route = $this->getRoute()) {
$properties['route'] = $route;
}
if ($user = $this->getUser()) {
$properties['user'] = $user;
}
return $properties;
}
}