Skip to content

Commit

Permalink
panel hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm committed Oct 25, 2024
1 parent f0ffb52 commit 662fbb9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/BackpackServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
10 changes: 9 additions & 1 deletion src/app/Http/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
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';
}
}
12 changes: 4 additions & 8 deletions src/app/Library/CrudPanel/Hooks/OperationHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

final class OperationHooks
{
const BEFORE_OPERATION_SETUP_ROUTES = 'beforeOperationSetupRoutes';
const AFTER_OPERATION_SETUP_ROUTES = 'afterOperationSetupRoutes';
const BEFORE_OPERATION_SETUP_DEFAULTS = 'beforeOperationSetupDefaults';
const AFTER_OPERATION_SETUP_DEFAULTS = 'afterOperationSetupDefaults';
const BEFORE_CONTROLLER_SETUP = 'beforeControllerSetup';
const AFTER_CONTROLLER_SETUP = 'afterControllerSetup';
const BEFORE_OPERATION_SETUP = 'beforeOperationSetup';
const AFTER_OPERATION_SETUP = 'afterOperationSetup';
const SETUP_OPERATION_FROM_CONFIG = 'setupOperationFromConfig';
Expand All @@ -21,7 +15,7 @@ public function register(string $hook, string|array $operations, callable $callb
$operations = is_array($operations) ? $operations : [$operations];

foreach ($operations as $operation) {
$this->hooks[$operation][$hook] = $callback;
$this->hooks[$operation][$hook][] = $callback;
}
}

Expand All @@ -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);
}
}
}
}
Expand Down
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]);
}
}

0 comments on commit 662fbb9

Please sign in to comment.