From ce40dfbc6b854928f9f98d60f4f74d76096250d7 Mon Sep 17 00:00:00 2001 From: Marvin Durot Date: Fri, 21 Jul 2023 15:45:06 +0200 Subject: [PATCH] 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..fbca7fde 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 === 'ing') { + 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 );