Skip to content

Commit

Permalink
refactoring hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm committed Nov 5, 2024
1 parent f75f74d commit b1d8a16
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 24 deletions.
11 changes: 3 additions & 8 deletions src/app/Http/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,17 @@ protected function setupConfigurationForCurrentOperation()
/*
* FIRST, run all Operation Closures for this operation.
*
* It's preferred for this to closures first, because
* It's preferred for this to run closures first, because
* (1) setup() is usually higher in a controller than any other method, so it's more intuitive,
* since the first thing you write is the first thing that is being run;
* (2) operations use operation closures themselves, inside their setupXxxDefaults(), and
* you'd like the defaults to be applied before anything you write. That way, anything you
* write is done after the default, so you can remove default settings, etc;
*/
if (! LifecycleHook::has($operationName.':setup_operation_config')) {
LifecycleHook::hookInto($operationName.':setup_operation_config', function () use ($operationName) {
return 'backpack.operations.'.$operationName;
});
}
LifecycleHook::trigger($operationName.':before_setup', [$this]);

$this->crud->loadDefaultOperationSettingsFromConfig(LifecycleHook::trigger($operationName.':setup_operation_config', [$this, $operationName]));
$this->crud->applyConfigurationFromSettings($operationName);

LifecycleHook::trigger($operationName.':before_setup', [$this]);
/*
* THEN, run the corresponding setupXxxOperation if it exists.
*/
Expand Down
15 changes: 10 additions & 5 deletions src/app/Http/Controllers/Operations/Concerns/HasForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li
// Access
$this->crud->allowAccess($operationName);

LifecycleHook::hookInto('crud:setup_operation_config', function () use ($operationName) {
return config()->has('backpack.operations.'.$operationName) ? 'backpack.operations.'.$operationName : 'backpack.operations.form';
});

LifecycleHook::hookInto($operationName.':before_setup', function () use ($operationName) {
LifecycleHook::hookInto($operationName.':before_setup', function() use ($operationName) {
// if the backpack.operations.{operationName} config exists, use that one
// otherwise, use the generic backpack.operations.form config
if (config()->has('backpack.operations.'.$operationName)) {
$this->crud->loadDefaultOperationSettingsFromConfig();
} else {
$this->crud->loadDefaultOperationSettingsFromConfig('backpack.operations.form');
}

// add a reasonable "save and back" save action
$this->crud->addSaveAction([
'name' => 'save_and_back',
'visible' => function ($crud) use ($operationName) {
Expand Down
3 changes: 2 additions & 1 deletion src/app/Http/Controllers/Operations/ListOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
use Illuminate\Support\Facades\Route;

trait ListOperation
Expand Down Expand Up @@ -43,7 +44,7 @@ protected function setupListDefaults()
{
$this->crud->allowAccess('list');

$this->crud->operation('list', function () {
LifecycleHook::hookInto('list:before_setup', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/app/Http/Controllers/Operations/ReorderOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
use Illuminate\Support\Facades\Route;

trait ReorderOperation
Expand Down Expand Up @@ -36,7 +37,7 @@ protected function setupReorderDefaults()
$this->crud->set('reorder.enabled', true);
$this->crud->allowAccess('reorder');

$this->crud->operation('reorder', function () {
LifecycleHook::hookInto('reorder:before_setup', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();
$this->crud->setOperationSetting('reorderColumnNames', [
'parent_id' => 'parent_id',
Expand All @@ -46,7 +47,7 @@ protected function setupReorderDefaults()
]);
});

$this->crud->operation('list', function () {
LifecycleHook::hookInto('list:before_setup', function () {
$this->crud->addButton('top', 'reorder', 'view', 'crud::buttons.reorder');
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/app/Http/Controllers/Operations/ShowOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
use Illuminate\Support\Facades\Route;

trait ShowOperation
Expand Down Expand Up @@ -30,19 +31,19 @@ protected function setupShowDefaults()
$this->crud->allowAccess('show');
$this->crud->setOperationSetting('setFromDb', true);

$this->crud->operation('show', function () {
LifecycleHook::hookInto('show:before_setup', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();

if (! method_exists($this, 'setupShowOperation')) {
$this->autoSetupShowOperation();
}
});

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

$this->crud->operation(['create', 'update'], function () {
LifecycleHook::hookInto(['create:before_setup', 'update:before_setup'], function () {
$this->crud->addSaveAction([
'name' => 'save_and_preview',
'visible' => function ($crud) {
Expand Down
5 changes: 3 additions & 2 deletions src/app/Http/Controllers/Operations/UpdateOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
use Illuminate\Support\Facades\Route;

trait UpdateOperation
Expand Down Expand Up @@ -35,7 +36,7 @@ protected function setupUpdateDefaults()
{
$this->crud->allowAccess('update');

$this->crud->operation('update', function () {
LifecycleHook::hookInto('update:before_setup', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();

if ($this->crud->getModel()->translationEnabled()) {
Expand All @@ -49,7 +50,7 @@ protected function setupUpdateDefaults()
$this->crud->setupDefaultSaveActions();
});

$this->crud->operation(['list', 'show'], function () {
LifecycleHook::hookInto(['list:before_setup', 'show:before_setup'], function () {
$this->crud->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
});
}
Expand Down
6 changes: 6 additions & 0 deletions src/app/Library/CrudPanel/CrudRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\App;
use ReflectionClass;
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;

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

return;
}
Expand All @@ -32,7 +35,10 @@ public static function setupControllerRoutes(string $name, string $routeName, st
str_ends_with($method->getName(), 'Routes')
) {
$method->setAccessible(true);
LifecycleHook::trigger('crud:before_setup_routes', [$name, $routeName, $controller]);
$method->invoke($controllerInstance, $name, $routeName, $controller);
LifecycleHook::trigger('crud:after_setup_routes', [$name, $routeName, $controller]);

}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/Library/CrudPanel/Hooks/LifecycleHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public function trigger(string|array $hooks, array $parameters): void
foreach ($hooks as $hook) {
if (isset($this->hooks[$hook])) {
foreach ($this->hooks[$hook] as $callback) {
$callback(...$parameters);
if($callback instanceof \Closure) {
$callback(...$parameters);
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/Library/CrudPanel/Traits/Operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ public function configureOperation($operations, $closure = false)
* This is called when an operation does setCurrentOperation().
*
*
* @param string|array $operations [description]
* @param string|array $operations
* @return void
*
* @deprecated use LifecycleHook::hookInto($operation.':before_setup', $closure) instead
*/
public function applyConfigurationFromSettings($operations)
{
Expand Down

0 comments on commit b1d8a16

Please sign in to comment.