Skip to content

Commit b9c31bb

Browse files
pxpmStyleCIBot
andcommitted
Lifecycle hooks for CRUD operations (#5687)
* wip * Apply fixes from StyleCI [ci skip] [skip ci] * tidy up * Apply fixes from StyleCI [ci skip] [skip ci] * panel hooks * Apply fixes from StyleCI [ci skip] [skip ci] * refactoring :broom * finish implementing hooks * Apply fixes from StyleCI [ci skip] [skip ci] * refactoring hooks * Apply fixes from StyleCI [ci skip] [skip ci] * add hook to delete * allow empty parameters * wip --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 8784f5b commit b9c31bb

File tree

13 files changed

+114
-37
lines changed

13 files changed

+114
-37
lines changed

src/BackpackServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public function register()
9191
return new DatabaseSchema();
9292
});
9393

94+
$this->app->scoped('BackpackLifecycleHooks', function ($app) {
95+
return new app\Library\CrudPanel\Hooks\LifecycleHooks();
96+
});
97+
9498
$this->app->singleton('BackpackViewNamespaces', function ($app) {
9599
return new ViewNamespaces();
96100
});

src/app/Http/Controllers/CrudController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Backpack\CRUD\app\Http\Controllers;
44

55
use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime;
6+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
67
use Illuminate\Foundation\Bus\DispatchesJobs;
78
use Illuminate\Foundation\Validation\ValidatesRequests;
89
use Illuminate\Routing\Controller;
@@ -19,6 +20,7 @@ class CrudController extends Controller
1920
use DispatchesJobs, ValidatesRequests;
2021

2122
public $crud;
23+
2224
public $data = [];
2325

2426
public function __construct()
@@ -40,8 +42,14 @@ public function __construct()
4042

4143
$this->crud->setRequest($request);
4244

45+
LifecycleHook::trigger('crud:before_setup_defaults', [$this]);
4346
$this->setupDefaults();
47+
LifecycleHook::trigger('crud:after_setup_defaults', [$this]);
48+
49+
LifecycleHook::trigger('crud:before_setup', [$this]);
4450
$this->setup();
51+
LifecycleHook::trigger('crud:after_setup', [$this]);
52+
4553
$this->setupConfigurationForCurrentOperation();
4654

4755
return $next($request);
@@ -109,13 +117,15 @@ protected function setupConfigurationForCurrentOperation()
109117
/*
110118
* FIRST, run all Operation Closures for this operation.
111119
*
112-
* It's preferred for this to closures first, because
120+
* It's preferred for this to run closures first, because
113121
* (1) setup() is usually higher in a controller than any other method, so it's more intuitive,
114122
* since the first thing you write is the first thing that is being run;
115123
* (2) operations use operation closures themselves, inside their setupXxxDefaults(), and
116124
* you'd like the defaults to be applied before anything you write. That way, anything you
117125
* write is done after the default, so you can remove default settings, etc;
118126
*/
127+
LifecycleHook::trigger($operationName.':before_setup', [$this]);
128+
119129
$this->crud->applyConfigurationFromSettings($operationName);
120130

