diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index 3e130a98db7..7836916ad9f 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -503,21 +503,21 @@ public function save() public function updateEntryUris($ids = null) { - Facades\Collection::updateEntryUris($this, $ids); + Facades\Entry::updateUris($this, $ids); return $this; } 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 f3afd5d1bff..95911347b87 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 @@ -96,19 +97,24 @@ public function delete(Collection $collection) $this->store->delete($collection); } + /** + * @deprecated Use Entry::updateUris($collection, $ids) + */ public function updateEntryUris(Collection $collection, $ids = null) { - $this->store->updateEntryUris($collection, $ids); + 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 511d8a7ee66..7078edcea02 100644 --- a/src/Stache/Repositories/EntryRepository.php +++ b/src/Stache/Repositories/EntryRepository.php @@ -163,4 +163,19 @@ public function applySubstitutions($items) return $this->substitutionsById[$item->id()] ?? $item; }); } + + 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 a00d70eaaa4..5c6450e575e 100644 --- a/src/Stache/Stores/CollectionEntriesStore.php +++ b/src/Stache/Stores/CollectionEntriesStore.php @@ -251,4 +251,43 @@ public function withoutBlinkingEntryUris($callback) return $return; } + + public function updateUris($ids = null) + { + $this->updateEntriesWithinIndex($this->index('uri'), $ids); + $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)) { + 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)); + } } diff --git a/src/Stache/Stores/CollectionsStore.php b/src/Stache/Stores/CollectionsStore.php index 81b7067b611..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,54 +81,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') - ->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 6eb11b6dafb..a751ec419f6 100644 --- a/tests/Data/Entries/CollectionTest.php +++ b/tests/Data/Entries/CollectionTest.php @@ -865,17 +865,41 @@ 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']); } + #[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)