From e26c73a0a739c2b2913e115eb5112989182d2419 Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 9 Mar 2024 20:50:25 -0600 Subject: [PATCH 1/2] Refactor Globals to locate items for a set using the Stache --- .../Repositories/GlobalVariablesRepository.php | 18 +++++++++++++----- src/Stache/Stores/GlobalVariablesStore.php | 7 +++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Stache/Repositories/GlobalVariablesRepository.php b/src/Stache/Repositories/GlobalVariablesRepository.php index 7e8608f132b..8f93100a34c 100644 --- a/src/Stache/Repositories/GlobalVariablesRepository.php +++ b/src/Stache/Repositories/GlobalVariablesRepository.php @@ -6,7 +6,6 @@ use Statamic\Contracts\Globals\Variables; use Statamic\Globals\VariablesCollection; use Statamic\Stache\Stache; -use Statamic\Support\Str; class GlobalVariablesRepository implements RepositoryContract { @@ -31,12 +30,21 @@ public function find($id): ?Variables return $this->store->getItem($id); } + private function getIdsForSet($handle) + { + return $this->store + ->index('handle') + ->items() + ->where(function ($value) use ($handle) { + return $value == $handle; + })->keys()->all(); + } + public function whereSet($handle): VariablesCollection { - return $this - ->all() - ->filter(fn ($variable) => Str::before($variable->id(), '::') == $handle) - ->values(); + return new VariablesCollection( + $this->store->getItems($this->getIdsForSet($handle)) + ); } public function save($variable) diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php index 77096a34313..331324679ee 100644 --- a/src/Stache/Stores/GlobalVariablesStore.php +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -84,4 +84,11 @@ protected function deleteItemFromDisk($item) $item->globalSet()->removeLocalization($item)->writeFile(); } } + + protected function storeIndexes() + { + return [ + 'handle', + ]; + } } From aea95aaa5c69da8ab1173fe9e663bb990b6e62bc Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 3 Apr 2024 11:08:36 -0400 Subject: [PATCH 2/2] nitpick --- .../Repositories/GlobalVariablesRepository.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Stache/Repositories/GlobalVariablesRepository.php b/src/Stache/Repositories/GlobalVariablesRepository.php index ae823c66200..06d4e7c1705 100644 --- a/src/Stache/Repositories/GlobalVariablesRepository.php +++ b/src/Stache/Repositories/GlobalVariablesRepository.php @@ -42,21 +42,15 @@ public function findOrFail($id): Variables return $variables; } - private function getIdsForSet($handle) + public function whereSet($handle): VariablesCollection { - return $this->store + $keys = $this->store ->index('handle') ->items() - ->where(function ($value) use ($handle) { - return $value == $handle; - })->keys()->all(); - } + ->filter(fn ($value) => $value == $handle) + ->keys(); - public function whereSet($handle): VariablesCollection - { - return new VariablesCollection( - $this->store->getItems($this->getIdsForSet($handle)) - ); + return VariablesCollection::make($this->store->getItems($keys)); } public function save($variable)