121131
/*
@@ -124,5 +134,7 @@ protected function setupConfigurationForCurrentOperation()
124134
if (method_exists($this, $setupClassName)) {
125135
$this->{$setupClassName}();
126136
}
137+
138+
LifecycleHook::trigger($operationName.':after_setup', [$this]);
127139
}
128140
}

src/app/Http/Controllers/Operations/Concerns/HasForm.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\CRUD\app\Http\Controllers\Operations\Concerns;
44

5+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
56
use Illuminate\Support\Facades\Route;
67
use Illuminate\Support\Str;
78

@@ -38,8 +39,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li
3839
// Access
3940
$this->crud->allowAccess($operationName);
4041

41-
// Config
42-
$this->crud->operation($operationName, function () use ($operationName) {
42+
LifecycleHook::hookInto($operationName.':before_setup', function () use ($operationName) {
4343
// if the backpack.operations.{operationName} config exists, use that one
4444
// otherwise, use the generic backpack.operations.form config
4545
if (config()->has('backpack.operations.'.$operationName)) {
@@ -61,8 +61,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li
6161
]);
6262
});
6363

64-
// Default Button
65-
$this->crud->operation(['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) {
64+
LifecycleHook::hookInto(['list:before_setup', 'show:before_setup'], function () use ($operationName, $buttonStack, $buttonMeta) {
6665
$this->crud->button($operationName)->view('crud::buttons.quick')->stack($buttonStack)->meta($buttonMeta);
6766
});
6867
}

src/app/Http/Controllers/Operations/CreateOperation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\CRUD\app\Http\Controllers\Operations;
44

5+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
56
use Illuminate\Support\Facades\Route;
67

78
trait CreateOperation
@@ -35,12 +36,11 @@ protected function setupCreateDefaults()
3536
{
3637
$this->crud->allowAccess('create');
3738

38-
$this->crud->operation('create', function () {
39-
$this->crud->loadDefaultOperationSettingsFromConfig();
39+
LifecycleHook::hookInto('create:before_setup', function () {
4040
$this->crud->setupDefaultSaveActions();
4141
});
4242

43-
$this->crud->operation('list', function () {
43+
LifecycleHook::hookInto('list:before_setup', function () {
4444
$this->crud->addButton('top', 'create', 'view', 'crud::buttons.create');
4545
});
4646
}

src/app/Http/Controllers/Operations/DeleteOperation.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\CRUD\app\Http\Controllers\Operations;
44

5+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
56
use Illuminate\Support\Facades\Route;
67

78
trait DeleteOperation
@@ -29,11 +30,11 @@ protected function setupDeleteDefaults()
2930
{
3031
$this->crud->allowAccess('delete');
3132

32-
$this->crud->operation('delete', function () {
33+
LifecycleHook::hookInto('delete:before_setup', function () {
3334
$this->crud->loadDefaultOperationSettingsFromConfig();
3435
});
3536

36-
$this->crud->operation(['list', 'show'], function () {
37+
LifecycleHook::hookInto(['list:before_setup', 'show:before_setup'], function () {
3738
$this->crud->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end');
3839
});
3940
}

src/app/Http/Controllers/Operations/ListOperation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\CRUD\app\Http\Controllers\Operations;
44

5+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
56
use Illuminate\Support\Facades\Route;
67

78
trait ListOperation
@@ -43,7 +44,7 @@ protected function setupListDefaults()
4344
{
4445
$this->crud->allowAccess('list');
4546

46-
$this->crud->operation('list', function () {
47+
LifecycleHook::hookInto('list:before_setup', function () {
4748
$this->crud->loadDefaultOperationSettingsFromConfig();
4849
$this->crud->setOperationSetting('datatablesUrl', $this->crud->getRoute());
4950
});

src/app/Http/Controllers/Operations/ReorderOperation.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\CRUD\app\Http\Controllers\Operations;
44

5+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
56
use Illuminate\Support\Facades\Route;
67

78
trait ReorderOperation
@@ -36,7 +37,7 @@ protected function setupReorderDefaults()
3637
$this->crud->set('reorder.enabled', true);
3738
$this->crud->allowAccess('reorder');
3839

39-
$this->crud->operation('reorder', function () {
40+
LifecycleHook::hookInto('reorder:before_setup', function () {
4041
$this->crud->loadDefaultOperationSettingsFromConfig();
4142
$this->crud->setOperationSetting('reorderColumnNames', [
4243
'parent_id' => 'parent_id',
@@ -46,7 +47,7 @@ protected function setupReorderDefaults()
4647
]);
4748
});
4849

49-
$this->crud->operation('list', function () {
50+
LifecycleHook::hookInto('list:before_setup', function () {
5051
$this->crud->addButton('top', 'reorder', 'view', 'crud::buttons.reorder');
5152
});
5253
}

src/app/Http/Controllers/Operations/ShowOperation.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\CRUD\app\Http\Controllers\Operations;
44

5+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
56
use Illuminate\Support\Facades\Route;
67

78
trait ShowOperation
@@ -30,19 +31,19 @@ protected function setupShowDefaults()
3031
$this->crud->allowAccess('show');
3132
$this->crud->setOperationSetting('setFromDb', true);
3233

33-
$this->crud->operation('show', function () {
34+
LifecycleHook::hookInto('show:before_setup', function () {
3435
$this->crud->loadDefaultOperationSettingsFromConfig();
3536

3637
if (! method_exists($this, 'setupShowOperation')) {
3738
$this->autoSetupShowOperation();
3839
}
3940
});
4041

41-
$this->crud->operation('list', function () {
42+
LifecycleHook::hookInto(['list:before_setup'], function () {
4243
$this->crud->addButton('line', 'show', 'view', 'crud::buttons.show', 'beginning');
4344
});
4445

45-
$this->crud->operation(['create', 'update'], function () {
46+
LifecycleHook::hookInto(['create:before_setup', 'update:before_setup'], function () {
4647
$this->crud->addSaveAction([
4748
'name' => 'save_and_preview',
4849
'visible' => function ($crud) {

src/app/Http/Controllers/Operations/UpdateOperation.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\CRUD\app\Http\Controllers\Operations;
44

5+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
56
use Illuminate\Support\Facades\Route;
67

78
trait UpdateOperation
@@ -35,7 +36,7 @@ protected function setupUpdateDefaults()
3536
{
3637
$this->crud->allowAccess('update');
3738

38-
$this->crud->operation('update', function () {
39+
LifecycleHook::hookInto('update:before_setup', function () {
3940
$this->crud->loadDefaultOperationSettingsFromConfig();
4041

4142
if ($this->crud->getModel()->translationEnabled()) {
@@ -49,7 +50,7 @@ protected function setupUpdateDefaults()
4950
$this->crud->setupDefaultSaveActions();
5051
});
5152

52-
$this->crud->operation(['list', 'show'], function () {
53+
LifecycleHook::hookInto(['list:before_setup', 'show:before_setup'], function () {
5354
$this->crud->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
5455
});
5556
}

src/app/Library/CrudPanel/CrudRouter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\CRUD\app\Library\CrudPanel;
44

5+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
56
use Illuminate\Support\Facades\App;
67
use ReflectionClass;
78

@@ -18,7 +19,9 @@ public static function setupControllerRoutes(string $name, string $routeName, st
1819
if (empty($setupRoutesMethod->getAttributes(\Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime::class))) {
1920
// when the attribute is not found the developer has overwritten the method
2021
// we will keep the old behavior for backwards compatibility
22+
LifecycleHook::trigger('crud:before_setup_routes', [$name, $routeName, $controller]);
2123
$setupRoutesMethod->invoke(App::make($namespacedController), $name, $routeName, $controller);
24+
LifecycleHook::trigger('crud:after_setup_routes', [$name, $routeName, $controller]);
2225

2326
return;
2427
}
@@ -32,7 +35,9 @@ public static function setupControllerRoutes(string $name, string $routeName, st
3235
str_ends_with($method->getName(), 'Routes')
3336
) {
3437
$method->setAccessible(true);
38+
LifecycleHook::trigger('crud:before_setup_routes', [$name, $routeName, $controller]);
3539
$method->invoke($controllerInstance, $name, $routeName, $controller);
40+
LifecycleHook::trigger('crud:after_setup_routes', [$name, $routeName, $controller]);
3641
}
3742
}
3843
}

0 commit comments

Comments
 (0)