Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add replicating and pruning event handlers #149

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,22 @@ 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

// No replicated / pruned events
if (
($radical === 'replicat' || $radical === 'prun')
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
&& $event === 'ed'
) {
continue;
}

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