Skip to content

Commit

Permalink
Add handlers for pruning and replicating events
Browse files Browse the repository at this point in the history
  • Loading branch information
marvindurot committed Jul 21, 2023
1 parent 41172a3 commit ce40dfb
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,19 @@ 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')
&& $event === 'ing') {
continue;
}

if ($radical != 'fetch') {
$method .= 'e';
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
);
Expand Down

0 comments on commit ce40dfb

Please sign in to comment.