Skip to content

Commit 02ea9cd

Browse files
committed
[Refactor] Pass resource into Eloquent controller hooks
The resource provided by the client is now passed down through the Eloquent controller hooks - saving, saved, creating, created, updating and updated. In addition all the Eloquent hooks have been deleted from the class, and `method_exists` is used instead. This allows a controller class to type-hint the arguments however it needs, plus it can optionally include the second argument (resource) if needed.
1 parent a8f3c13 commit 02ea9cd

File tree

2 files changed

+40
-103
lines changed

2 files changed

+40
-103
lines changed

UPGRADE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ public function read(JsonApiRequest $request) {}
205205
Use the request object to get the resource id, relationship name and document (the JSON API request content). Refer
206206
to the interface for methods available.
207207

208+
#### Eloquent Hooks
209+
210+
The `saving`, `saved`, `creating`, `created`, `updating`, `updated` method hooks now receive the resource provided
211+
by the client as the second method argument. (This object passed implements
212+
`CloudCreativity\JsonApi\Contracts\Object\ResourceInterface`.)
213+
214+
In addition, these methods are no longer implemented in the parent class. This means you can use whatever method
215+
signature you want, including type-hinting the first argument as the specific class of model that is being handled by
216+
the controller.
217+
208218
#### Search All
209219

210220
You now need to always inject a search object into the Eloquent controller, otherwise the controller will return a

src/Http/Controllers/EloquentController.php

Lines changed: 30 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ protected function commit(Model $model, ResourceInterface $resource)
247247
{
248248
$isUpdating = $model->exists;
249249

250-
$this->beforeCommit($model, $isUpdating);
250+
$this->beforeCommit($model, $resource, $isUpdating);
251251

252252
$result = $this->save($model, $resource);
253253

254254
if ($result) {
255-
$this->afterCommit($model, $isUpdating);
255+
$this->afterCommit($model, $resource, $isUpdating);
256256
}
257257

258258
return $result;
@@ -310,34 +310,44 @@ protected function saveRelated($related)
310310
* Determines which callback to use before creating or updating a model.
311311
*
312312
* @param Model $model
313+
* @param ResourceInterface $resource
313314
* @param bool $isUpdating
314315
*/
315-
protected function beforeCommit(Model $model, $isUpdating)
316+
protected function beforeCommit(Model $model, ResourceInterface $resource, $isUpdating)
316317
{
317-
$this->saving($model);
318+
/** Trigger the saving hook if it is implemented */
319+
if (method_exists($this, 'saving')) {
320+
$this->saving($model, $resource);
321+
}
322+
323+
$fn = $isUpdating ? 'updating' : 'creating';
318324

319-
if ($isUpdating) {
320-
$this->updating($model);
321-
} else {
322-
$this->creating($model);
325+
/** Trigger the updating or creating hook if it is implemented */
326+
if (method_exists($this, $fn)) {
327+
call_user_func([$this, $fn], $model, $resource);
323328
}
324329
}
325330

326331
/**
327332
* Determines which callback to use after a model is updated or created.
328333
*
329334
* @param Model $model
335+
* @param ResourceInterface $resource
330336
* @param bool $isUpdating
331337
*/
332-
protected function afterCommit(Model $model, $isUpdating)
338+
protected function afterCommit(Model $model, ResourceInterface $resource, $isUpdating)
333339
{
334-
if ($isUpdating) {
335-
$this->updated($model);
336-
} else {
337-
$this->created($model);
340+
$fn = $isUpdating ? 'updated' : 'created';
341+
342+
/** Trigger the updated or created hook if it is implemented */
343+
if (method_exists($this, $fn)) {
344+
call_user_func([$this, $fn], $model, $resource);
338345
}
339346

340-
$this->saved($model);
347+
/** Trigger the saved hook if it is implemented */
348+
if (method_exists($this, 'saved')) {
349+
$this->saved($model, $resource);
350+
}
341351
}
342352

343353
/**
@@ -348,11 +358,15 @@ protected function afterCommit(Model $model, $isUpdating)
348358
*/
349359
protected function destroy(Model $model)
350360
{
351-
$this->deleting($model);
361+
/** Trigger the deleting hook if it is implemented */
362+
if (method_exists($this, 'deleting')) {
363+
$this->deleting($model);
364+
}
352365

353366
$result = (bool) $model->delete();
354367

355-
if ($result) {
368+
/** Trigger the deleted hook if it is implemented and delete was successful */
369+
if ($result && method_exists($this, 'deleted')) {
356370
$this->deleted($model);
357371
}
358372

@@ -451,91 +465,4 @@ private function doDestroy(Model $model)
451465
});
452466
}
453467

454-
/**
455-
* Called before the model is saved (either creating or updating an existing model).
456-
*
457-
* Child classes can overload this method if they need to do any logic pre-save.
458-
*
459-
* @param Model $model
460-
*/
461-
protected function saving(Model $model)
462-
{
463-
}
464-
465-
/**
466-
* Called after the model has been saved (when a model has been created or updated)
467-
*
468-
* Child classes can overload this method if they need to do any logic post-save.
469-
*
470-
* @param Model $model
471-
*/
472-
protected function saved(Model $model)
473-
{
474-
}
475-
476-
/**
477-
* Called before the model is created.
478-
*
479-
* Child classes can overload this method if they need to do any logic pre-creation.
480-
*
481-
* @param Model $model
482-
*/
483-
protected function creating(Model $model)
484-
{
485-
}
486-
487-
/**
488-
* Called after the model has been created.
489-
*
490-
* Child classes can overload this method if they need to do any logic post-creation.
491-
*
492-
* @param Model $model
493-
*/
494-
protected function created(Model $model)
495-
{
496-
}
497-
498-
/**
499-
* Called before the model is updated.
500-
*
501-
* Child classes can overload this method if they need to do any logic pre-updating.
502-
*
503-
* @param Model $model
504-
*/
505-
protected function updating(Model $model)
506-
{
507-
}
508-
509-
/**
510-
* Called after the model has been updated.
511-
*
512-
* Child classes can overload this method if they need to do any logic post-updating.
513-
*
514-
* @param Model $model
515-
*/
516-
protected function updated(Model $model)
517-
{
518-
}
519-
520-
/**
521-
* Called before the model is destroyed.
522-
*
523-
* Child classes can overload this method if they need to do any logic pre-delete.
524-
*
525-
* @param Model $model
526-
*/
527-
protected function deleting(Model $model)
528-
{
529-
}
530-
531-
/**
532-
* Called after the model has been destroyed.
533-
*
534-
* Child classes can overload this method if they need to do any logic post-delete.
535-
*
536-
* @param Model $model
537-
*/
538-
protected function deleted(Model $model)
539-
{
540-
}
541468
}

0 commit comments

Comments
 (0)