Skip to content

Commit

Permalink
Provide Livewire 3 context
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Jan 26, 2024
1 parent 0fe3b0a commit 151ead4
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/ContextProviders/LaravelLivewireRequestContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Livewire\LivewireManager;
use Livewire\Mechanisms\ComponentRegistry;

class LaravelLivewireRequestContextProvider extends LaravelRequestContextProvider
{
Expand Down Expand Up @@ -40,6 +41,23 @@ public function toArray(): array
/** @return array<string, mixed> */
protected function getLivewireInformation(): array
{
if ($this->request->has('components')) {
$component = $this->request->get('components')[0];


$snapshot = json_decode($component['snapshot'], true);

$class = app(ComponentRegistry::class)->getClass($snapshot['memo']['name']);

return [
'component_class' => $class ?? null,
'data' => $snapshot['data'],
'memo' => $snapshot['memo'],
'updates' => $this->resolveUpdates($component['updates']),
'calls' => $component['calls'],
];
}

/** @phpstan-ignore-next-line */
$componentId = $this->request->input('fingerprint.id');

Expand All @@ -56,12 +74,15 @@ protected function getLivewireInformation(): array
$componentClass = null;
}

/** @phpstan-ignore-next-line */
$updates = $this->request->input('updates') ?? [];

return [
'component_class' => $componentClass,
'component_alias' => $componentAlias,
'component_id' => $componentId,
'data' => $this->resolveData(),
'updates' => $this->resolveUpdates(),
'updates' => $this->resolveUpdates($updates),
];
}

Expand All @@ -86,11 +107,8 @@ protected function resolveData(): array
}

/** @return array<string, mixed> */
protected function resolveUpdates(): array
protected function resolveUpdates(array $updates): array
{
/** @phpstan-ignore-next-line */
$updates = $this->request->input('updates') ?? [];

return array_map(function (array $update) {
$update['payload'] = Arr::except($update['payload'] ?? [], ['id']);

Expand Down

0 comments on commit 151ead4

Please sign in to comment.