From 85e9e7a810919e9fad476fcf6c92af768a215dc1 Mon Sep 17 00:00:00 2001 From: pxpm Date: Thu, 10 Oct 2024 12:19:59 +0100 Subject: [PATCH 01/13] wip --- src/app/Http/Controllers/CrudController.php | 13 ++++++++- .../Operations/Concerns/HasForm.php | 23 ++++++--------- .../Operations/CreateOperation.php | 7 +++-- .../Library/CrudPanel/Hooks/BackpackHooks.php | 29 +++++++++++++++++++ .../CrudPanel/Hooks/OperationHooks.php | 24 +++++++++++++++ .../Library/CrudPanel/Traits/Operations.php | 10 +++++-- 6 files changed, 86 insertions(+), 20 deletions(-) create mode 100644 src/app/Library/CrudPanel/Hooks/BackpackHooks.php create mode 100644 src/app/Library/CrudPanel/Hooks/OperationHooks.php diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index cc929b75ac..3c62ec86c2 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -3,6 +3,8 @@ namespace Backpack\CRUD\app\Http\Controllers; use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller; @@ -116,13 +118,22 @@ protected function setupConfigurationForCurrentOperation() * 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; */ - $this->crud->applyConfigurationFromSettings($operationName); + if (! BackpackHooks::has(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName)) { + BackpackHooks::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () { + $this->crud->loadDefaultOperationSettingsFromConfig(); + }); + } + + $this->crud->loadDefaultOperationSettingsFromConfig(BackpackHooks::run(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, [$this])); + BackpackHooks::run(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, [$this]); /* * THEN, run the corresponding setupXxxOperation if it exists. */ if (method_exists($this, $setupClassName)) { $this->{$setupClassName}(); } + + BackpackHooks::run(OperationHooks::AFTER_OPERATION_SETUP, $operationName, [$this]); } } diff --git a/src/app/Http/Controllers/Operations/Concerns/HasForm.php b/src/app/Http/Controllers/Operations/Concerns/HasForm.php index 1e38e83dba..98d2f4bdfd 100644 --- a/src/app/Http/Controllers/Operations/Concerns/HasForm.php +++ b/src/app/Http/Controllers/Operations/Concerns/HasForm.php @@ -2,6 +2,8 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations\Concerns; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; use Illuminate\Support\Facades\Route; use Illuminate\Support\Str; @@ -38,19 +40,13 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li // Access $this->crud->allowAccess($operationName); - // Config - $this->crud->operation($operationName, 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 + BackpackHooks::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () use ($operationName) { + return config()->has('backpack.operations.'.$operationName) ? 'backpack.operations.'.$operationName : 'backpack.operations.form'; + }); + + BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, function () use ($operationName) { $this->crud->addSaveAction([ - 'name' => 'save_and_back', + 'name' => 'save_and_back', 'visible' => function ($crud) use ($operationName) { return $crud->hasAccess($operationName); }, @@ -61,8 +57,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li ]); }); - // Default Button - $this->crud->operation(['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) { + BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, ['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) { $this->crud->button($operationName)->view('crud::buttons.quick')->stack($buttonStack)->meta($buttonMeta); }); } diff --git a/src/app/Http/Controllers/Operations/CreateOperation.php b/src/app/Http/Controllers/Operations/CreateOperation.php index 58ca954733..3ea6c5f142 100644 --- a/src/app/Http/Controllers/Operations/CreateOperation.php +++ b/src/app/Http/Controllers/Operations/CreateOperation.php @@ -2,6 +2,8 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; use Illuminate\Support\Facades\Route; trait CreateOperation @@ -35,12 +37,11 @@ protected function setupCreateDefaults() { $this->crud->allowAccess('create'); - $this->crud->operation('create', function () { - $this->crud->loadDefaultOperationSettingsFromConfig(); + BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, 'create', function () { $this->crud->setupDefaultSaveActions(); }); - $this->crud->operation('list', function () { + BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, 'list', function () { $this->crud->addButton('top', 'create', 'view', 'crud::buttons.create'); }); } diff --git a/src/app/Library/CrudPanel/Hooks/BackpackHooks.php b/src/app/Library/CrudPanel/Hooks/BackpackHooks.php new file mode 100644 index 0000000000..ef5163f0de --- /dev/null +++ b/src/app/Library/CrudPanel/Hooks/BackpackHooks.php @@ -0,0 +1,29 @@ +configureOperation($operations, $closure); + BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operations, $closure); } /** * Store a closure which configures a certain operation inside settings. - * Allc configurations are put inside that operation's namespace. + * All configurations are put inside that operation's namespace. * Ex: show.configuration. * * @param string|array $operation Operation name in string form * @param bool|\Closure $closure Code that calls CrudPanel methods. * @return void + * @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead */ public function configureOperation($operations, $closure = false) { @@ -93,6 +98,7 @@ public function configureOperation($operations, $closure = false) * * @param string|array $operations [description] * @return void + * @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead */ public function applyConfigurationFromSettings($operations) { From 037cb0c4e7125ece7fea2c5514645e511efd350a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 10 Oct 2024 11:20:13 +0000 Subject: [PATCH 02/13] Apply fixes from StyleCI [ci skip] [skip ci] --- src/app/Http/Controllers/Operations/Concerns/HasForm.php | 2 +- src/app/Library/CrudPanel/Traits/Operations.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/app/Http/Controllers/Operations/Concerns/HasForm.php b/src/app/Http/Controllers/Operations/Concerns/HasForm.php index 98d2f4bdfd..61f1bdc09d 100644 --- a/src/app/Http/Controllers/Operations/Concerns/HasForm.php +++ b/src/app/Http/Controllers/Operations/Concerns/HasForm.php @@ -46,7 +46,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, function () use ($operationName) { $this->crud->addSaveAction([ - 'name' => 'save_and_back', + 'name' => 'save_and_back', 'visible' => function ($crud) use ($operationName) { return $crud->hasAccess($operationName); }, diff --git a/src/app/Library/CrudPanel/Traits/Operations.php b/src/app/Library/CrudPanel/Traits/Operations.php index 0d862eee04..dd329eceaa 100644 --- a/src/app/Library/CrudPanel/Traits/Operations.php +++ b/src/app/Library/CrudPanel/Traits/Operations.php @@ -62,6 +62,7 @@ public function setCurrentOperation($operation_name) * @param string|array $operation Operation name in string form * @param bool|\Closure $closure Code that calls CrudPanel methods. * @return void + * * @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead */ public function operation($operations, $closure = false) @@ -77,6 +78,7 @@ public function operation($operations, $closure = false) * @param string|array $operation Operation name in string form * @param bool|\Closure $closure Code that calls CrudPanel methods. * @return void + * * @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead */ public function configureOperation($operations, $closure = false) @@ -98,6 +100,7 @@ public function configureOperation($operations, $closure = false) * * @param string|array $operations [description] * @return void + * * @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead */ public function applyConfigurationFromSettings($operations) From 2761dbc57833aed20a757f9e75387264a1911cb3 Mon Sep 17 00:00:00 2001 From: pxpm Date: Thu, 10 Oct 2024 13:53:35 +0100 Subject: [PATCH 03/13] tidy up --- src/BackpackServiceProvider.php | 4 +++ src/app/Http/Controllers/CrudController.php | 14 ++++---- .../Operations/Concerns/HasForm.php | 8 ++--- .../Operations/CreateOperation.php | 6 ++-- .../Library/CrudPanel/Hooks/BackpackHooks.php | 29 ---------------- .../Hooks/Contracts/OperationHook.php | 20 +++++++++++ .../CrudPanel/Hooks/OperationHooks.php | 34 ++++++++++++++----- .../Library/CrudPanel/Traits/Operations.php | 30 ++++------------ 8 files changed, 71 insertions(+), 74 deletions(-) delete mode 100644 src/app/Library/CrudPanel/Hooks/BackpackHooks.php create mode 100644 src/app/Library/CrudPanel/Hooks/Contracts/OperationHook.php diff --git a/src/BackpackServiceProvider.php b/src/BackpackServiceProvider.php index c3119fc4a6..798210d6eb 100644 --- a/src/BackpackServiceProvider.php +++ b/src/BackpackServiceProvider.php @@ -91,6 +91,10 @@ public function register() return new DatabaseSchema(); }); + $this->app->scoped('operation-hook', function ($app) { + return new \Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks(); + }); + $this->app->singleton('BackpackViewNamespaces', function ($app) { return new ViewNamespaces(); }); diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index 3c62ec86c2..ca243473c5 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -3,7 +3,7 @@ namespace Backpack\CRUD\app\Http\Controllers; use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\OperationHook; use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; @@ -118,15 +118,15 @@ protected function setupConfigurationForCurrentOperation() * 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 (! BackpackHooks::has(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName)) { - BackpackHooks::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () { - $this->crud->loadDefaultOperationSettingsFromConfig(); + if (! OperationHook::has(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName)) { + OperationHook::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () use ($operationName) { + return 'backpack.operations.'.$operationName; }); } - $this->crud->loadDefaultOperationSettingsFromConfig(BackpackHooks::run(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, [$this])); + $this->crud->loadDefaultOperationSettingsFromConfig(OperationHook::run(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, [$this])); - BackpackHooks::run(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, [$this]); + OperationHook::run(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, [$this]); /* * THEN, run the corresponding setupXxxOperation if it exists. */ @@ -134,6 +134,6 @@ protected function setupConfigurationForCurrentOperation() $this->{$setupClassName}(); } - BackpackHooks::run(OperationHooks::AFTER_OPERATION_SETUP, $operationName, [$this]); + OperationHook::run(OperationHooks::AFTER_OPERATION_SETUP, $operationName, [$this]); } } diff --git a/src/app/Http/Controllers/Operations/Concerns/HasForm.php b/src/app/Http/Controllers/Operations/Concerns/HasForm.php index 61f1bdc09d..9f4a57f5d8 100644 --- a/src/app/Http/Controllers/Operations/Concerns/HasForm.php +++ b/src/app/Http/Controllers/Operations/Concerns/HasForm.php @@ -2,7 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations\Concerns; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\OperationHook; use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; use Illuminate\Support\Facades\Route; use Illuminate\Support\Str; @@ -40,11 +40,11 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li // Access $this->crud->allowAccess($operationName); - BackpackHooks::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () use ($operationName) { + OperationHook::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () use ($operationName) { return config()->has('backpack.operations.'.$operationName) ? 'backpack.operations.'.$operationName : 'backpack.operations.form'; }); - BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, function () use ($operationName) { + OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, function () use ($operationName) { $this->crud->addSaveAction([ 'name' => 'save_and_back', 'visible' => function ($crud) use ($operationName) { @@ -57,7 +57,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li ]); }); - BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, ['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) { + OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, ['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) { $this->crud->button($operationName)->view('crud::buttons.quick')->stack($buttonStack)->meta($buttonMeta); }); } diff --git a/src/app/Http/Controllers/Operations/CreateOperation.php b/src/app/Http/Controllers/Operations/CreateOperation.php index 3ea6c5f142..774d72e273 100644 --- a/src/app/Http/Controllers/Operations/CreateOperation.php +++ b/src/app/Http/Controllers/Operations/CreateOperation.php @@ -2,7 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\OperationHook; use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; use Illuminate\Support\Facades\Route; @@ -37,11 +37,11 @@ protected function setupCreateDefaults() { $this->crud->allowAccess('create'); - BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, 'create', function () { + OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, 'create', function () { $this->crud->setupDefaultSaveActions(); }); - BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, 'list', function () { + OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, 'list', function () { $this->crud->addButton('top', 'create', 'view', 'crud::buttons.create'); }); } diff --git a/src/app/Library/CrudPanel/Hooks/BackpackHooks.php b/src/app/Library/CrudPanel/Hooks/BackpackHooks.php deleted file mode 100644 index ef5163f0de..0000000000 --- a/src/app/Library/CrudPanel/Hooks/BackpackHooks.php +++ /dev/null @@ -1,29 +0,0 @@ -hooks[$operation][$hook] = $callback; + } + } + + public function run(string $hook, string|array $operations, array $parameters): void + { + $operations = is_array($operations) ? $operations : [$operations]; + foreach ($operations as $operation) { + if (isset($this->hooks[$operation][$hook])) { + $this->hooks[$operation][$hook](...$parameters); + } + } + } + + public function has(string $hook, string $operation): bool + { + return isset($this->hooks[$operation][$hook]); + } } diff --git a/src/app/Library/CrudPanel/Traits/Operations.php b/src/app/Library/CrudPanel/Traits/Operations.php index dd329eceaa..10fc6b75ed 100644 --- a/src/app/Library/CrudPanel/Traits/Operations.php +++ b/src/app/Library/CrudPanel/Traits/Operations.php @@ -2,7 +2,7 @@ namespace Backpack\CRUD\app\Library\CrudPanel\Traits; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\OperationHook; use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; trait Operations @@ -63,11 +63,11 @@ public function setCurrentOperation($operation_name) * @param bool|\Closure $closure Code that calls CrudPanel methods. * @return void * - * @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead + * @deprecated use OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead */ public function operation($operations, $closure = false) { - BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operations, $closure); + OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, $operations, $closure); } /** @@ -79,18 +79,13 @@ public function operation($operations, $closure = false) * @param bool|\Closure $closure Code that calls CrudPanel methods. * @return void * - * @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead + * @deprecated use OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead */ public function configureOperation($operations, $closure = false) { $operations = (array) $operations; - foreach ($operations as $operation) { - $configuration = (array) $this->get($operation.'.configuration'); - $configuration[] = $closure; - - $this->set($operation.'.configuration', $configuration); - } + OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, $operations, $closure); } /** @@ -101,23 +96,12 @@ public function configureOperation($operations, $closure = false) * @param string|array $operations [description] * @return void * - * @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead + * @deprecated use OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead */ public function applyConfigurationFromSettings($operations) { $operations = (array) $operations; - foreach ($operations as $operation) { - $configuration = (array) $this->get($operation.'.configuration'); - - if (count($configuration)) { - foreach ($configuration as $closure) { - if (is_callable($closure)) { - // apply the closure - ($closure)(); - } - } - } - } + OperationHook::run(OperationHooks::BEFORE_OPERATION_SETUP, $operations, []); } } From f0ffb526bbe6fb12008151fd3e40ded391cebfcd Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 10 Oct 2024 12:53:49 +0000 Subject: [PATCH 04/13] Apply fixes from StyleCI [ci skip] [skip ci] --- src/app/Library/CrudPanel/Hooks/Contracts/OperationHook.php | 4 ++-- src/app/Library/CrudPanel/Hooks/OperationHooks.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/Library/CrudPanel/Hooks/Contracts/OperationHook.php b/src/app/Library/CrudPanel/Hooks/Contracts/OperationHook.php index 94ec1682d7..b24067addc 100644 --- a/src/app/Library/CrudPanel/Hooks/Contracts/OperationHook.php +++ b/src/app/Library/CrudPanel/Hooks/Contracts/OperationHook.php @@ -8,7 +8,7 @@ * @method static void register(string $hook, string|array $operations, callable $callback) * @method static void run(string $hook, string|array $operations, array $parameters) * @method static bool has(string $hook, string $operation) - * + * * @see \Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks */ class OperationHook extends Facade @@ -17,4 +17,4 @@ protected static function getFacadeAccessor() { return 'operation-hook'; } -} \ No newline at end of file +} diff --git a/src/app/Library/CrudPanel/Hooks/OperationHooks.php b/src/app/Library/CrudPanel/Hooks/OperationHooks.php index cf23019392..0381538d8d 100644 --- a/src/app/Library/CrudPanel/Hooks/OperationHooks.php +++ b/src/app/Library/CrudPanel/Hooks/OperationHooks.php @@ -16,7 +16,7 @@ final class OperationHooks public array $hooks = []; - public function register(string $hook, string|array $operations, callable $callback): void + public function register(string $hook, string|array $operations, callable $callback): void { $operations = is_array($operations) ? $operations : [$operations]; From 662fbb9079d2a1c94f6022009ce1fab8d5e46c12 Mon Sep 17 00:00:00 2001 From: pxpm Date: Thu, 10 Oct 2024 14:06:19 +0100 Subject: [PATCH 05/13] panel hooks --- src/BackpackServiceProvider.php | 4 +++ src/app/Http/Controllers/CrudController.php | 10 +++++- .../CrudPanel/Hooks/Contracts/PanelHook.php | 20 +++++++++++ .../CrudPanel/Hooks/OperationHooks.php | 12 +++---- .../Library/CrudPanel/Hooks/PanelHooks.php | 34 +++++++++++++++++++ 5 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php create mode 100644 src/app/Library/CrudPanel/Hooks/PanelHooks.php diff --git a/src/BackpackServiceProvider.php b/src/BackpackServiceProvider.php index 798210d6eb..1e97b800ec 100644 --- a/src/BackpackServiceProvider.php +++ b/src/BackpackServiceProvider.php @@ -95,6 +95,10 @@ public function register() return new \Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks(); }); + $this->app->scoped('panel-hook', function ($app) { + return new \Backpack\CRUD\app\Library\CrudPanel\Hooks\PanelHooks(); + }); + $this->app->singleton('BackpackViewNamespaces', function ($app) { return new ViewNamespaces(); }); diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index ca243473c5..a24f343f56 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -4,7 +4,9 @@ use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime; use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\OperationHook; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\PanelHook; use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\PanelHooks; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller; @@ -41,9 +43,15 @@ public function __construct() $this->crud = app('crud'); $this->crud->setRequest($request); - + + PanelHook::run(PanelHooks::BEFORE_SETUP_DEFAULTS, [$this]); $this->setupDefaults(); + PanelHook::run(PanelHooks::AFTER_SETUP_DEFAULTS, [$this]); + + PanelHook::run(PanelHooks::BEFORE_CONTROLLER_SETUP, [$this]); $this->setup(); + PanelHook::run(PanelHooks::AFTER_CONTROLLER_SETUP, [$this]); + $this->setupConfigurationForCurrentOperation(); return $next($request); diff --git a/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php b/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php new file mode 100644 index 0000000000..cb8398fb61 --- /dev/null +++ b/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php @@ -0,0 +1,20 @@ +hooks[$operation][$hook] = $callback; + $this->hooks[$operation][$hook][] = $callback; } } @@ -30,7 +24,9 @@ public function run(string $hook, string|array $operations, array $parameters): $operations = is_array($operations) ? $operations : [$operations]; foreach ($operations as $operation) { if (isset($this->hooks[$operation][$hook])) { - $this->hooks[$operation][$hook](...$parameters); + foreach ($this->hooks[$operation][$hook] as $callback) { + $callback(...$parameters); + } } } } diff --git a/src/app/Library/CrudPanel/Hooks/PanelHooks.php b/src/app/Library/CrudPanel/Hooks/PanelHooks.php new file mode 100644 index 0000000000..30126c36d5 --- /dev/null +++ b/src/app/Library/CrudPanel/Hooks/PanelHooks.php @@ -0,0 +1,34 @@ +hooks[$hook][] = $callback; + } + + public function run(string $hook, array $parameters): void + { + if (isset($this->hooks[$hook])) { + foreach ($this->hooks[$hook] as $callback) { + $callback(...$parameters); + } + } + } + + public function has(string $hook): bool + { + return isset($this->hooks[$hook]); + } +} From da886ea97d362c2f09a086c97bc53b681ef77e74 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 10 Oct 2024 13:06:35 +0000 Subject: [PATCH 06/13] Apply fixes from StyleCI [ci skip] [skip ci] --- src/app/Http/Controllers/CrudController.php | 2 +- src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php | 4 ++-- src/app/Library/CrudPanel/Hooks/PanelHooks.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index a24f343f56..244625442d 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -43,7 +43,7 @@ public function __construct() $this->crud = app('crud'); $this->crud->setRequest($request); - + PanelHook::run(PanelHooks::BEFORE_SETUP_DEFAULTS, [$this]); $this->setupDefaults(); PanelHook::run(PanelHooks::AFTER_SETUP_DEFAULTS, [$this]); diff --git a/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php b/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php index cb8398fb61..aeaf581dbb 100644 --- a/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php +++ b/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php @@ -8,7 +8,7 @@ * @method static void register(string $hook, callable $callback) * @method static void run(string $hook, array $parameters) * @method static bool has(string $hook) - * + * * @see \Backpack\CRUD\app\Library\CrudPanel\Hooks\PanelHooks */ class PanelHook extends Facade @@ -17,4 +17,4 @@ protected static function getFacadeAccessor() { return 'panel-hook'; } -} \ No newline at end of file +} diff --git a/src/app/Library/CrudPanel/Hooks/PanelHooks.php b/src/app/Library/CrudPanel/Hooks/PanelHooks.php index 30126c36d5..b85d54a3e4 100644 --- a/src/app/Library/CrudPanel/Hooks/PanelHooks.php +++ b/src/app/Library/CrudPanel/Hooks/PanelHooks.php @@ -13,7 +13,7 @@ final class PanelHooks public array $hooks = []; - public function register(string $hook, callable $callback): void + public function register(string $hook, callable $callback): void { $this->hooks[$hook][] = $callback; } From ab644a7880d312d6e2db54fe0c02d48ce03e7598 Mon Sep 17 00:00:00 2001 From: pxpm Date: Fri, 25 Oct 2024 11:54:01 +0100 Subject: [PATCH 07/13] refactoring :broom --- src/BackpackServiceProvider.php | 18 ++++----- src/app/Http/Controllers/CrudController.php | 24 ++++++------ .../Operations/CreateOperation.php | 15 ++++---- .../Operations/DeleteOperation.php | 11 ++---- .../CrudPanel/Hooks/Contracts/PanelHook.php | 20 ---------- .../LifecycleHook.php} | 6 +-- .../CrudPanel/Hooks/LifecycleHooks.php | 33 ++++++++++++++++ .../CrudPanel/Hooks/OperationHooks.php | 38 ------------------- .../Library/CrudPanel/Hooks/PanelHooks.php | 34 ----------------- 9 files changed, 65 insertions(+), 134 deletions(-) delete mode 100644 src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php rename src/app/Library/CrudPanel/Hooks/{Contracts/OperationHook.php => Facades/LifecycleHook.php} (76%) create mode 100644 src/app/Library/CrudPanel/Hooks/LifecycleHooks.php delete mode 100644 src/app/Library/CrudPanel/Hooks/OperationHooks.php delete mode 100644 src/app/Library/CrudPanel/Hooks/PanelHooks.php diff --git a/src/BackpackServiceProvider.php b/src/BackpackServiceProvider.php index 1e97b800ec..d3ad5a1dfe 100644 --- a/src/BackpackServiceProvider.php +++ b/src/BackpackServiceProvider.php @@ -91,12 +91,8 @@ public function register() return new DatabaseSchema(); }); - $this->app->scoped('operation-hook', function ($app) { - return new \Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks(); - }); - - $this->app->scoped('panel-hook', function ($app) { - return new \Backpack\CRUD\app\Library\CrudPanel\Hooks\PanelHooks(); + $this->app->scoped('lifecycle-hook', function ($app) { + return new app\Library\CrudPanel\Hooks\LifecycleHooks(); }); $this->app->singleton('BackpackViewNamespaces', function ($app) { @@ -262,7 +258,7 @@ public function loadConfigs() // add the root disk to filesystem configuration app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [ 'driver' => 'local', - 'root' => base_path(), + 'root' => base_path(), ]; /* @@ -281,7 +277,7 @@ public function loadConfigs() [ 'backpack' => [ 'driver' => 'eloquent', - 'model' => config('backpack.base.user_model_fqn'), + 'model' => config('backpack.base.user_model_fqn'), ], ]; @@ -299,8 +295,8 @@ public function loadConfigs() [ 'backpack' => [ 'provider' => 'backpack', - 'table' => $backpackPasswordBrokerTable, - 'expire' => config('backpack.base.password_recovery_token_expiration', 60), + 'table' => $backpackPasswordBrokerTable, + 'expire' => config('backpack.base.password_recovery_token_expiration', 60), 'throttle' => config('backpack.base.password_recovery_throttle_notifications'), ], ]; @@ -309,7 +305,7 @@ public function loadConfigs() app()->config['auth.guards'] = app()->config['auth.guards'] + [ 'backpack' => [ - 'driver' => 'session', + 'driver' => 'session', 'provider' => 'backpack', ], ]; diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index 244625442d..5ce760f8a2 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -3,10 +3,7 @@ namespace Backpack\CRUD\app\Http\Controllers; use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\OperationHook; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\PanelHook; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\PanelHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facadees\LifecycleHook; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller; @@ -23,6 +20,7 @@ class CrudController extends Controller use DispatchesJobs, ValidatesRequests; public $crud; + public $data = []; public function __construct() @@ -44,13 +42,13 @@ public function __construct() $this->crud->setRequest($request); - PanelHook::run(PanelHooks::BEFORE_SETUP_DEFAULTS, [$this]); + LifecycleHook::trigger('crud:before_setup_defaults', [$this]); $this->setupDefaults(); - PanelHook::run(PanelHooks::AFTER_SETUP_DEFAULTS, [$this]); + LifecycleHook::trigger('crud:after_setup_defaults', [$this]); - PanelHook::run(PanelHooks::BEFORE_CONTROLLER_SETUP, [$this]); + LifecycleHook::trigger('crud:before_setup', [$this]); $this->setup(); - PanelHook::run(PanelHooks::AFTER_CONTROLLER_SETUP, [$this]); + LifecycleHook::trigger('crud:after_setup', [$this]); $this->setupConfigurationForCurrentOperation(); @@ -126,15 +124,15 @@ protected function setupConfigurationForCurrentOperation() * 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 (! OperationHook::has(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName)) { - OperationHook::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () use ($operationName) { + if (! LifecycleHook::has($operationName.':setup_operation_config')) { + LifecycleHook::hookInto($operationName.':setup_operation_config', function () use ($operationName) { return 'backpack.operations.'.$operationName; }); } - $this->crud->loadDefaultOperationSettingsFromConfig(OperationHook::run(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, [$this])); + $this->crud->loadDefaultOperationSettingsFromConfig(LifecycleHook::trigger($operationName.':setup_operation_config', [$this, $operationName])); - OperationHook::run(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, [$this]); + LifecycleHook::trigger($operationName.':before_setup', [$this]); /* * THEN, run the corresponding setupXxxOperation if it exists. */ @@ -142,6 +140,6 @@ protected function setupConfigurationForCurrentOperation() $this->{$setupClassName}(); } - OperationHook::run(OperationHooks::AFTER_OPERATION_SETUP, $operationName, [$this]); + LifecycleHook::trigger($operationName.':after_setup'[$this]); } } diff --git a/src/app/Http/Controllers/Operations/CreateOperation.php b/src/app/Http/Controllers/Operations/CreateOperation.php index 774d72e273..44dc0b007f 100644 --- a/src/app/Http/Controllers/Operations/CreateOperation.php +++ b/src/app/Http/Controllers/Operations/CreateOperation.php @@ -2,8 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\OperationHook; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facadees\LifecycleHook; use Illuminate\Support\Facades\Route; trait CreateOperation @@ -18,14 +17,14 @@ trait CreateOperation protected function setupCreateRoutes($segment, $routeName, $controller) { Route::get($segment.'/create', [ - 'as' => $routeName.'.create', - 'uses' => $controller.'@create', + 'as' => $routeName.'.create', + 'uses' => $controller.'@create', 'operation' => 'create', ]); Route::post($segment, [ - 'as' => $routeName.'.store', - 'uses' => $controller.'@store', + 'as' => $routeName.'.store', + 'uses' => $controller.'@store', 'operation' => 'create', ]); } @@ -37,11 +36,11 @@ protected function setupCreateDefaults() { $this->crud->allowAccess('create'); - OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, 'create', function () { + LifecycleHook::hookInto('create:before_setup', function () { $this->crud->setupDefaultSaveActions(); }); - OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, 'list', function () { + LifecycleHook::hookInto('list:before_setup', function () { $this->crud->addButton('top', 'create', 'view', 'crud::buttons.create'); }); } diff --git a/src/app/Http/Controllers/Operations/DeleteOperation.php b/src/app/Http/Controllers/Operations/DeleteOperation.php index 81a80199fa..6c422f8a2a 100644 --- a/src/app/Http/Controllers/Operations/DeleteOperation.php +++ b/src/app/Http/Controllers/Operations/DeleteOperation.php @@ -2,6 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facadees\LifecycleHook; use Illuminate\Support\Facades\Route; trait DeleteOperation @@ -16,8 +17,8 @@ trait DeleteOperation protected function setupDeleteRoutes($segment, $routeName, $controller) { Route::delete($segment.'/{id}', [ - 'as' => $routeName.'.destroy', - 'uses' => $controller.'@destroy', + 'as' => $routeName.'.destroy', + 'uses' => $controller.'@destroy', 'operation' => 'delete', ]); } @@ -29,11 +30,7 @@ protected function setupDeleteDefaults() { $this->crud->allowAccess('delete'); - $this->crud->operation('delete', function () { - $this->crud->loadDefaultOperationSettingsFromConfig(); - }); - - $this->crud->operation(['list', 'show'], function () { + LifecycleHook::hookInto(['list:before_setup', 'show:before_setup'], function () { $this->crud->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end'); }); } diff --git a/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php b/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php deleted file mode 100644 index aeaf581dbb..0000000000 --- a/src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php +++ /dev/null @@ -1,20 +0,0 @@ -hooks[$hook][] = $callback; + } + } + + public function trigger(string|array $hooks, array $parameters): void + { + $hooks = is_array($hooks) ? $hooks : [$hooks]; + foreach ($hooks as $hook) { + if (isset($this->hooks[$hook])) { + foreach ($this->hooks[$hook] as $callback) { + $callback(...$parameters); + } + } + } + } + + public function has(string $hook): bool + { + return isset($this->hooks[$hook]); + } +} diff --git a/src/app/Library/CrudPanel/Hooks/OperationHooks.php b/src/app/Library/CrudPanel/Hooks/OperationHooks.php deleted file mode 100644 index 78bbcdf4e7..0000000000 --- a/src/app/Library/CrudPanel/Hooks/OperationHooks.php +++ /dev/null @@ -1,38 +0,0 @@ -hooks[$operation][$hook][] = $callback; - } - } - - public function run(string $hook, string|array $operations, array $parameters): void - { - $operations = is_array($operations) ? $operations : [$operations]; - foreach ($operations as $operation) { - if (isset($this->hooks[$operation][$hook])) { - foreach ($this->hooks[$operation][$hook] as $callback) { - $callback(...$parameters); - } - } - } - } - - public function has(string $hook, string $operation): bool - { - return isset($this->hooks[$operation][$hook]); - } -} diff --git a/src/app/Library/CrudPanel/Hooks/PanelHooks.php b/src/app/Library/CrudPanel/Hooks/PanelHooks.php deleted file mode 100644 index b85d54a3e4..0000000000 --- a/src/app/Library/CrudPanel/Hooks/PanelHooks.php +++ /dev/null @@ -1,34 +0,0 @@ -hooks[$hook][] = $callback; - } - - public function run(string $hook, array $parameters): void - { - if (isset($this->hooks[$hook])) { - foreach ($this->hooks[$hook] as $callback) { - $callback(...$parameters); - } - } - } - - public function has(string $hook): bool - { - return isset($this->hooks[$hook]); - } -} From 6a167e93009f392e6d7ccc22e9421d1fa25a0501 Mon Sep 17 00:00:00 2001 From: pxpm Date: Fri, 25 Oct 2024 15:20:36 +0100 Subject: [PATCH 08/13] finish implementing hooks --- src/app/Http/Controllers/CrudController.php | 4 +-- .../Operations/Concerns/HasForm.php | 19 +++++++------- .../Operations/CreateOperation.php | 2 +- .../Operations/DeleteOperation.php | 2 +- .../CrudPanel/Hooks/Facades/LifecycleHook.php | 2 +- .../Library/CrudPanel/Traits/Operations.php | 26 ++++++++++++------- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index 5ce760f8a2..c331be9a36 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -3,7 +3,7 @@ namespace Backpack\CRUD\app\Http\Controllers; use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facadees\LifecycleHook; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller; @@ -140,6 +140,6 @@ protected function setupConfigurationForCurrentOperation() $this->{$setupClassName}(); } - LifecycleHook::trigger($operationName.':after_setup'[$this]); + LifecycleHook::trigger($operationName.':after_setup', [$this]); } } diff --git a/src/app/Http/Controllers/Operations/Concerns/HasForm.php b/src/app/Http/Controllers/Operations/Concerns/HasForm.php index 9f4a57f5d8..b2aa011f10 100644 --- a/src/app/Http/Controllers/Operations/Concerns/HasForm.php +++ b/src/app/Http/Controllers/Operations/Concerns/HasForm.php @@ -2,8 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations\Concerns; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\OperationHook; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; use Illuminate\Support\Facades\Route; use Illuminate\Support\Str; @@ -20,13 +19,13 @@ protected function formRoutes(string $operationName, bool $routesHaveIdSegment, $postFormMethod = 'post'.$operationName.'Form'; Route::get($segment.$secondSegment.$thirdSegment, [ - 'as' => $routeName.'.'.$getFormMethod, - 'uses' => $controller.'@'.$getFormMethod, + 'as' => $routeName.'.'.$getFormMethod, + 'uses' => $controller.'@'.$getFormMethod, 'operation' => $operationName, ]); Route::post($segment.$secondSegment.$thirdSegment, [ - 'as' => $routeName.'.'.$postFormMethod, - 'uses' => $controller.'@'.$postFormMethod, + 'as' => $routeName.'.'.$postFormMethod, + 'uses' => $controller.'@'.$postFormMethod, 'operation' => $operationName, ]); } @@ -40,13 +39,13 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li // Access $this->crud->allowAccess($operationName); - OperationHook::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () use ($operationName) { + LifecycleHook::hookInto('crud:setup_operation_config', function () use ($operationName) { return config()->has('backpack.operations.'.$operationName) ? 'backpack.operations.'.$operationName : 'backpack.operations.form'; }); - OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, function () use ($operationName) { + LifecycleHook::hookInto($operationName.':before_setup', function () use ($operationName) { $this->crud->addSaveAction([ - 'name' => 'save_and_back', + 'name' => 'save_and_back', 'visible' => function ($crud) use ($operationName) { return $crud->hasAccess($operationName); }, @@ -57,7 +56,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li ]); }); - OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, ['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) { + LifecycleHook::hookInto(['list:before_setup', 'show:before_setup'], function () use ($operationName, $buttonStack, $buttonMeta) { $this->crud->button($operationName)->view('crud::buttons.quick')->stack($buttonStack)->meta($buttonMeta); }); } diff --git a/src/app/Http/Controllers/Operations/CreateOperation.php b/src/app/Http/Controllers/Operations/CreateOperation.php index 44dc0b007f..a926f6c12b 100644 --- a/src/app/Http/Controllers/Operations/CreateOperation.php +++ b/src/app/Http/Controllers/Operations/CreateOperation.php @@ -2,7 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facadees\LifecycleHook; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; use Illuminate\Support\Facades\Route; trait CreateOperation diff --git a/src/app/Http/Controllers/Operations/DeleteOperation.php b/src/app/Http/Controllers/Operations/DeleteOperation.php index 6c422f8a2a..9a708de348 100644 --- a/src/app/Http/Controllers/Operations/DeleteOperation.php +++ b/src/app/Http/Controllers/Operations/DeleteOperation.php @@ -2,7 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facadees\LifecycleHook; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; use Illuminate\Support\Facades\Route; trait DeleteOperation diff --git a/src/app/Library/CrudPanel/Hooks/Facades/LifecycleHook.php b/src/app/Library/CrudPanel/Hooks/Facades/LifecycleHook.php index 81e92a651d..13a92422a1 100644 --- a/src/app/Library/CrudPanel/Hooks/Facades/LifecycleHook.php +++ b/src/app/Library/CrudPanel/Hooks/Facades/LifecycleHook.php @@ -1,6 +1,6 @@ Date: Fri, 25 Oct 2024 14:20:49 +0000 Subject: [PATCH 09/13] Apply fixes from StyleCI [ci skip] [skip ci] --- src/BackpackServiceProvider.php | 10 +++++----- .../Http/Controllers/Operations/Concerns/HasForm.php | 10 +++++----- .../Http/Controllers/Operations/CreateOperation.php | 8 ++++---- .../Http/Controllers/Operations/DeleteOperation.php | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/BackpackServiceProvider.php b/src/BackpackServiceProvider.php index d3ad5a1dfe..4b4131ec3e 100644 --- a/src/BackpackServiceProvider.php +++ b/src/BackpackServiceProvider.php @@ -258,7 +258,7 @@ public function loadConfigs() // add the root disk to filesystem configuration app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [ 'driver' => 'local', - 'root' => base_path(), + 'root' => base_path(), ]; /* @@ -277,7 +277,7 @@ public function loadConfigs() [ 'backpack' => [ 'driver' => 'eloquent', - 'model' => config('backpack.base.user_model_fqn'), + 'model' => config('backpack.base.user_model_fqn'), ], ]; @@ -295,8 +295,8 @@ public function loadConfigs() [ 'backpack' => [ 'provider' => 'backpack', - 'table' => $backpackPasswordBrokerTable, - 'expire' => config('backpack.base.password_recovery_token_expiration', 60), + 'table' => $backpackPasswordBrokerTable, + 'expire' => config('backpack.base.password_recovery_token_expiration', 60), 'throttle' => config('backpack.base.password_recovery_throttle_notifications'), ], ]; @@ -305,7 +305,7 @@ public function loadConfigs() app()->config['auth.guards'] = app()->config['auth.guards'] + [ 'backpack' => [ - 'driver' => 'session', + 'driver' => 'session', 'provider' => 'backpack', ], ]; diff --git a/src/app/Http/Controllers/Operations/Concerns/HasForm.php b/src/app/Http/Controllers/Operations/Concerns/HasForm.php index b2aa011f10..5775f4165c 100644 --- a/src/app/Http/Controllers/Operations/Concerns/HasForm.php +++ b/src/app/Http/Controllers/Operations/Concerns/HasForm.php @@ -19,13 +19,13 @@ protected function formRoutes(string $operationName, bool $routesHaveIdSegment, $postFormMethod = 'post'.$operationName.'Form'; Route::get($segment.$secondSegment.$thirdSegment, [ - 'as' => $routeName.'.'.$getFormMethod, - 'uses' => $controller.'@'.$getFormMethod, + 'as' => $routeName.'.'.$getFormMethod, + 'uses' => $controller.'@'.$getFormMethod, 'operation' => $operationName, ]); Route::post($segment.$secondSegment.$thirdSegment, [ - 'as' => $routeName.'.'.$postFormMethod, - 'uses' => $controller.'@'.$postFormMethod, + 'as' => $routeName.'.'.$postFormMethod, + 'uses' => $controller.'@'.$postFormMethod, 'operation' => $operationName, ]); } @@ -45,7 +45,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li LifecycleHook::hookInto($operationName.':before_setup', function () use ($operationName) { $this->crud->addSaveAction([ - 'name' => 'save_and_back', + 'name' => 'save_and_back', 'visible' => function ($crud) use ($operationName) { return $crud->hasAccess($operationName); }, diff --git a/src/app/Http/Controllers/Operations/CreateOperation.php b/src/app/Http/Controllers/Operations/CreateOperation.php index a926f6c12b..e6282ff68f 100644 --- a/src/app/Http/Controllers/Operations/CreateOperation.php +++ b/src/app/Http/Controllers/Operations/CreateOperation.php @@ -17,14 +17,14 @@ trait CreateOperation protected function setupCreateRoutes($segment, $routeName, $controller) { Route::get($segment.'/create', [ - 'as' => $routeName.'.create', - 'uses' => $controller.'@create', + 'as' => $routeName.'.create', + 'uses' => $controller.'@create', 'operation' => 'create', ]); Route::post($segment, [ - 'as' => $routeName.'.store', - 'uses' => $controller.'@store', + 'as' => $routeName.'.store', + 'uses' => $controller.'@store', 'operation' => 'create', ]); } diff --git a/src/app/Http/Controllers/Operations/DeleteOperation.php b/src/app/Http/Controllers/Operations/DeleteOperation.php index 9a708de348..c5a05bf2f5 100644 --- a/src/app/Http/Controllers/Operations/DeleteOperation.php +++ b/src/app/Http/Controllers/Operations/DeleteOperation.php @@ -17,8 +17,8 @@ trait DeleteOperation protected function setupDeleteRoutes($segment, $routeName, $controller) { Route::delete($segment.'/{id}', [ - 'as' => $routeName.'.destroy', - 'uses' => $controller.'@destroy', + 'as' => $routeName.'.destroy', + 'uses' => $controller.'@destroy', 'operation' => 'delete', ]); } From b1d8a16fdca535074ac3d77b92e54970e867db38 Mon Sep 17 00:00:00 2001 From: pxpm Date: Tue, 5 Nov 2024 15:42:09 +0000 Subject: [PATCH 10/13] refactoring hooks --- src/app/Http/Controllers/CrudController.php | 11 +++-------- .../Controllers/Operations/Concerns/HasForm.php | 15 ++++++++++----- .../Http/Controllers/Operations/ListOperation.php | 3 ++- .../Controllers/Operations/ReorderOperation.php | 5 +++-- .../Http/Controllers/Operations/ShowOperation.php | 7 ++++--- .../Controllers/Operations/UpdateOperation.php | 5 +++-- src/app/Library/CrudPanel/CrudRouter.php | 6 ++++++ .../Library/CrudPanel/Hooks/LifecycleHooks.php | 4 +++- src/app/Library/CrudPanel/Traits/Operations.php | 3 +-- 9 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index c331be9a36..92422c3a34 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -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. */ diff --git a/src/app/Http/Controllers/Operations/Concerns/HasForm.php b/src/app/Http/Controllers/Operations/Concerns/HasForm.php index 5775f4165c..7cf6a4ef94 100644 --- a/src/app/Http/Controllers/Operations/Concerns/HasForm.php +++ b/src/app/Http/Controllers/Operations/Concerns/HasForm.php @@ -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) { diff --git a/src/app/Http/Controllers/Operations/ListOperation.php b/src/app/Http/Controllers/Operations/ListOperation.php index f9ea830507..11b678b95d 100644 --- a/src/app/Http/Controllers/Operations/ListOperation.php +++ b/src/app/Http/Controllers/Operations/ListOperation.php @@ -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 @@ -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(); }); } diff --git a/src/app/Http/Controllers/Operations/ReorderOperation.php b/src/app/Http/Controllers/Operations/ReorderOperation.php index 0fe547f78a..90fcee0efd 100644 --- a/src/app/Http/Controllers/Operations/ReorderOperation.php +++ b/src/app/Http/Controllers/Operations/ReorderOperation.php @@ -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 @@ -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', @@ -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'); }); } diff --git a/src/app/Http/Controllers/Operations/ShowOperation.php b/src/app/Http/Controllers/Operations/ShowOperation.php index d2eedc72d8..e1ea3ab53b 100644 --- a/src/app/Http/Controllers/Operations/ShowOperation.php +++ b/src/app/Http/Controllers/Operations/ShowOperation.php @@ -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 @@ -30,7 +31,7 @@ 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')) { @@ -38,11 +39,11 @@ protected function setupShowDefaults() } }); - $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) { diff --git a/src/app/Http/Controllers/Operations/UpdateOperation.php b/src/app/Http/Controllers/Operations/UpdateOperation.php index f3c622a9c5..f0c656092c 100644 --- a/src/app/Http/Controllers/Operations/UpdateOperation.php +++ b/src/app/Http/Controllers/Operations/UpdateOperation.php @@ -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 @@ -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()) { @@ -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'); }); } diff --git a/src/app/Library/CrudPanel/CrudRouter.php b/src/app/Library/CrudPanel/CrudRouter.php index 0c1c1f464c..83a490128a 100644 --- a/src/app/Library/CrudPanel/CrudRouter.php +++ b/src/app/Library/CrudPanel/CrudRouter.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\App; use ReflectionClass; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; final class CrudRouter { @@ -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; } @@ -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]); + } } } diff --git a/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php b/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php index 3ee79c2940..f9a100cdfa 100644 --- a/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php +++ b/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php @@ -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); + } } } } diff --git a/src/app/Library/CrudPanel/Traits/Operations.php b/src/app/Library/CrudPanel/Traits/Operations.php index 2786037f26..9362fc3215 100644 --- a/src/app/Library/CrudPanel/Traits/Operations.php +++ b/src/app/Library/CrudPanel/Traits/Operations.php @@ -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) { From 0ca7b97929208cc93c45f53ade3460fae2147ddf Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 5 Nov 2024 15:42:22 +0000 Subject: [PATCH 11/13] Apply fixes from StyleCI [ci skip] [skip ci] --- src/app/Http/Controllers/CrudController.php | 4 ++-- src/app/Http/Controllers/Operations/Concerns/HasForm.php | 2 +- src/app/Library/CrudPanel/CrudRouter.php | 3 +-- src/app/Library/CrudPanel/Hooks/LifecycleHooks.php | 2 +- src/app/Library/CrudPanel/Traits/Operations.php | 3 +-- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index 92422c3a34..180c6f7837 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -124,9 +124,9 @@ protected function setupConfigurationForCurrentOperation() * 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; */ - LifecycleHook::trigger($operationName.':before_setup', [$this]); + LifecycleHook::trigger($operationName.':before_setup', [$this]); - $this->crud->applyConfigurationFromSettings($operationName); + $this->crud->applyConfigurationFromSettings($operationName); /* * THEN, run the corresponding setupXxxOperation if it exists. diff --git a/src/app/Http/Controllers/Operations/Concerns/HasForm.php b/src/app/Http/Controllers/Operations/Concerns/HasForm.php index 7cf6a4ef94..10a202a963 100644 --- a/src/app/Http/Controllers/Operations/Concerns/HasForm.php +++ b/src/app/Http/Controllers/Operations/Concerns/HasForm.php @@ -39,7 +39,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li // Access $this->crud->allowAccess($operationName); - 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)) { diff --git a/src/app/Library/CrudPanel/CrudRouter.php b/src/app/Library/CrudPanel/CrudRouter.php index 83a490128a..fd52ee5cd8 100644 --- a/src/app/Library/CrudPanel/CrudRouter.php +++ b/src/app/Library/CrudPanel/CrudRouter.php @@ -2,9 +2,9 @@ namespace Backpack\CRUD\app\Library\CrudPanel; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; use Illuminate\Support\Facades\App; use ReflectionClass; -use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; final class CrudRouter { @@ -38,7 +38,6 @@ public static function setupControllerRoutes(string $name, string $routeName, st LifecycleHook::trigger('crud:before_setup_routes', [$name, $routeName, $controller]); $method->invoke($controllerInstance, $name, $routeName, $controller); LifecycleHook::trigger('crud:after_setup_routes', [$name, $routeName, $controller]); - } } } diff --git a/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php b/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php index f9a100cdfa..49d3b616b3 100644 --- a/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php +++ b/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php @@ -20,7 +20,7 @@ public function trigger(string|array $hooks, array $parameters): void foreach ($hooks as $hook) { if (isset($this->hooks[$hook])) { foreach ($this->hooks[$hook] as $callback) { - if($callback instanceof \Closure) { + if ($callback instanceof \Closure) { $callback(...$parameters); } } diff --git a/src/app/Library/CrudPanel/Traits/Operations.php b/src/app/Library/CrudPanel/Traits/Operations.php index 9362fc3215..373b3ea0fe 100644 --- a/src/app/Library/CrudPanel/Traits/Operations.php +++ b/src/app/Library/CrudPanel/Traits/Operations.php @@ -97,9 +97,8 @@ public function configureOperation($operations, $closure = false) * This is called when an operation does setCurrentOperation(). * * - * @param string|array $operations + * @param string|array $operations * @return void - * */ public function applyConfigurationFromSettings($operations) { From eac7c45f96007b338ecd6bd8cf7afa3231526730 Mon Sep 17 00:00:00 2001 From: pxpm Date: Tue, 5 Nov 2024 15:43:40 +0000 Subject: [PATCH 12/13] add hook to delete --- src/app/Http/Controllers/Operations/DeleteOperation.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/app/Http/Controllers/Operations/DeleteOperation.php b/src/app/Http/Controllers/Operations/DeleteOperation.php index c5a05bf2f5..8862227394 100644 --- a/src/app/Http/Controllers/Operations/DeleteOperation.php +++ b/src/app/Http/Controllers/Operations/DeleteOperation.php @@ -30,6 +30,10 @@ protected function setupDeleteDefaults() { $this->crud->allowAccess('delete'); + LifecycleHook::hookInto('delete:before_setup', function () { + $this->crud->loadDefaultOperationSettingsFromConfig(); + }); + LifecycleHook::hookInto(['list:before_setup', 'show:before_setup'], function () { $this->crud->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end'); }); From b4d2082ab5348aed90a18da53be6bbd5986637ae Mon Sep 17 00:00:00 2001 From: pxpm Date: Tue, 5 Nov 2024 15:51:04 +0000 Subject: [PATCH 13/13] allow empty parameters --- src/app/Library/CrudPanel/Hooks/LifecycleHooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php b/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php index 49d3b616b3..336e49e5e8 100644 --- a/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php +++ b/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php @@ -14,7 +14,7 @@ public function hookInto(string|array $hooks, callable $callback): void } } - public function trigger(string|array $hooks, array $parameters): void + public function trigger(string|array $hooks, array $parameters = []): void { $hooks = is_array($hooks) ? $hooks : [$hooks]; foreach ($hooks as $hook) {