From 38acff8aba58c9a3e18f2bdc8252357558ac440f Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Fri, 3 Jan 2025 12:38:46 +0000 Subject: [PATCH] Fix event firing for extended models --- .../src/Base/Traits/HasModelExtending.php | 19 ++++++++++++++ packages/core/src/LunarServiceProvider.php | 4 +++ .../core/src/Observers/ProductObserver.php | 23 +++++++++++++++++ .../Base/Traits/HasModelExtendingTest.php | 25 +++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 packages/core/src/Observers/ProductObserver.php diff --git a/packages/core/src/Base/Traits/HasModelExtending.php b/packages/core/src/Base/Traits/HasModelExtending.php index a8e0f2a689..4b5d20b93b 100644 --- a/packages/core/src/Base/Traits/HasModelExtending.php +++ b/packages/core/src/Base/Traits/HasModelExtending.php @@ -138,4 +138,23 @@ public static function observe($classes): void $instance->registerObserver($class); } } + + /** + * Fire the given event for the model. + */ + protected function fireModelEvent($event, $halt = true): mixed + { + // Fire the actual models events + $result = parent::fireModelEvent($event, $halt); + + $lunarClass = str_replace('Contracts\\', '', ModelManifest::guessContractClass(static::class)); + + if ($lunarClass == static::class) { + return $result; + } + + return static::$dispatcher->{($halt ? 'until' : 'dispatch')}( + "eloquent.{$event}: ".$lunarClass, $this + ); + } } diff --git a/packages/core/src/LunarServiceProvider.php b/packages/core/src/LunarServiceProvider.php index d4df153a7d..7923d5a532 100644 --- a/packages/core/src/LunarServiceProvider.php +++ b/packages/core/src/LunarServiceProvider.php @@ -68,6 +68,7 @@ use Lunar\Models\Language; use Lunar\Models\Order; use Lunar\Models\OrderLine; +use Lunar\Models\Product; use Lunar\Models\ProductOption; use Lunar\Models\ProductOptionValue; use Lunar\Models\ProductVariant; @@ -83,6 +84,7 @@ use Lunar\Observers\MediaObserver; use Lunar\Observers\OrderLineObserver; use Lunar\Observers\OrderObserver; +use Lunar\Observers\ProductObserver; use Lunar\Observers\ProductOptionObserver; use Lunar\Observers\ProductOptionValueObserver; use Lunar\Observers\ProductVariantObserver; @@ -296,6 +298,8 @@ protected function registerStateListeners() */ protected function registerObservers(): void { + Product::observe(ProductObserver::class); + Channel::observe(ChannelObserver::class); CustomerGroup::observe(CustomerGroupObserver::class); Language::observe(LanguageObserver::class); diff --git a/packages/core/src/Observers/ProductObserver.php b/packages/core/src/Observers/ProductObserver.php new file mode 100644 index 0000000000..947e7e4745 --- /dev/null +++ b/packages/core/src/Observers/ProductObserver.php @@ -0,0 +1,23 @@ +variants()->delete(); + } + + public function restored(Product $product): void + { + $product->variants()->restore(); + } +} diff --git a/tests/core/Unit/Base/Traits/HasModelExtendingTest.php b/tests/core/Unit/Base/Traits/HasModelExtendingTest.php index 24f55d3e09..f7f80ccb8c 100644 --- a/tests/core/Unit/Base/Traits/HasModelExtendingTest.php +++ b/tests/core/Unit/Base/Traits/HasModelExtendingTest.php @@ -68,3 +68,28 @@ function () { ->and(Product::morphName()) ->toBe('product'); }); + +test('core model events are triggered with extended models', function () { + \Illuminate\Support\Facades\Event::fake(); + + $product = \Lunar\Tests\Core\Stubs\Models\Product::factory()->create(); + + $product->delete(); + + \Illuminate\Support\Facades\Event::assertDispatched( + 'eloquent.deleted: ' . Product::class + ); + + \Lunar\Facades\ModelManifest::replace( + \Lunar\Models\Contracts\Product::class, + \Lunar\Tests\Core\Stubs\Models\CustomProduct::class + ); + + $product = \Lunar\Tests\Core\Stubs\Models\CustomProduct::factory()->create(); + + $product->delete(); + + \Illuminate\Support\Facades\Event::assertDispatched( + 'eloquent.deleted: ' . Product::class + ); +});