From 0478ba7377fac2d9915fc8fbd0978c7572a4d4ee Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 4 Apr 2023 07:09:24 -0400 Subject: [PATCH 1/2] Change order of event firing/method calls --- src/Database/Model.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Database/Model.php b/src/Database/Model.php index d03442c5e..277cffedd 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -183,11 +183,14 @@ protected function bootNicerEvents() } self::$eventMethod(function ($model) use ($method) { - $model->fireEvent('model.' . $method); + $result = null; if ($model->methodExists($method)) { - return $model->$method(); + $result = $model->$method(); } + $model->fireEvent('model.' . $method); + + return $result; }); } } From 2289e038fc7e8f77c1a9dacfe594ed163ee5cdc7 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 5 Aug 2023 17:59:15 -0400 Subject: [PATCH 2/2] call the event after the model method is called everywhere --- src/Database/Traits/SoftDelete.php | 12 ++++++------ src/Database/Traits/Validation.php | 16 ++++++++-------- src/Halcyon/Model.php | 7 +++++-- src/Halcyon/Traits/Validation.php | 14 +++++++------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/Database/Traits/SoftDelete.php b/src/Database/Traits/SoftDelete.php index 1b7243514..4c2320018 100644 --- a/src/Database/Traits/SoftDelete.php +++ b/src/Database/Traits/SoftDelete.php @@ -24,6 +24,9 @@ public static function bootSoftDelete() static::addGlobalScope(new SoftDeletingScope); static::restoring(function ($model) { + if ($model->methodExists('beforeRestore')) { + $model->beforeRestore(); + } /** * @event model.beforeRestore * Called before the model is restored from a soft delete @@ -36,12 +39,12 @@ public static function bootSoftDelete() * */ $model->fireEvent('model.beforeRestore'); - if ($model->methodExists('beforeRestore')) { - $model->beforeRestore(); - } }); static::restored(function ($model) { + if ($model->methodExists('afterRestore')) { + $model->afterRestore(); + } /** * @event model.afterRestore * Called after the model is restored from a soft delete @@ -54,9 +57,6 @@ public static function bootSoftDelete() * */ $model->fireEvent('model.afterRestore'); - if ($model->methodExists('afterRestore')) { - $model->afterRestore(); - } }); } diff --git a/src/Database/Traits/Validation.php b/src/Database/Traits/Validation.php index a4f68504b..e7e6f59ea 100644 --- a/src/Database/Traits/Validation.php +++ b/src/Database/Traits/Validation.php @@ -174,6 +174,10 @@ public function validate($rules = null, $customMessages = null, $attributeNames ? $this->throwOnValidation : true; + if ($this->methodExists('beforeValidate')) { + $this->beforeValidate(); + } + /** * @event model.beforeValidate * Called before the model is validated @@ -194,10 +198,6 @@ public function validate($rules = null, $customMessages = null, $attributeNames return false; } - if ($this->methodExists('beforeValidate')) { - $this->beforeValidate(); - } - /* * Perform validation */ @@ -323,6 +323,10 @@ public function validate($rules = null, $customMessages = null, $attributeNames } } + if ($this->methodExists('afterValidate')) { + $this->afterValidate(); + } + /** * @event model.afterValidate * Called after the model is validated @@ -337,10 +341,6 @@ public function validate($rules = null, $customMessages = null, $attributeNames $this->fireModelEvent('validated', false); $this->fireEvent('model.afterValidate'); - if ($this->methodExists('afterValidate')) { - $this->afterValidate(); - } - if (!$success && $throwOnValidation) { throw new ModelException($this); } diff --git a/src/Halcyon/Model.php b/src/Halcyon/Model.php index 65ca0ee4e..c796ebf23 100644 --- a/src/Halcyon/Model.php +++ b/src/Halcyon/Model.php @@ -239,11 +239,14 @@ protected function bootNicerEvents() } self::$eventMethod(function ($model) use ($method) { - $model->fireEvent('model.' . $method); + $result = null; if ($model->methodExists($method)) { - return $model->$method(); + $result = $model->$method(); } + $model->fireEvent('model.' . $method); + + return $result; }); } } diff --git a/src/Halcyon/Traits/Validation.php b/src/Halcyon/Traits/Validation.php index 0c6a1950d..6861d8997 100644 --- a/src/Halcyon/Traits/Validation.php +++ b/src/Halcyon/Traits/Validation.php @@ -120,6 +120,10 @@ public function validate($rules = null, $customMessages = null, $attributeNames ? $this->throwOnValidation : true; + if ($this->methodExists('beforeValidate')) { + $this->beforeValidate(); + } + if (($this->fireModelEvent('validating') === false) || ($this->fireEvent('model.beforeValidate') === false)) { if ($throwOnValidation) { throw new ModelException($this); @@ -128,10 +132,6 @@ public function validate($rules = null, $customMessages = null, $attributeNames return false; } - if ($this->methodExists('beforeValidate')) { - $this->beforeValidate(); - } - /* * Perform validation */ @@ -209,13 +209,13 @@ public function validate($rules = null, $customMessages = null, $attributeNames } } - $this->fireModelEvent('validated', false); - $this->fireEvent('model.afterValidate'); - if ($this->methodExists('afterValidate')) { $this->afterValidate(); } + $this->fireModelEvent('validated', false); + $this->fireEvent('model.afterValidate'); + if (!$success && $throwOnValidation) { throw new ModelException($this); }