Skip to content

Commit

Permalink
Use Lazy Collection for artisan commands (#3670)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
patrickomeara authored Jul 18, 2024
1 parent 85d97c9 commit 5c3d40c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Conversions/Commands/RegenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
10 changes: 5 additions & 5 deletions src/MediaCollections/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,8 +68,8 @@ public function handle(
$this->info('All done!');
}

/** @return Collection<int, Media> */
public function getMediaItems(): Collection
/** @return LazyCollection<int, Media> */
public function getMediaItems(): LazyCollection
{
$modelType = $this->argument('modelType');
$collectionName = $this->argument('collectionName');
Expand Down Expand Up @@ -111,8 +111,8 @@ protected function deleteOrphanedMediaItems(): void
});
}

/** @return Collection<int, Media> */
protected function getOrphanedMediaItems(): Collection
/** @return LazyCollection<int, Media> */
protected function getOrphanedMediaItems(): LazyCollection
{
$collectionName = $this->argument('collectionName');

Expand Down
6 changes: 3 additions & 3 deletions src/MediaCollections/Commands/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,8 +41,8 @@ public function handle(MediaRepository $mediaRepository): void
$this->info('All done!');
}

/** @return Collection<int, Media> */
public function getMediaItems(): Collection
/** @return LazyCollection<int, Media> */
public function getMediaItems(): LazyCollection
{
$modelType = $this->argument('modelType');
$collectionName = $this->argument('collectionName');
Expand Down
34 changes: 17 additions & 17 deletions src/MediaCollections/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,60 +41,60 @@ 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
{
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
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Media/MediaRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 5c3d40c

Please sign in to comment.