From 4608ceb7322dae8a63b6745ef0c0e966468cbd7e Mon Sep 17 00:00:00 2001 From: Marvin Durot Date: Fri, 21 Jul 2023 15:46:26 +0200 Subject: [PATCH 1/3] Add handlers for pruning and replicating events --- src/Database/Model.php | 47 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Database/Model.php b/src/Database/Model.php index 974ccd4d..6f135c1b 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -213,7 +213,7 @@ protected function bootNicerEvents() return; } - $radicals = ['creat', 'sav', 'updat', 'delet', 'fetch']; + $radicals = ['creat', 'sav', 'updat', 'delet', 'fetch', 'prun', 'replicat']; $hooks = ['before' => 'ing', 'after' => 'ed']; foreach ($radicals as $radical) { @@ -221,6 +221,11 @@ protected function bootNicerEvents() $eventMethod = $radical . $event; // saving / saved $method = $hook . ucfirst($radical); // beforeSave / afterSave + if (($radical === 'replicat' || $radical === 'prun') + && $event === 'ed') { + continue; + } + if ($radical != 'fetch') { $method .= 'e'; } @@ -479,6 +484,44 @@ protected function afterFetch() */ } + /** + * Handle the "replicating" model event + */ + protected function beforeReplicate() + { + /** + * @event model.beforeReplicate + * Called before the model is replicated + * > **Note:** also triggered in Winter\Storm\Halcyon\Model + * + * Example usage: + * + * $model->bindEvent('model.beforeReplicate', function () use (\Winter\Storm\Database\Model $model) { + * $model->name = $model->name . ' copy'; + * }); + * + */ + } + + /** + * Handle the "pruning" model event + */ + protected function beforePrune() + { + /** + * @event model.beforePrune + * Called before the model is pruned + * > **Note:** also triggered in Winter\Storm\Halcyon\Model + * + * Example usage: + * + * $model->bindEvent('model.beforePrune', function () use (\Winter\Storm\Database\Model $model) { + * $model->attachment->delete(); + * }); + * + */ + } + /** * Flush the memory cache. * @return void @@ -579,7 +622,7 @@ public function getObservableEvents() [ 'creating', 'created', 'updating', 'updated', 'deleting', 'deleted', 'saving', 'saved', - 'restoring', 'restored', 'fetching', 'fetched' + 'restoring', 'restored', 'fetching', 'fetched', 'pruning', 'replicating' ], $this->observables ); From 67ad30338b6ad9eb9e9e603e6adb0124da29891b Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 21 Jul 2023 11:57:00 -0600 Subject: [PATCH 2/3] Update src/Database/Model.php --- 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 6f135c1b..f51af563 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -221,8 +221,11 @@ protected function bootNicerEvents() $eventMethod = $radical . $event; // saving / saved $method = $hook . ucfirst($radical); // beforeSave / afterSave - if (($radical === 'replicat' || $radical === 'prun') - && $event === 'ed') { + // No replicated / pruned events + if ( + ($radical === 'replicat' || $radical === 'prun') + && $event === 'ed' + ) { continue; } From dfda07c3e36483f0b07a072bd7313cd9a22f7976 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 21 Jul 2023 11:58:59 -0600 Subject: [PATCH 3/3] Update src/Database/Model.php --- src/Database/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Model.php b/src/Database/Model.php index f51af563..9f9d975a 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -223,7 +223,7 @@ protected function bootNicerEvents() // No replicated / pruned events if ( - ($radical === 'replicat' || $radical === 'prun') + ($radical === 'replicat' || $radical === 'prun') && $event === 'ed' ) { continue;