From 916af44ccbbf251b20e98e34732bd8d331e90241 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 30 Apr 2025 10:07:43 +0100 Subject: [PATCH] Revert cached state --- .../Columns/SpatieMediaLibraryImageColumn.php | 54 ++++++++-------- .../src/Tables/Columns/SpatieTagsColumn.php | 62 +++++++++--------- .../support/src/Concerns/HasCellState.php | 63 ++++--------------- .../tables/src/Columns/Summarizers/Count.php | 2 - packages/tables/src/Testing/TestsColumns.php | 8 --- 5 files changed, 69 insertions(+), 120 deletions(-) diff --git a/packages/spatie-laravel-media-library-plugin/src/Tables/Columns/SpatieMediaLibraryImageColumn.php b/packages/spatie-laravel-media-library-plugin/src/Tables/Columns/SpatieMediaLibraryImageColumn.php index 4fb99b832d..2a9dac33c9 100644 --- a/packages/spatie-laravel-media-library-plugin/src/Tables/Columns/SpatieMediaLibraryImageColumn.php +++ b/packages/spatie-laravel-media-library-plugin/src/Tables/Columns/SpatieMediaLibraryImageColumn.php @@ -128,40 +128,38 @@ class SpatieMediaLibraryImageColumn extends ImageColumn */ public function getState(): array { - return $this->cacheState(function (): array { - $record = $this->getRecord(); + $record = $this->getRecord(); - if ($this->hasRelationship($record)) { - $record = $this->getRelationshipResults($record); - } + if ($this->hasRelationship($record)) { + $record = $this->getRelationshipResults($record); + } - $records = Arr::wrap($record); + $records = Arr::wrap($record); - $state = []; + $state = []; - $collection = $this->getCollection() ?? 'default'; + $collection = $this->getCollection() ?? 'default'; - foreach ($records as $record) { - /** @var Model $record */ - $state = [ - ...$state, - ...$record->getRelationValue('media') - ->when( - ! $collection instanceof AllMediaCollections, - fn (MediaCollection $mediaCollection) => $mediaCollection->filter(fn (Media $media): bool => $media->getAttributeValue('collection_name') === $collection), - ) - ->when( - $this->hasMediaFilter(), - fn (Collection $media) => $this->filterMedia($media) - ) - ->sortBy('order_column') - ->pluck('uuid') - ->all(), - ]; - } + foreach ($records as $record) { + /** @var Model $record */ + $state = [ + ...$state, + ...$record->getRelationValue('media') + ->when( + ! $collection instanceof AllMediaCollections, + fn (MediaCollection $mediaCollection) => $mediaCollection->filter(fn (Media $media): bool => $media->getAttributeValue('collection_name') === $collection), + ) + ->when( + $this->hasMediaFilter(), + fn (Collection $media) => $this->filterMedia($media) + ) + ->sortBy('order_column') + ->pluck('uuid') + ->all(), + ]; + } - return array_unique($state); - }); + return array_unique($state); } public function applyEagerLoading(Builder | Relation $query): Builder | Relation diff --git a/packages/spatie-laravel-tags-plugin/src/Tables/Columns/SpatieTagsColumn.php b/packages/spatie-laravel-tags-plugin/src/Tables/Columns/SpatieTagsColumn.php index 1168a7867b..5d4fae938f 100644 --- a/packages/spatie-laravel-tags-plugin/src/Tables/Columns/SpatieTagsColumn.php +++ b/packages/spatie-laravel-tags-plugin/src/Tables/Columns/SpatieTagsColumn.php @@ -28,45 +28,43 @@ class SpatieTagsColumn extends TextColumn */ public function getState(): array { - return $this->cacheState(function (): array { - $state = parent::getState(); + $state = parent::getState(); - if ($state && (! $state instanceof Collection)) { - return $state; + if ($state && (! $state instanceof Collection)) { + return $state; + } + + $record = $this->getRecord(); + + if ($this->hasRelationship($record)) { + $record = $this->getRelationshipResults($record); + } + + $records = Arr::wrap($record); + + $state = []; + + foreach ($records as $record) { + /** @var Model $record */ + if (! (method_exists($record, 'tags') && method_exists($record, 'tagsWithType'))) { + continue; } - $record = $this->getRecord(); + $type = $this->getType(); - if ($this->hasRelationship($record)) { - $record = $this->getRelationshipResults($record); + if ($this->isAnyTagTypeAllowed()) { + $tags = $record->getRelationValue('tags'); + } else { + $tags = $record->tagsWithType($type); } - $records = Arr::wrap($record); + $state = [ + ...$state, + ...$tags->pluck('name')->all(), + ]; + } - $state = []; - - foreach ($records as $record) { - /** @var Model $record */ - if (! (method_exists($record, 'tags') && method_exists($record, 'tagsWithType'))) { - continue; - } - - $type = $this->getType(); - - if ($this->isAnyTagTypeAllowed()) { - $tags = $record->getRelationValue('tags'); - } else { - $tags = $record->tagsWithType($type); - } - - $state = [ - ...$state, - ...$tags->pluck('name')->all(), - ]; - } - - return array_unique($state); - }); + return array_unique($state); } public function type(string | AllTagTypes | Closure | null $type): static diff --git a/packages/support/src/Concerns/HasCellState.php b/packages/support/src/Concerns/HasCellState.php index 3fad55e852..9a6a98454f 100644 --- a/packages/support/src/Concerns/HasCellState.php +++ b/packages/support/src/Concerns/HasCellState.php @@ -25,11 +25,6 @@ trait HasCellState protected ?string $inverseRelationshipName = null; - /** - * @var array - */ - protected array $cachedState = []; - public function inverseRelationship(?string $name): static { $this->inverseRelationshipName = $name; @@ -77,24 +72,22 @@ trait HasCellState public function getState(): mixed { - return $this->cacheState(function (): mixed { - $state = ($this->getStateUsing !== null) ? - $this->evaluate($this->getStateUsing) : - $this->getStateFromRecord(); + $state = ($this->getStateUsing !== null) ? + $this->evaluate($this->getStateUsing) : + $this->getStateFromRecord(); - if (is_string($state) && ($separator = $this->getSeparator())) { - $state = explode($separator, $state); - $state = (count($state) === 1 && blank($state[0])) ? - [] : - $state; - } + if (is_string($state) && ($separator = $this->getSeparator())) { + $state = explode($separator, $state); + $state = (count($state) === 1 && blank($state[0])) ? + [] : + $state; + } - if (blank($state)) { - $state = $this->getDefaultState(); - } + if (blank($state)) { + $state = $this->getDefaultState(); + } - return $state; - }); + return $state; } public function getStateFromRecord(): mixed @@ -133,11 +126,6 @@ trait HasCellState return $state->all(); } - public function clearCachedState(): void - { - $this->cachedState = []; - } - public function separator(string | Closure | null $separator = ','): static { $this->separator = $separator; @@ -300,29 +288,4 @@ trait HasCellState return (string) str($name)->beforeLast('.'); } - - protected function cacheState(Closure $state): mixed - { - $record = $this->getRecord(); - - if (! $record) { - return null; - } - - if ($this instanceof Column) { - $recordKey = $this->getLivewire()->getTableRecordKey($record); - } else { - $recordKey = (string) $record->getKey(); - } - - if (blank($recordKey)) { - return $state(); - } - - if (array_key_exists($recordKey, $this->cachedState)) { - return $this->cachedState[$recordKey]; - } - - return $this->cachedState[$recordKey] = $state(); - } } diff --git a/packages/tables/src/Columns/Summarizers/Count.php b/packages/tables/src/Columns/Summarizers/Count.php index 6a2bbc44ad..9631a8c5c8 100644 --- a/packages/tables/src/Columns/Summarizers/Count.php +++ b/packages/tables/src/Columns/Summarizers/Count.php @@ -39,9 +39,7 @@ class Count extends Summarizer foreach ($query->clone()->distinct()->pluck($attribute) as $value) { $column->record($this->getQuery()->getModel()->setKeyName($attribute)->setAttribute($attribute, $value)); - $column->clearCachedState(); $columnState = $column->getState(); - $column->clearCachedState(); $color = json_encode($column->getColor($columnState)); $icon = $column->getIcon($columnState); diff --git a/packages/tables/src/Testing/TestsColumns.php b/packages/tables/src/Testing/TestsColumns.php index cc473f5314..d2c902a90b 100644 --- a/packages/tables/src/Testing/TestsColumns.php +++ b/packages/tables/src/Testing/TestsColumns.php @@ -180,8 +180,6 @@ class TestsColumns $livewireClass = $this->instance()::class; - $column->clearCachedState(); - $state = $column->getState(); if (is_array($state)) { @@ -218,8 +216,6 @@ class TestsColumns $livewireClass = $this->instance()::class; - $column->clearCachedState(); - $state = $column->getState(); if (is_array($state)) { @@ -256,8 +252,6 @@ class TestsColumns $livewireClass = $this->instance()::class; - $column->clearCachedState(); - Assert::assertEquals( $value, $column->formatState($column->getState()), @@ -285,8 +279,6 @@ class TestsColumns $livewireClass = $this->instance()::class; - $column->clearCachedState(); - Assert::assertFalse( $column->formatState($column->getState()) == $value, message: "Failed asserting that a table column with name [{$name}] does not have a formatted state of [{$value}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",