From 9c541b91ee648622f1cdda14e2d69f634047448f Mon Sep 17 00:00:00 2001 From: wychoong <67364036+wychoong@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:51:43 +0800 Subject: [PATCH 1/3] doc: add remark for custom model definition --- packages/core/config/media.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/core/config/media.php b/packages/core/config/media.php index d2d0814963..13a1dd29cc 100644 --- a/packages/core/config/media.php +++ b/packages/core/config/media.php @@ -4,6 +4,19 @@ return [ + /* + |-------------------------------------------------------------------------- + | Media Definition + |-------------------------------------------------------------------------- + | + | Specify which media definition should be used when generating media. + | + | note: if extended Lunar's model, you should update the key, + | or default definition will be used + | + | example: App\Models\Product::class => CustomMediaDefinitions::class, + | + */ 'definitions' => [ Lunar\Models\Asset::class => StandardMediaDefinitions::class, Lunar\Models\Brand::class => StandardMediaDefinitions::class, From 1f4c33257e99f9e8ef76d513c6b8fa3cbceaabf0 Mon Sep 17 00:00:00 2001 From: wychoong <67364036+wychoong@users.noreply.github.com> Date: Tue, 19 Nov 2024 14:00:21 +0800 Subject: [PATCH 2/3] add failing test for extended model media definition --- .../Stubs/TestStandardMediaDefinitions.php | 7 +++++ tests/core/Unit/Traits/HasMediaTraitTest.php | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/core/Stubs/TestStandardMediaDefinitions.php diff --git a/tests/core/Stubs/TestStandardMediaDefinitions.php b/tests/core/Stubs/TestStandardMediaDefinitions.php new file mode 100644 index 0000000000..b866c4a5b6 --- /dev/null +++ b/tests/core/Stubs/TestStandardMediaDefinitions.php @@ -0,0 +1,7 @@ +hasGeneratedConversion('zoom'))->toBeTrue(); }); +test('custom conversions are loaded', function () { + Config::set('lunar.media.definitions', [ + Product::class => TestStandardMediaDefinitions::class, + ]); + + $product = invade(new Product); + + expect($product->getDefinitionClass())->toEqual(TestStandardMediaDefinitions::class); +}); + +test('custom conversions are loaded for extended model', function () { + \Lunar\Facades\ModelManifest::replace( + \Lunar\Models\Contracts\Product::class, + \Lunar\Tests\Core\Stubs\Models\Product::class + ); + + Config::set('lunar.media.definitions', [ + Product::class => TestStandardMediaDefinitions::class, + ]); + + $product = invade(app(\Lunar\Models\Contracts\Product::class)); + + expect($product->getDefinitionClass())->toEqual(TestStandardMediaDefinitions::class); +}); + test('images can have fallback url', function () { $testImageUrl = 'https://picsum.photos/200'; config()->set('lunar.media.fallback.url', $testImageUrl); From 03493ade8c1d8aaecb58b6c51d0b4560e7902a4f Mon Sep 17 00:00:00 2001 From: wychoong <67364036+wychoong@users.noreply.github.com> Date: Tue, 19 Nov 2024 14:24:28 +0800 Subject: [PATCH 3/3] use alias for media conversion config --- packages/core/config/media.php | 25 +++++--------------- packages/core/src/Base/Traits/HasMedia.php | 7 +++++- tests/core/Unit/Traits/HasMediaTraitTest.php | 6 ++--- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/packages/core/config/media.php b/packages/core/config/media.php index 13a1dd29cc..05039f101b 100644 --- a/packages/core/config/media.php +++ b/packages/core/config/media.php @@ -4,26 +4,13 @@ return [ - /* - |-------------------------------------------------------------------------- - | Media Definition - |-------------------------------------------------------------------------- - | - | Specify which media definition should be used when generating media. - | - | note: if extended Lunar's model, you should update the key, - | or default definition will be used - | - | example: App\Models\Product::class => CustomMediaDefinitions::class, - | - */ 'definitions' => [ - Lunar\Models\Asset::class => StandardMediaDefinitions::class, - Lunar\Models\Brand::class => StandardMediaDefinitions::class, - Lunar\Models\Collection::class => StandardMediaDefinitions::class, - Lunar\Models\Product::class => StandardMediaDefinitions::class, - Lunar\Models\ProductOption::class => StandardMediaDefinitions::class, - Lunar\Models\ProductOptionValue::class => StandardMediaDefinitions::class, + 'asset' => StandardMediaDefinitions::class, + 'brand' => StandardMediaDefinitions::class, + 'collection' => StandardMediaDefinitions::class, + 'product' => StandardMediaDefinitions::class, + 'product-option' => StandardMediaDefinitions::class, + 'product-option-value' => StandardMediaDefinitions::class, ], 'collection' => 'images', diff --git a/packages/core/src/Base/Traits/HasMedia.php b/packages/core/src/Base/Traits/HasMedia.php index 90fddbd88d..a2876f2b02 100644 --- a/packages/core/src/Base/Traits/HasMedia.php +++ b/packages/core/src/Base/Traits/HasMedia.php @@ -63,6 +63,11 @@ protected function getDefinitionClass() { $conversionClasses = config('lunar.media.definitions', []); - return $conversionClasses[static::class] ?? StandardMediaDefinitions::class; + $alias = \Illuminate\Support\Str::snake(class_basename(static::class)); + + return $conversionClasses[$alias] + ?? $conversionClasses[static::class] // fallback for published config + ?? $conversionClasses[get_parent_class(static::class)] // fallback use parent class + ?? StandardMediaDefinitions::class; } } diff --git a/tests/core/Unit/Traits/HasMediaTraitTest.php b/tests/core/Unit/Traits/HasMediaTraitTest.php index 06e5f39f4a..8c6e82c373 100644 --- a/tests/core/Unit/Traits/HasMediaTraitTest.php +++ b/tests/core/Unit/Traits/HasMediaTraitTest.php @@ -14,7 +14,7 @@ expect($definitions)->toHaveCount(6); - expect($definitions[Product::class])->toEqual(StandardMediaDefinitions::class); + expect($definitions['product'])->toEqual(StandardMediaDefinitions::class); $file = UploadedFile::fake()->image('avatar.jpg'); @@ -32,7 +32,7 @@ test('custom conversions are loaded', function () { Config::set('lunar.media.definitions', [ - Product::class => TestStandardMediaDefinitions::class, + 'product' => TestStandardMediaDefinitions::class, ]); $product = invade(new Product); @@ -47,7 +47,7 @@ ); Config::set('lunar.media.definitions', [ - Product::class => TestStandardMediaDefinitions::class, + 'product' => TestStandardMediaDefinitions::class, ]); $product = invade(app(\Lunar\Models\Contracts\Product::class));