From 368558c7a46982341308b55aaf119be9f4fc23bd Mon Sep 17 00:00:00 2001 From: Patrick O'Meara Date: Thu, 18 Jul 2024 12:07:28 +1000 Subject: [PATCH] Use Lazy Collection for artisan commands * this way the models aren't hydrated until they are needed using far less memory. * this change only affects the artisan commands `getCollection()` still returns a collection. --- .../Commands/RegenerateCommand.php | 4 +-- .../Commands/CleanCommand.php | 10 +++--- .../Commands/ClearCommand.php | 6 ++-- src/MediaCollections/MediaRepository.php | 34 +++++++++---------- tests/Feature/Media/MediaRepositoryTest.php | 2 +- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Conversions/Commands/RegenerateCommand.php b/src/Conversions/Commands/RegenerateCommand.php index 9c5626a11..49fb0390c 100644 --- a/src/Conversions/Commands/RegenerateCommand.php +++ b/src/Conversions/Commands/RegenerateCommand.php @@ -6,7 +6,7 @@ use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Support\Arr; -use Illuminate\Support\Collection; +use Illuminate\Support\LazyCollection; use Illuminate\Support\Str; use Spatie\MediaLibrary\Conversions\FileManipulator; use Spatie\MediaLibrary\MediaCollections\MediaRepository; @@ -80,7 +80,7 @@ public function handle(MediaRepository $mediaRepository, FileManipulator $fileMa $this->info('All done!'); } - public function getMediaToBeRegenerated(): Collection + public function getMediaToBeRegenerated(): LazyCollection { // Get this arg first as it can also be passed to the greater-than-id branch $modelType = $this->argument('modelType'); diff --git a/src/MediaCollections/Commands/CleanCommand.php b/src/MediaCollections/Commands/CleanCommand.php index 4fffd5d13..aeca371db 100644 --- a/src/MediaCollections/Commands/CleanCommand.php +++ b/src/MediaCollections/Commands/CleanCommand.php @@ -5,7 +5,7 @@ use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Contracts\Filesystem\Factory; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Support\LazyCollection; use Illuminate\Support\Str; use Spatie\MediaLibrary\Conversions\Conversion; use Spatie\MediaLibrary\Conversions\ConversionCollection; @@ -68,8 +68,8 @@ public function handle( $this->info('All done!'); } - /** @return Collection */ - public function getMediaItems(): Collection + /** @return LazyCollection */ + public function getMediaItems(): LazyCollection { $modelType = $this->argument('modelType'); $collectionName = $this->argument('collectionName'); @@ -111,8 +111,8 @@ protected function deleteOrphanedMediaItems(): void }); } - /** @return Collection */ - protected function getOrphanedMediaItems(): Collection + /** @return LazyCollection */ + protected function getOrphanedMediaItems(): LazyCollection { $collectionName = $this->argument('collectionName'); diff --git a/src/MediaCollections/Commands/ClearCommand.php b/src/MediaCollections/Commands/ClearCommand.php index eef895e23..a91680357 100644 --- a/src/MediaCollections/Commands/ClearCommand.php +++ b/src/MediaCollections/Commands/ClearCommand.php @@ -4,7 +4,7 @@ use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Support\LazyCollection; use Spatie\MediaLibrary\MediaCollections\MediaRepository; use Spatie\MediaLibrary\MediaCollections\Models\Media; @@ -41,8 +41,8 @@ public function handle(MediaRepository $mediaRepository): void $this->info('All done!'); } - /** @return Collection */ - public function getMediaItems(): Collection + /** @return LazyCollection */ + public function getMediaItems(): LazyCollection { $modelType = $this->argument('modelType'); $collectionName = $this->argument('collectionName'); diff --git a/src/MediaCollections/MediaRepository.php b/src/MediaCollections/MediaRepository.php index 66ec3662e..d7b6cbcca 100644 --- a/src/MediaCollections/MediaRepository.php +++ b/src/MediaCollections/MediaRepository.php @@ -4,9 +4,9 @@ use Closure; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Collection as DbCollection; use Illuminate\Support\Arr; use Illuminate\Support\Collection; +use Illuminate\Support\LazyCollection; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\MediaCollections\Models\Media; @@ -41,9 +41,9 @@ protected function applyFilterToMediaCollection( return $media->filter($filter); } - public function all(): DbCollection + public function all(): LazyCollection { - return $this->query()->get(); + return $this->query()->cursor(); } public function allIds(): Collection @@ -51,50 +51,50 @@ public function allIds(): Collection return $this->query()->pluck($this->model->getKeyName()); } - public function getByModelType(string $modelType): DbCollection + public function getByModelType(string $modelType): LazyCollection { - return $this->query()->where('model_type', $modelType)->get(); + return $this->query()->where('model_type', $modelType)->cursor(); } - public function getByIds(array $ids): DbCollection + public function getByIds(array $ids): LazyCollection { - return $this->query()->whereIn($this->model->getKeyName(), $ids)->get(); + return $this->query()->whereIn($this->model->getKeyName(), $ids)->cursor(); } - public function getByIdGreaterThan(int $startingFromId, bool $excludeStartingId = false, string $modelType = ''): DbCollection + public function getByIdGreaterThan(int $startingFromId, bool $excludeStartingId = false, string $modelType = ''): LazyCollection { return $this->query() ->where($this->model->getKeyName(), $excludeStartingId ? '>' : '>=', $startingFromId) ->when($modelType !== '', fn (Builder $q) => $q->where('model_type', $modelType)) - ->get(); + ->cursor(); } - public function getByModelTypeAndCollectionName(string $modelType, string $collectionName): DbCollection + public function getByModelTypeAndCollectionName(string $modelType, string $collectionName): LazyCollection { return $this->query() ->where('model_type', $modelType) ->where('collection_name', $collectionName) - ->get(); + ->cursor(); } - public function getByCollectionName(string $collectionName): DbCollection + public function getByCollectionName(string $collectionName): LazyCollection { return $this->query() ->where('collection_name', $collectionName) - ->get(); + ->cursor(); } - public function getOrphans(): DbCollection + public function getOrphans(): LazyCollection { return $this->orphansQuery() - ->get(); + ->cursor(); } - public function getOrphansByCollectionName(string $collectionName): DbCollection + public function getOrphansByCollectionName(string $collectionName): LazyCollection { return $this->orphansQuery() ->where('collection_name', $collectionName) - ->get(); + ->cursor(); } protected function query(): Builder diff --git a/tests/Feature/Media/MediaRepositoryTest.php b/tests/Feature/Media/MediaRepositoryTest.php index 782858666..65abed492 100644 --- a/tests/Feature/Media/MediaRepositoryTest.php +++ b/tests/Feature/Media/MediaRepositoryTest.php @@ -12,5 +12,5 @@ $mediaRepository = app(MediaRepository::class); - expect($mediaRepository->all()->getQueueableClass())->toEqual(TestCustomMediaModel::class); + expect($mediaRepository->getCollection($this->testModel, 'default')->getQueueableClass())->toEqual(TestCustomMediaModel::class); });