Skip to content

Commit

Permalink
Add replicating and pruning event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
marvindurot committed Jul 21, 2023
1 parent 41172a3 commit 954121e
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,18 @@ 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) {
foreach ($hooks as $hook => $event) {
$eventMethod = $radical . $event; // saving / saved
$method = $hook . ucfirst($radical); // beforeSave / afterSave

if ($radical === 'replicat' || $radical === 'prun') {
continue;
}

if ($radical != 'fetch') {
$method .= 'e';
}
Expand Down Expand Up @@ -479,6 +483,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
Expand Down Expand Up @@ -579,7 +621,7 @@ public function getObservableEvents()
[
'creating', 'created', 'updating', 'updated',
'deleting', 'deleted', 'saving', 'saved',
'restoring', 'restored', 'fetching', 'fetched'
'restoring', 'restored', 'fetching', 'fetched', 'pruning', 'replicating'
],
$this->observables
);
Expand Down

0 comments on commit 954121e

Please sign in to comment.