Skip to content

Commit

Permalink
Make media lazy loading configurable (#3627)
Browse files Browse the repository at this point in the history
* Make media lazy loading configurable

* Fix config variable name
  • Loading branch information
alidevweb authored Jun 17, 2024
1 parent ed7401c commit 8c0d2ea
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ phpunit.xml
.php-cs-fixer.cache
phpstan.neon
tests/Support/temp/
.idea/
6 changes: 6 additions & 0 deletions config/media-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,10 @@
* If you set this to `/my-subdir`, all your media will be stored in a `/my-subdir` directory.
*/
'prefix' => env('MEDIA_PREFIX', ''),

/*
* When forcing lazy loading, media will be loaded even if you don't eager load media and you have
* disabled lazy loading globally in the service provider.
*/
'force_lazy_loading' => env('FORCE_MEDIA_LIBRARY_LAZY_LOADING', true),
];
8 changes: 7 additions & 1 deletion docs/installation-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,13 @@ return [
* You can specify a prefix for that is used for storing all media.
* If you set this to `/my-subdir`, all your media will be stored in a `/my-subdir` directory.
*/
'prefix' => env('MEDIA_PREFIX', ''),
'prefix' => env('MEDIA_PREFIX', ''),

/*
* When forcing lazy loading, media will be loaded even if you don't eager load media and you have
* disabled lazy loading globally in the service provider.
*/
'force_lazy_loading' => env('FORCE_MEDIA_LIBRARY_LAZY_LOADING', true),
];
```

Expand Down
6 changes: 5 additions & 1 deletion src/InteractsWithMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,12 @@ protected function mediaIsPreloaded(): bool

public function loadMedia(string $collectionName): Collection
{
if (config('media-library.force_lazy_loading') && $this->exists) {
$this->loadMissing('media');
}

$collection = $this->exists
? $this->loadMissing('media')->media
? $this->media
: collect($this->unAttachedMediaLibraryItems)->pluck('media');

$collection = new MediaCollections\Models\Collections\MediaCollection($collection);
Expand Down
38 changes: 38 additions & 0 deletions tests/Feature/Performance/PerformanceTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\LazyLoadingViolationException;
use Illuminate\Support\Facades\Config;

it('can use eagerly loaded media', function () {
foreach (range(1, 10) as $index) {
$testModel = $this->testModelWithConversion->create(['name' => "test{$index}"]);
Expand All @@ -17,3 +21,37 @@

expect(DB::getQueryLog())->toHaveCount(2);
});

it('can lazy load media by default even prevent lazy loading globally enabled', function () {
foreach (range(1, 10) as $index) {
$testModel = $this->testModelWithConversion->create(['name' => "test{$index}"]);
$testModel->addMedia($this->getTestJpg())->preservingOriginal()->toMediaCollection('images');
}

Model::preventLazyLoading();

DB::connection()->enableQueryLog();

$testModels = $this->testModelWithConversion->get();

foreach ($testModels as $testModel) {
$testModel->getFirstMediaUrl('images', 'thumb');
}

expect(DB::getQueryLog())->toHaveCount(12);
});

it('throws an exception when lazy loading is disabled on both the config and globally', function () {
foreach (range(1, 10) as $index) {
$testModel = $this->testModelWithConversion->create(['name' => "test{$index}"]);
$testModel->addMedia($this->getTestJpg())->preservingOriginal()->toMediaCollection('images');
}

Model::preventLazyLoading();

Config::set('media-library.force_lazy_loading', false);

$testModels = $this->testModelWithConversion->get();

expect(fn() => $testModels->first()->getFirstMediaUrl('images', 'thumb'))->toThrow(LazyLoadingViolationException::class);
});

0 comments on commit 8c0d2ea

Please sign in to comment.