From 893c9c096140c99bef8f6236e8077657f58585b1 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 2 Jul 2024 16:53:25 -0400 Subject: [PATCH 1/4] move updating of entry uris from collection repo into entry repo --- src/Entries/Collection.php | 2 +- .../Repositories/CollectionRepository.php | 3 ++ src/Stache/Repositories/EntryRepository.php | 5 ++++ src/Stache/Stores/CollectionEntriesStore.php | 29 +++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index 3e130a98db7..5afa63b0d96 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -503,7 +503,7 @@ public function save() public function updateEntryUris($ids = null) { - Facades\Collection::updateEntryUris($this, $ids); + Facades\Entry::updateUris($this, $ids); return $this; } diff --git a/src/Stache/Repositories/CollectionRepository.php b/src/Stache/Repositories/CollectionRepository.php index f3afd5d1bff..85def88f83f 100644 --- a/src/Stache/Repositories/CollectionRepository.php +++ b/src/Stache/Repositories/CollectionRepository.php @@ -96,6 +96,9 @@ public function delete(Collection $collection) $this->store->delete($collection); } + /** + * @deprecated Use EntryRepository::updateUris($collection, $ids) + */ public function updateEntryUris(Collection $collection, $ids = null) { $this->store->updateEntryUris($collection, $ids); diff --git a/src/Stache/Repositories/EntryRepository.php b/src/Stache/Repositories/EntryRepository.php index 511d8a7ee66..1058d91ae82 100644 --- a/src/Stache/Repositories/EntryRepository.php +++ b/src/Stache/Repositories/EntryRepository.php @@ -163,4 +163,9 @@ public function applySubstitutions($items) return $this->substitutionsById[$item->id()] ?? $item; }); } + + public function updateUris($collection, $ids = null) + { + $this->store->store($collection->handle())->updateUris($ids); + } } diff --git a/src/Stache/Stores/CollectionEntriesStore.php b/src/Stache/Stores/CollectionEntriesStore.php index a00d70eaaa4..b096fae27c3 100644 --- a/src/Stache/Stores/CollectionEntriesStore.php +++ b/src/Stache/Stores/CollectionEntriesStore.php @@ -251,4 +251,33 @@ public function withoutBlinkingEntryUris($callback) return $return; } + + public function updateUris($ids = null) + { + $this->updateEntriesWithinIndex($this->index('uri'), $ids); + $this->updateEntriesWithinStore($ids); + } + + private function updateEntriesWithinIndex($index, $ids) + { + if (empty($ids)) { + return $index->update(); + } + + collect($ids) + ->map(fn ($id) => Entry::find($id)) + ->filter() + ->each(fn ($entry) => $index->updateItem($entry)); + } + + private function updateEntriesWithinStore($ids) + { + if (empty($ids)) { + $ids = $this->paths()->keys(); + } + + $entries = $this->withoutBlinkingEntryUris(fn () => collect($ids)->map(fn ($id) => Entry::find($id))->filter()); + + $entries->each(fn ($entry) => $this->cacheItem($entry)); + } } From 6a343532d2ce66e9aafdea5e473814d53c7b3ff2 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 2 Jul 2024 16:59:57 -0400 Subject: [PATCH 2/4] wip --- .../Repositories/CollectionRepository.php | 5 +++-- src/Stache/Stores/CollectionsStore.php | 18 ------------------ 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/src/Stache/Repositories/CollectionRepository.php b/src/Stache/Repositories/CollectionRepository.php index 85def88f83f..bd557ff69a6 100644 --- a/src/Stache/Repositories/CollectionRepository.php +++ b/src/Stache/Repositories/CollectionRepository.php @@ -8,6 +8,7 @@ use Statamic\Data\StoresScopedComputedFieldCallbacks; use Statamic\Exceptions\CollectionNotFoundException; use Statamic\Facades\Blink; +use Statamic\Facades\Entry; use Statamic\Stache\Stache; class CollectionRepository implements RepositoryContract @@ -97,11 +98,11 @@ public function delete(Collection $collection) } /** - * @deprecated Use EntryRepository::updateUris($collection, $ids) + * @deprecated Use Entry::updateUris($collection, $ids) */ public function updateEntryUris(Collection $collection, $ids = null) { - $this->store->updateEntryUris($collection, $ids); + Entry::updateUris($collection, $ids); } public function updateEntryOrder(Collection $collection, $ids = null) diff --git a/src/Stache/Stores/CollectionsStore.php b/src/Stache/Stores/CollectionsStore.php index 81b7067b611..8efd66d46f6 100644 --- a/src/Stache/Stores/CollectionsStore.php +++ b/src/Stache/Stores/CollectionsStore.php @@ -82,24 +82,6 @@ protected function getDefaultPublishState($data) return $value === 'published'; } - public function updateEntryUris($collection, $ids = null) - { - $store = Stache::store('entries')->store($collection->handle()); - $this->updateEntriesWithinIndex($store->index('uri'), $ids); - $this->updateEntriesWithinStore($store, $ids); - } - - private function updateEntriesWithinStore($store, $ids) - { - if (empty($ids)) { - $ids = $store->paths()->keys(); - } - - $entries = $store->withoutBlinkingEntryUris(fn () => collect($ids)->map(fn ($id) => Entry::find($id))->filter()); - - $entries->each(fn ($entry) => $store->cacheItem($entry)); - } - public function updateEntryOrder($collection, $ids = null) { $index = Stache::store('entries') From 3009e2ac04c7cd3a38095c5c198353dda72ac892 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 9 Jul 2024 12:03:33 -0400 Subject: [PATCH 3/4] fix test --- tests/Data/Entries/CollectionTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Data/Entries/CollectionTest.php b/tests/Data/Entries/CollectionTest.php index 6eb11b6dafb..40470b12dbb 100644 --- a/tests/Data/Entries/CollectionTest.php +++ b/tests/Data/Entries/CollectionTest.php @@ -865,12 +865,12 @@ public function it_gets_the_uri_and_url_from_the_mounted_entry() } #[Test] - public function it_updates_entry_uris_through_the_repository() + public function it_updates_entry_uris_through_the_entry_repository() { $collection = (new Collection)->handle('test'); - Facades\Collection::shouldReceive('updateEntryUris')->with($collection, null)->once()->ordered(); - Facades\Collection::shouldReceive('updateEntryUris')->with($collection, ['one', 'two'])->once()->ordered(); + Facades\Entry::shouldReceive('updateUris')->with($collection, null)->once()->ordered(); + Facades\Entry::shouldReceive('updateUris')->with($collection, ['one', 'two'])->once()->ordered(); $collection->updateEntryUris(); $collection->updateEntryUris(['one', 'two']); From 7930aca1e2abe3e3ef1b11093fc791d5f1ba369a Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 9 Jul 2024 12:10:46 -0400 Subject: [PATCH 4/4] wip --- src/Entries/Collection.php | 4 +-- .../Repositories/CollectionRepository.php | 6 ++-- src/Stache/Repositories/EntryRepository.php | 10 ++++++ src/Stache/Stores/CollectionEntriesStore.php | 10 ++++++ src/Stache/Stores/CollectionsStore.php | 31 ------------------- tests/Data/Entries/CollectionTest.php | 24 ++++++++++++++ 6 files changed, 50 insertions(+), 35 deletions(-) diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index 5afa63b0d96..7836916ad9f 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -510,14 +510,14 @@ public function updateEntryUris($ids = null) public function updateEntryOrder($ids = null) { - Facades\Collection::updateEntryOrder($this, $ids); + Facades\Entry::updateOrders($this, $ids); return $this; } public function updateEntryParent($ids = null) { - Facades\Collection::updateEntryParent($this, $ids); + Facades\Entry::updateParents($this, $ids); return $this; } diff --git a/src/Stache/Repositories/CollectionRepository.php b/src/Stache/Repositories/CollectionRepository.php index bd557ff69a6..95911347b87 100644 --- a/src/Stache/Repositories/CollectionRepository.php +++ b/src/Stache/Repositories/CollectionRepository.php @@ -105,14 +105,16 @@ public function updateEntryUris(Collection $collection, $ids = null) Entry::updateUris($collection, $ids); } + /** @deprecated Use Entry::updateOrders($collection, $ids) */ public function updateEntryOrder(Collection $collection, $ids = null) { - $this->store->updateEntryOrder($collection, $ids); + Entry::updateOrders($collection, $ids); } + /** @deprecated Use Entry::updateParents($collection, $ids) */ public function updateEntryParent(Collection $collection, $ids = null) { - $this->store->updateEntryParent($collection, $ids); + Entry::updateParents($collection, $ids); } public function whereStructured(): IlluminateCollection diff --git a/src/Stache/Repositories/EntryRepository.php b/src/Stache/Repositories/EntryRepository.php index 1058d91ae82..7078edcea02 100644 --- a/src/Stache/Repositories/EntryRepository.php +++ b/src/Stache/Repositories/EntryRepository.php @@ -168,4 +168,14 @@ public function updateUris($collection, $ids = null) { $this->store->store($collection->handle())->updateUris($ids); } + + public function updateOrders($collection, $ids = null) + { + $this->store->store($collection->handle())->updateOrders($ids); + } + + public function updateParents($collection, $ids = null) + { + $this->store->store($collection->handle())->updateParents($ids); + } } diff --git a/src/Stache/Stores/CollectionEntriesStore.php b/src/Stache/Stores/CollectionEntriesStore.php index b096fae27c3..5c6450e575e 100644 --- a/src/Stache/Stores/CollectionEntriesStore.php +++ b/src/Stache/Stores/CollectionEntriesStore.php @@ -258,6 +258,16 @@ public function updateUris($ids = null) $this->updateEntriesWithinStore($ids); } + public function updateOrders($ids = null) + { + $this->updateEntriesWithinIndex($this->index('order'), $ids); + } + + public function updateParents($ids = null) + { + $this->updateEntriesWithinIndex($this->index('parent'), $ids); + } + private function updateEntriesWithinIndex($index, $ids) { if (empty($ids)) { diff --git a/src/Stache/Stores/CollectionsStore.php b/src/Stache/Stores/CollectionsStore.php index 8efd66d46f6..041fe704ad8 100644 --- a/src/Stache/Stores/CollectionsStore.php +++ b/src/Stache/Stores/CollectionsStore.php @@ -3,7 +3,6 @@ namespace Statamic\Stache\Stores; use Statamic\Facades\Collection; -use Statamic\Facades\Entry; use Statamic\Facades\Path; use Statamic\Facades\Site; use Statamic\Facades\Stache; @@ -82,36 +81,6 @@ protected function getDefaultPublishState($data) return $value === 'published'; } - public function updateEntryOrder($collection, $ids = null) - { - $index = Stache::store('entries') - ->store($collection->handle()) - ->index('order'); - - $this->updateEntriesWithinIndex($index, $ids); - } - - public function updateEntryParent($collection, $ids = null) - { - $index = Stache::store('entries') - ->store($collection->handle()) - ->index('parent'); - - $this->updateEntriesWithinIndex($index, $ids); - } - - private function updateEntriesWithinIndex($index, $ids) - { - if (empty($ids)) { - return $index->update(); - } - - collect($ids) - ->map(fn ($id) => Entry::find($id)) - ->filter() - ->each(fn ($entry) => $index->updateItem($entry)); - } - public function handleFileChanges() { if ($this->fileChangesHandled || ! Stache::isWatcherEnabled()) { diff --git a/tests/Data/Entries/CollectionTest.php b/tests/Data/Entries/CollectionTest.php index 40470b12dbb..a751ec419f6 100644 --- a/tests/Data/Entries/CollectionTest.php +++ b/tests/Data/Entries/CollectionTest.php @@ -876,6 +876,30 @@ public function it_updates_entry_uris_through_the_entry_repository() $collection->updateEntryUris(['one', 'two']); } + #[Test] + public function it_updates_entry_orders_through_the_entry_repository() + { + $collection = (new Collection)->handle('test'); + + Facades\Entry::shouldReceive('updateOrders')->with($collection, null)->once()->ordered(); + Facades\Entry::shouldReceive('updateOrders')->with($collection, ['one', 'two'])->once()->ordered(); + + $collection->updateEntryOrder(); + $collection->updateEntryOrder(['one', 'two']); + } + + #[Test] + public function it_updates_entry_parents_through_the_entry_repository() + { + $collection = (new Collection)->handle('test'); + + Facades\Entry::shouldReceive('updateParents')->with($collection, null)->once()->ordered(); + Facades\Entry::shouldReceive('updateParents')->with($collection, ['one', 'two'])->once()->ordered(); + + $collection->updateEntryParent(); + $collection->updateEntryParent(['one', 'two']); + } + #[Test] #[DataProvider('additionalPreviewTargetProvider')] public function it_gets_and_sets_preview_targets($throughFacade)