-
Notifications
You must be signed in to change notification settings - Fork 922
[POC] datatable as a component for multiple cruds #5688
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
Merged
Merged
Changes from 47 commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
831be33
wip
pxpm 3a61e54
wip
pxpm f8fec3f
Apply fixes from StyleCI
StyleCIBot c222441
Apply fixes from StyleCI
StyleCIBot 5e59690
wip
pxpm 792da51
wip
pxpm dd89d3d
wip
pxpm d8b8d16
Apply fixes from StyleCI
StyleCIBot f9b3117
wip
pxpm e6490ab
wip
pxpm 2e58485
wip
pxpm f1fdd91
wip
pxpm 693849b
Apply fixes from StyleCI
StyleCIBot 861be11
wip
pxpm 7357fc2
wip
pxpm 74c0bab
wip
pxpm 3ffacd9
Apply fixes from StyleCI
StyleCIBot 60dbf9d
wip
pxpm 863f179
Apply fixes from StyleCI
StyleCIBot 49cc789
wip
pxpm 868c74c
Apply fixes from StyleCI
StyleCIBot b255686
wip
pxpm 6a92d63
wip
pxpm 9760254
Apply fixes from StyleCI
StyleCIBot b246d64
wip
pxpm 641ce12
wip
pxpm e94f20e
wip
pxpm 5a71915
fix responsive table
pxpm f4a0aea
wip
pxpm 7cc007d
wip
pxpm e3b5759
fix pagination
pxpm 878ac24
wip
pxpm dae5da8
wip
pxpm 2d583e8
Apply fixes from StyleCI
StyleCIBot f66a1f1
wip
pxpm 9806d93
wip
pxpm a83d5b6
Apply fixes from StyleCI
StyleCIBot 7f00a36
wip
pxpm cc93c39
Apply fixes from StyleCI
StyleCIBot b8f3157
wip
pxpm c12786c
Apply fixes from StyleCI
StyleCIBot e77738c
wip
pxpm 70d6d31
Apply fixes from StyleCI
StyleCIBot 801e5e9
add explanatory comments to CrudPanelManager class
tabacitu 277721f
Apply fixes from StyleCI
StyleCIBot 9bb9011
rename datatables_logic to datatable_logic
tabacitu 262a1f0
Merge branch 'datatable-single-component' of https://github.com/Larav…
tabacitu 1b6ba20
fix
pxpm 0a47c05
Apply fixes from StyleCI
StyleCIBot 3b89f24
wip
pxpm bd958bb
wip
pxpm 762f513
Apply fixes from StyleCI
StyleCIBot a974b45
Create setup cache class and create the datatable cache class (#5807)
pxpm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace Backpack\CRUD; | ||
|
|
||
| use Illuminate\Support\Facades\Facade; | ||
|
|
||
| /** | ||
| * @see CrudPanelManager | ||
| */ | ||
| class CrudManager extends Facade | ||
| { | ||
| protected static function getFacadeAccessor() | ||
| { | ||
| return 'CrudManager'; | ||
| } | ||
| } |
tabacitu marked this conversation as resolved.
Show resolved
Hide resolved
tabacitu marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| <?php | ||
|
|
||
| namespace Backpack\CRUD; | ||
|
|
||
| use Backpack\CRUD\app\Http\Controllers\Contracts\CrudControllerContract; | ||
| use Backpack\CRUD\app\Http\Controllers\CrudController; | ||
| use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; | ||
| use Illuminate\Support\Facades\Facade; | ||
|
|
||
| /** | ||
| * CrudPanelManager - Central registry and factory for CRUD panels. | ||
| * | ||
| * This class manages multiple CrudPanel instances across different controllers. | ||
| * It acts as a singleton registry that: | ||
| * - Creates and stores CrudPanel instances for each controller | ||
| * - Tracks which operations have been initialized for each controller | ||
| * - Manages the currently active controller context | ||
| * - Provides methods to retrieve the appropriate CrudPanel based on context | ||
| * | ||
| * This allows multiple CRUD controllers to coexist and share state properly | ||
| * within a single request lifecycle. | ||
| */ | ||
| final class CrudPanelManager | ||
| { | ||
| /** @var array<string, CrudPanel> Registry of CrudPanel instances indexed by controller class name */ | ||
| private array $cruds = []; | ||
|
|
||
| /** @var array<string, array<string>> Tracks which operations have been initialized for each controller */ | ||
| private array $initializedOperations = []; | ||
|
|
||
| /** @var string|null The currently active controller class name */ | ||
| private ?string $currentlyActiveCrudController = null; | ||
|
|
||
| /** | ||
| * Get or create a CrudPanel instance for the given controller. | ||
| */ | ||
| public function getCrudPanel(CrudControllerContract|string $controller): CrudPanel | ||
| { | ||
| $controllerClass = is_string($controller) ? $controller : get_class($controller); | ||
|
|
||
| if (isset($this->cruds[$controllerClass])) { | ||
| return $this->cruds[$controllerClass]; | ||
| } | ||
|
|
||
| $instance = new CrudPanel(); | ||
|
|
||
| $this->cruds[$controllerClass] = $instance; | ||
|
|
||
| return $this->cruds[$controllerClass]; | ||
| } | ||
|
|
||
| /** | ||
| * Setup and initialize a CrudPanel for the given controller and operation. | ||
| * | ||
| * @param string $controller The controller class name | ||
| * @param string|null $operation The operation to set (defaults to 'list') | ||
| * @return CrudPanel The initialized CrudPanel instance | ||
| */ | ||
| public function setupCrudPanel(string $controller, ?string $operation = null): CrudPanel | ||
| { | ||
| $controller = $this->getActiveController() ?? $controller; | ||
|
|
||
| $controller = is_string($controller) ? app($controller) : $controller; | ||
|
|
||
| $crud = $this->getCrudPanel($controller); | ||
|
|
||
| // Use provided operation or default to 'list' | ||
| $operation = $operation ?? 'list'; | ||
| $crud->setOperation($operation); | ||
|
|
||
| $primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest(); | ||
| if (! $crud->isInitialized()) { | ||
| self::setActiveController($controller::class); | ||
| $controller->initializeCrudPanel($primaryControllerRequest, $crud); | ||
| self::unsetActiveController(); | ||
| $crud = $this->cruds[$controller::class]; | ||
|
|
||
| return $this->cruds[$controller::class]; | ||
| } | ||
|
|
||
| return $this->cruds[$controller::class]; | ||
| } | ||
|
|
||
| /** | ||
| * Record that an operation has been initialized for a controller. | ||
| * | ||
| * @param string $controller The controller class name | ||
| * @param string $operation The operation name (e.g., 'list', 'create', 'update') | ||
| */ | ||
| public function storeInitializedOperation(string $controller, string $operation): void | ||
| { | ||
| $this->initializedOperations[$controller][] = $operation; | ||
| } | ||
|
|
||
| /** | ||
| * Get the list of operations that have been initialized for a controller. | ||
| * | ||
| * @param string $controller The controller class name | ||
| * @return array<string> Array of initialized operation names | ||
| */ | ||
| public function getInitializedOperations(string $controller): array | ||
| { | ||
| return $this->initializedOperations[$controller] ?? []; | ||
| } | ||
|
|
||
| /** | ||
| * Store a CrudPanel instance for a specific controller. | ||
| */ | ||
| public function storeCrudPanel(string $controller, CrudPanel $crud): void | ||
| { | ||
| $this->cruds[$controller] = $crud; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a CrudPanel exists for the given controller. | ||
| */ | ||
| public function hasCrudPanel(string $controller): bool | ||
| { | ||
| return isset($this->cruds[$controller]); | ||
| } | ||
|
|
||
| /** | ||
| * Get the active CrudPanel for a controller, with fallback logic. | ||
| * | ||
| * @param string $controller The controller class name | ||
| * @return CrudPanel The CrudPanel instance, creating one if necessary | ||
| */ | ||
| public function getActiveCrudPanel(string $controller): CrudPanel | ||
| { | ||
| if (! isset($this->cruds[$controller])) { | ||
| return $this->getCrudPanel($this->getActiveController() ?? $this->getParentController() ?? $controller); | ||
| } | ||
|
|
||
| return $this->cruds[$controller]; | ||
| } | ||
|
|
||
| /** | ||
| * Get the parent (first registered) controller class name. | ||
| * | ||
| * @return string|null The parent controller class name or null if none exists | ||
| */ | ||
| public function getParentController(): ?string | ||
| { | ||
| if (! empty($this->cruds)) { | ||
| return array_key_first($this->cruds); | ||
| } | ||
|
|
||
| return $this->getActiveController(); | ||
| } | ||
|
|
||
| /** | ||
| * Set the currently active controller and clear the CRUD facade cache. | ||
| * | ||
| * @param string $controller The controller class name to set as active | ||
| */ | ||
| public function setActiveController(string $controller): void | ||
| { | ||
| Facade::clearResolvedInstance('crud'); | ||
| $this->currentlyActiveCrudController = $controller; | ||
| } | ||
|
|
||
| /** | ||
| * Get the currently active controller class name. | ||
| * | ||
| * @return string|null The active controller class name or null if none is set | ||
| */ | ||
| public function getActiveController(): ?string | ||
| { | ||
| return $this->currentlyActiveCrudController; | ||
| } | ||
|
|
||
| /** | ||
| * Clear the currently active controller. | ||
| */ | ||
| public function unsetActiveController(): void | ||
| { | ||
| $this->currentlyActiveCrudController = null; | ||
| } | ||
|
|
||
| /** | ||
| * Intelligently identify and return the appropriate CrudPanel based on context. | ||
| * | ||
| * This method uses multiple strategies to find the correct CrudPanel: | ||
| * 1. Use the currently active controller if set | ||
| * 2. Analyze the call stack to find a CRUD controller in the backtrace | ||
| * 3. Return the first available CrudPanel if any exist | ||
| * 4. Create a default CrudPanel as a last resort | ||
| * | ||
| * @return CrudPanel The identified or created CrudPanel instance | ||
| */ | ||
| public function identifyCrudPanel(): CrudPanel | ||
| { | ||
| if ($this->getActiveController()) { | ||
| return $this->getCrudPanel($this->getActiveController()); | ||
| } | ||
|
|
||
| // Prioritize explicit controller context | ||
| $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); | ||
| $controller = null; | ||
|
|
||
| foreach ($trace as $step) { | ||
| if (isset($step['class']) && | ||
| is_a($step['class'], CrudControllerContract::class, true)) { | ||
| $controller = (string) $step['class']; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if ($controller) { | ||
| $crudPanel = $this->getActiveCrudPanel($controller); | ||
|
|
||
| return $crudPanel; | ||
| } | ||
|
|
||
| $cruds = $this->getCrudPanels(); | ||
|
|
||
| if (! empty($cruds)) { | ||
| $crudPanel = reset($cruds); | ||
|
|
||
| return $crudPanel; | ||
| } | ||
|
|
||
| $this->cruds[CrudController::class] = new CrudPanel(); | ||
|
|
||
| return $this->cruds[CrudController::class]; | ||
| } | ||
|
|
||
| /** | ||
| * Get all registered CrudPanel instances. | ||
| * | ||
| * @return array<string, CrudPanel> Array of CrudPanel instances indexed by controller class name | ||
| */ | ||
| public function getCrudPanels(): array | ||
| { | ||
| return $this->cruds; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/app/Http/Controllers/Contracts/CrudControllerContract.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?php | ||
|
|
||
| namespace Backpack\CRUD\app\Http\Controllers\Contracts; | ||
|
|
||
| interface CrudControllerContract | ||
| { | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.