Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzarbn committed Nov 17, 2021
2 parents e5760c9 + 49c8e77 commit 7fefd8f
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/HandlesRelationManyToManyOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ protected function updatePivotWithTransaction(Request $request, $parentKey, $rel
$query = $this->buildShowFetchQuery($request, $parentEntity, []);
$entity = $this->runShowFetchQuery($request, $query, $parentEntity, $relatedKey);

$this->authorize('update', $entity);
$this->authorize('update', [$entity, $parentEntity]);

$updateResult = $this->performUpdatePivot($request, $parentEntity, $relatedKey, $request->get('pivot', []));

Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/HandlesRelationOneToManyOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function associateWithTransaction(Request $request, $parentKey)
}

$this->authorize('view', $parentEntity);
$this->authorize('update', $entity);
$this->authorize('update', [$entity, $parentEntity]);

$this->performAssociate($request, $parentEntity, $entity);

Expand Down Expand Up @@ -204,7 +204,7 @@ protected function dissociateWithTransaction(Request $request, $parentKey, $rela
return $beforeHookResult;
}

$this->authorize('update', $entity);
$this->authorize('update', [$entity, $parentEntity]);

$this->performDissociate($request, $parentEntity, $entity);

Expand Down
14 changes: 7 additions & 7 deletions src/Concerns/HandlesRelationStandardBatchOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public function batchStore(Request $request, $parentKey)
*/
protected function batchStoreWithTransaction(Request $request, $parentKey)
{
$resourceModelClass = $this->resolveResourceModelClass();

$this->authorize('create', $resourceModelClass);

$parentQuery = $this->buildBatchStoreParentFetchQuery($request, $parentKey);
$parentEntity = $this->runBatchStoreParentFetchQuery($request, $parentQuery, $parentKey);

$resourceModelClass = $this->resolveResourceModelClass();

$this->authorize('create', [$resourceModelClass, $parentEntity]);

$beforeHookResult = $this->beforeBatchStore($request, $parentEntity);
if ($this->hookResponds($beforeHookResult)) {
return $beforeHookResult;
Expand Down Expand Up @@ -194,7 +194,7 @@ protected function batchUpdateWithTransaction(Request $request, $parentKey)

foreach ($entities as $entity) {
/** @var Model $entity */
$this->authorize('update', $entity);
$this->authorize('update', [$entity, $parentEntity]);

$resource = $request->input("resources.{$entity->{$this->keyName()}}");

Expand Down Expand Up @@ -393,7 +393,7 @@ protected function batchDestroyWithTransaction(Request $request, $parentKey)

foreach ($entities as $entity) {
/** @var Model $entity */
$this->authorize($forceDeletes ? 'forceDelete' : 'delete', $entity);
$this->authorize($forceDeletes ? 'forceDelete' : 'delete', [$entity, $parentEntity]);

$this->beforeDestroy($request, $parentEntity, $entity);

Expand Down Expand Up @@ -560,7 +560,7 @@ protected function batchRestoreWithTransaction(Request $request, $parentKey)

foreach ($entities as $entity) {
/** @var Model $entity */
$this->authorize('restore', $entity);
$this->authorize('restore', [$entity, $parentEntity]);

$this->beforeRestore($request, $parentEntity, $entity);

Expand Down
20 changes: 10 additions & 10 deletions src/Concerns/HandlesRelationStandardOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ trait HandlesRelationStandardOperations
*/
public function index(Request $request, $parentKey)
{
$this->authorize('viewAny', $this->resolveResourceModelClass());

$requestedRelations = $this->relationsResolver->requestedRelations($request);

$parentQuery = $this->buildIndexParentFetchQuery($request, $parentKey);
$parentEntity = $this->runIndexParentFetchQuery($request, $parentQuery, $parentKey);

$this->authorize('viewAny', [$this->resolveResourceModelClass(), $parentEntity]);

$beforeHookResult = $this->beforeIndex($request, $parentEntity);
if ($this->hookResponds($beforeHookResult)) {
return $beforeHookResult;
Expand Down Expand Up @@ -251,13 +251,13 @@ public function store(Request $request, $parentKey)
*/
protected function storeWithTransaction(Request $request, $parentKey)
{
$resourceModelClass = $this->resolveResourceModelClass();

$this->authorize('create', $resourceModelClass);

$parentQuery = $this->buildStoreParentFetchQuery($request, $parentKey);
$parentEntity = $this->runStoreParentFetchQuery($request, $parentQuery, $parentKey);

$resourceModelClass = $this->resolveResourceModelClass();

$this->authorize('create', [$resourceModelClass, $parentEntity]);

/** @var Model $entity */
$entity = new $resourceModelClass;

Expand Down Expand Up @@ -434,7 +434,7 @@ public function show(Request $request, $parentKey, $relatedKey = null)
$query = $this->buildShowFetchQuery($request, $parentEntity, $requestedRelations);
$entity = $this->runShowFetchQuery($request, $query, $parentEntity, $relatedKey);

$this->authorize('view', $entity);
$this->authorize('view', [$entity, $parentEntity]);

$entity = $this->cleanupEntity($entity);

Expand Down Expand Up @@ -613,7 +613,7 @@ protected function updateWithTransaction(Request $request, $parentKey, $relatedK
$query = $this->buildUpdateFetchQuery($request, $parentEntity, $requestedRelations);
$entity = $this->runUpdateFetchQuery($request, $query, $parentEntity, $relatedKey);

$this->authorize('update', $entity);
$this->authorize('update', [$entity, $parentEntity]);

$beforeHookResult = $this->beforeUpdate($request, $parentEntity, $entity);
if ($this->hookResponds($beforeHookResult)) {
Expand Down Expand Up @@ -811,7 +811,7 @@ protected function destroyWithTransaction(Request $request, $parentKey, $related
abort(404);
}

$this->authorize($forceDeletes ? 'forceDelete' : 'delete', $entity);
$this->authorize($forceDeletes ? 'forceDelete' : 'delete', [$entity, $parentEntity]);

$beforeHookResult = $this->beforeDestroy($request, $parentEntity, $entity);
if ($this->hookResponds($beforeHookResult)) {
Expand Down Expand Up @@ -994,7 +994,7 @@ protected function restoreWithTransaction(Request $request, $parentKey, $related
$query = $this->buildRestoreFetchQuery($request, $parentEntity, $requestedRelations);
$entity = $this->runRestoreFetchQuery($request, $query, $parentEntity, $relatedKey);

$this->authorize('restore', $entity);
$this->authorize('restore', [$entity, $parentEntity]);

$beforeHookResult = $this->beforeRestore($request, $parentEntity, $entity);
if ($this->hookResponds($beforeHookResult)) {
Expand Down
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
Loading

0 comments on commit 7fefd8f

Please sign in to comment.