Skip to content

Commit 49c8e77

Browse files
authored
Merge pull request #131 from horstoeko/beforefresh
feat: before*Fresh hook
2 parents 0394152 + d2ee92a commit 49c8e77

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/Concerns/HandlesStandardBatchOperations.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ protected function batchStoreWithTransaction(Request $request)
6262

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

65+
$this->beforeStoreFresh($request, $entity);
66+
6567
$entity = $entity->fresh($requestedRelations);
6668
$entity->wasRecentlyCreated = true;
6769

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

158+
$this->beforeUpdateFresh($request, $entity);
159+
156160
$entity = $entity->fresh($requestedRelations);
157161

158162
$this->afterSave($request, $entity);
@@ -296,6 +300,7 @@ protected function batchDestroyWithTransaction(Request $request)
296300
if (!$forceDeletes) {
297301
$this->performDestroy($entity);
298302
if ($softDeletes) {
303+
$this->beforeDestroyFresh($request, $entity);
299304
$entity = $entity->fresh($requestedRelations);
300305
}
301306
} else {
@@ -414,6 +419,8 @@ protected function batchRestoreWithTransaction(Request $request)
414419

415420
$this->performRestore($entity);
416421

422+
$this->beforeRestoreFresh($request, $entity);
423+
417424
$entity = $entity->fresh($requestedRelations);
418425

419426
$this->afterRestore($request, $entity);

src/Concerns/HandlesStandardOperations.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ protected function storeWithTransaction(Request $request)
174174
$request->all()
175175
);
176176

177+
$beforeStoreFreshResult = $this->beforeStoreFresh($request, $entity);
178+
if ($this->hookResponds($beforeStoreFreshResult)) {
179+
return $beforeStoreFreshResult;
180+
}
181+
177182
$entity = $entity->fresh($requestedRelations);
178183
$entity->wasRecentlyCreated = true;
179184

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

237+
/**
238+
* The hook is executed after creating and before refreshing the resource.
239+
*
240+
* @param Request $request
241+
* @param Model $entity
242+
* @return mixed
243+
*/
244+
protected function beforeStoreFresh(Request $request, Model $entity)
245+
{
246+
return null;
247+
}
248+
232249
/**
233250
* The hook is executed after creating or updating a resource.
234251
*
@@ -398,6 +415,11 @@ protected function updateWithTransaction(Request $request, $key)
398415
$request->all()
399416
);
400417

418+
$beforeUpdateFreshResult = $this->beforeUpdateFresh($request, $entity);
419+
if ($this->hookResponds($beforeUpdateFreshResult)) {
420+
return $beforeUpdateFreshResult;
421+
}
422+
401423
$entity = $entity->fresh($requestedRelations);
402424

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

490+
/**
491+
* The hook is executed after updating and before refreshing the resource.
492+
*
493+
* @param Request $request
494+
* @param Model $entity
495+
* @return mixed
496+
*/
497+
protected function beforeUpdateFresh(Request $request, Model $entity)
498+
{
499+
return null;
500+
}
501+
468502
/**
469503
* The hook is executed after updating a resource.
470504
*
@@ -529,6 +563,11 @@ protected function destroyWithTransaction(Request $request, $key)
529563
if (!$forceDeletes) {
530564
$this->performDestroy($entity);
531565
if ($softDeletes) {
566+
$beforeDestroyFreshResult = $this->beforeDestroyFresh($request, $entity);
567+
if ($this->hookResponds($beforeDestroyFreshResult)) {
568+
return $beforeDestroyFreshResult;
569+
}
570+
532571
$entity = $entity->fresh($requestedRelations);
533572
}
534573
} else {
@@ -610,6 +649,19 @@ protected function performForceDestroy(Model $entity): void
610649
$entity->forceDelete();
611650
}
612651

652+
/**
653+
* The hook is executed after deleting and before refreshing the resource.
654+
* This hook is only called when not using forced deletes
655+
*
656+
* @param Request $request
657+
* @param Model $entity
658+
* @return mixed
659+
*/
660+
protected function beforeDestroyFresh(Request $request, Model $entity)
661+
{
662+
return null;
663+
}
664+
613665
/**
614666
* The hook is executed after deleting a resource.
615667
*
@@ -666,6 +718,11 @@ protected function restoreWithTransaction(Request $request, $key)
666718

667719
$this->performRestore($entity);
668720

721+
$beforeHookResult = $this->beforeRestoreFresh($request, $entity);
722+
if ($this->hookResponds($beforeHookResult)) {
723+
return $beforeHookResult;
724+
}
725+
669726
$entity = $entity->fresh($requestedRelations);
670727

671728
$afterHookResult = $this->afterRestore($request, $entity);
@@ -726,6 +783,19 @@ protected function performRestore(Model $entity): void
726783
$entity->restore();
727784
}
728785

786+
/**
787+
* The hook is executed after force restoring a previously deleted resource but before
788+
* refreshing the resource.
789+
*
790+
* @param Request $request
791+
* @param Model $entity
792+
* @return mixed
793+
*/
794+
protected function beforeRestoreFresh(Request $request, Model $entity)
795+
{
796+
return null;
797+
}
798+
729799
/**
730800
* The hook is executed after force restoring a previously deleted resource.
731801
*

0 commit comments

Comments
 (0)