Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] Operation hooks #5687

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/BackpackServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ 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->singleton('BackpackViewNamespaces', function ($app) {
return new ViewNamespaces();
});
Expand Down
21 changes: 20 additions & 1 deletion src/app/Http/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
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 Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;
Expand Down Expand Up @@ -40,8 +44,14 @@ public function __construct()

$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);
Expand Down Expand Up @@ -116,13 +126,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 (! 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(OperationHook::run(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, [$this]));

OperationHook::run(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, [$this]);
/*
* THEN, run the corresponding setupXxxOperation if it exists.
*/
if (method_exists($this, $setupClassName)) {
$this->{$setupClassName}();
}

OperationHook::run(OperationHooks::AFTER_OPERATION_SETUP, $operationName, [$this]);
}
}
21 changes: 8 additions & 13 deletions src/app/Http/Controllers/Operations/Concerns/HasForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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 Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -38,17 +40,11 @@ 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
OperationHook::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, 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) {
$this->crud->addSaveAction([
'name' => 'save_and_back',
'visible' => function ($crud) use ($operationName) {
Expand All @@ -61,8 +57,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li
]);
});

// Default Button
$this->crud->operation(['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);
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/app/Http/Controllers/Operations/CreateOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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 Illuminate\Support\Facades\Route;

trait CreateOperation
Expand Down Expand Up @@ -35,12 +37,11 @@ protected function setupCreateDefaults()
{
$this->crud->allowAccess('create');

$this->crud->operation('create', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();
OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, 'create', function () {
$this->crud->setupDefaultSaveActions();
});

$this->crud->operation('list', function () {
OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, 'list', function () {
$this->crud->addButton('top', 'create', 'view', 'crud::buttons.create');
});
}
Expand Down
20 changes: 20 additions & 0 deletions src/app/Library/CrudPanel/Hooks/Contracts/OperationHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts;

use Illuminate\Support\Facades\Facade;

/**
* @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
{
protected static function getFacadeAccessor()
{
return 'operation-hook';
}
}
20 changes: 20 additions & 0 deletions src/app/Library/CrudPanel/Hooks/Contracts/PanelHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts;

use Illuminate\Support\Facades\Facade;

/**
* @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
{
protected static function getFacadeAccessor()
{
return 'panel-hook';
}
}
38 changes: 38 additions & 0 deletions src/app/Library/CrudPanel/Hooks/OperationHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Backpack\CRUD\app\Library\CrudPanel\Hooks;

final class OperationHooks
{
const BEFORE_OPERATION_SETUP = 'beforeOperationSetup';
const AFTER_OPERATION_SETUP = 'afterOperationSetup';
const SETUP_OPERATION_FROM_CONFIG = 'setupOperationFromConfig';

public array $hooks = [];

public function register(string $hook, string|array $operations, callable $callback): void
{
$operations = is_array($operations) ? $operations : [$operations];

foreach ($operations as $operation) {
$this->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]);
}
}
34 changes: 34 additions & 0 deletions src/app/Library/CrudPanel/Hooks/PanelHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Backpack\CRUD\app\Library\CrudPanel\Hooks;

final class PanelHooks
{
const BEFORE_SETUP_ROUTES = 'beforeSetupRoutes';
const AFTER_SETUP_ROUTES = 'afterSetupRoutes';
const BEFORE_SETUP_DEFAULTS = 'beforeSetupDefaults';
const AFTER_SETUP_DEFAULTS = 'afterSetupDefaults';
const BEFORE_CONTROLLER_SETUP = 'beforeControllerSetup';
const AFTER_CONTROLLER_SETUP = 'afterControllerSetup';

public array $hooks = [];

public function register(string $hook, callable $callback): void
{
$this->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]);
}
}
33 changes: 13 additions & 20 deletions src/app/Library/CrudPanel/Traits/Operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Backpack\CRUD\app\Library\CrudPanel\Traits;

use Backpack\CRUD\app\Library\CrudPanel\Hooks\Contracts\OperationHook;
use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks;

trait Operations
{
/*
Expand Down Expand Up @@ -59,31 +62,30 @@ 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 OperationHook::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead
*/
public function operation($operations, $closure = false)
{
return $this->configureOperation($operations, $closure);
OperationHook::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 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);
}

/**
Expand All @@ -93,22 +95,13 @@ public function configureOperation($operations, $closure = false)
*
* @param string|array $operations [description]
* @return void
*
* @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, []);
}
}