Skip to content

Commit

Permalink
Merge pull request #131 from horstoeko/beforefresh
Browse files Browse the repository at this point in the history
feat: before*Fresh hook
  • Loading branch information
alexzarbn authored Nov 16, 2021
2 parents 0394152 + d2ee92a commit 49c8e77
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Concerns/HandlesStandardBatchOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ protected function batchStoreWithTransaction(Request $request)

$this->performStore($request, $entity, $resource);

$this->beforeStoreFresh($request, $entity);

$entity = $entity->fresh($requestedRelations);
$entity->wasRecentlyCreated = true;

Expand Down Expand Up @@ -153,6 +155,8 @@ protected function batchUpdateWithTransaction(Request $request)
$request->input("resources.{$entity->{$this->keyName()}}")
);

$this->beforeUpdateFresh($request, $entity);

$entity = $entity->fresh($requestedRelations);

$this->afterSave($request, $entity);
Expand Down Expand Up @@ -296,6 +300,7 @@ protected function batchDestroyWithTransaction(Request $request)
if (!$forceDeletes) {
$this->performDestroy($entity);
if ($softDeletes) {
$this->beforeDestroyFresh($request, $entity);
$entity = $entity->fresh($requestedRelations);
}
} else {
Expand Down Expand Up @@ -414,6 +419,8 @@ protected function batchRestoreWithTransaction(Request $request)

$this->performRestore($entity);

$this->beforeRestoreFresh($request, $entity);

$entity = $entity->fresh($requestedRelations);

$this->afterRestore($request, $entity);
Expand Down
70 changes: 70 additions & 0 deletions src/Concerns/HandlesStandardOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ protected function storeWithTransaction(Request $request)
$request->all()
);

$beforeStoreFreshResult = $this->beforeStoreFresh($request, $entity);
if ($this->hookResponds($beforeStoreFreshResult)) {
return $beforeStoreFreshResult;
}

$entity = $entity->fresh($requestedRelations);
$entity->wasRecentlyCreated = true;

Expand Down Expand Up @@ -229,6 +234,18 @@ protected function performStore(Request $request, Model $entity, array $attribut
$entity->save();
}

/**
* The hook is executed after creating and before refreshing the resource.
*
* @param Request $request
* @param Model $entity
* @return mixed
*/
protected function beforeStoreFresh(Request $request, Model $entity)
{
return null;
}

/**
* The hook is executed after creating or updating a resource.
*
Expand Down Expand Up @@ -398,6 +415,11 @@ protected function updateWithTransaction(Request $request, $key)
$request->all()
);

$beforeUpdateFreshResult = $this->beforeUpdateFresh($request, $entity);
if ($this->hookResponds($beforeUpdateFreshResult)) {
return $beforeUpdateFreshResult;
}

$entity = $entity->fresh($requestedRelations);

$afterSaveHookResult = $this->afterSave($request, $entity);
Expand Down Expand Up @@ -465,6 +487,18 @@ protected function performUpdate(Request $request, Model $entity, array $attribu
$entity->save();
}

/**
* The hook is executed after updating and before refreshing the resource.
*
* @param Request $request
* @param Model $entity
* @return mixed
*/
protected function beforeUpdateFresh(Request $request, Model $entity)
{
return null;
}

/**
* The hook is executed after updating a resource.
*
Expand Down Expand Up @@ -529,6 +563,11 @@ protected function destroyWithTransaction(Request $request, $key)
if (!$forceDeletes) {
$this->performDestroy($entity);
if ($softDeletes) {
$beforeDestroyFreshResult = $this->beforeDestroyFresh($request, $entity);
if ($this->hookResponds($beforeDestroyFreshResult)) {
return $beforeDestroyFreshResult;
}

$entity = $entity->fresh($requestedRelations);
}
} else {
Expand Down Expand Up @@ -610,6 +649,19 @@ protected function performForceDestroy(Model $entity): void
$entity->forceDelete();
}

/**
* The hook is executed after deleting and before refreshing the resource.
* This hook is only called when not using forced deletes
*
* @param Request $request
* @param Model $entity
* @return mixed
*/
protected function beforeDestroyFresh(Request $request, Model $entity)
{
return null;
}

/**
* The hook is executed after deleting a resource.
*
Expand Down Expand Up @@ -666,6 +718,11 @@ protected function restoreWithTransaction(Request $request, $key)

$this->performRestore($entity);

$beforeHookResult = $this->beforeRestoreFresh($request, $entity);
if ($this->hookResponds($beforeHookResult)) {
return $beforeHookResult;
}

$entity = $entity->fresh($requestedRelations);

$afterHookResult = $this->afterRestore($request, $entity);
Expand Down Expand Up @@ -726,6 +783,19 @@ protected function performRestore(Model $entity): void
$entity->restore();
}

/**
* The hook is executed after force restoring a previously deleted resource but before
* refreshing the resource.
*
* @param Request $request
* @param Model $entity
* @return mixed
*/
protected function beforeRestoreFresh(Request $request, Model $entity)
{
return null;
}

/**
* The hook is executed after force restoring a previously deleted resource.
*
Expand Down

0 comments on commit 49c8e77

Please sign in to comment.