Skip to content

Commit 20ebccb

Browse files
committed
wip
1 parent 70676d1 commit 20ebccb

File tree

1 file changed

+54
-36
lines changed

1 file changed

+54
-36
lines changed

src/CrudPanelManager.php

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public function getCrudPanel(CrudControllerContract|string $controller): CrudPan
5858
*/
5959
public function setupCrudPanel(string $controller, ?string $operation = null): CrudPanel
6060
{
61+
// Resolve potential active controller and ensure we have an instance
6162
$controller = $this->getActiveController() ?? $controller;
62-
6363
$controller = is_string($controller) ? app($controller) : $controller;
6464

6565
$crud = $this->getCrudPanel($controller);
@@ -69,55 +69,73 @@ public function setupCrudPanel(string $controller, ?string $operation = null): C
6969

7070
$shouldIsolate = $this->shouldIsolateOperation($controller::class, $operation);
7171

72+
// primary controller request is used when doing a full initialization
7273
$primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest();
7374

75+
// If the panel is already initialized but a different operation is requested
76+
// and we don't need to isolate that operation, do a simple setup and return early.
7477
if ($crud->isInitialized() && $crud->getOperation() !== $operation && ! $shouldIsolate) {
75-
self::setActiveController($controller::class);
78+
return $this->performSimpleOperationSwitch($controller, $operation, $crud);
79+
}
7680

77-
$crud->setOperation($operation);
78-
$this->setupSpecificOperation($controller, $operation, $crud);
81+
// If the panel (or this specific operation) hasn't been initialized yet,
82+
// perform the required initialization (full or operation-specific).
83+
if (! $crud->isInitialized() || ! $this->isOperationInitialized($controller::class, $operation)) {
84+
return $this->performInitialization($controller, $operation, $crud, $primaryControllerRequest, $shouldIsolate);
85+
}
7986

80-
// Mark this operation as initialized
81-
$this->storeInitializedOperation($controller::class, $operation);
87+
// Already initialized and operation matches: nothing to do.
88+
return $this->cruds[$controller::class];
89+
}
8290

83-
self::unsetActiveController();
91+
/**
92+
* Perform a lightweight operation switch when the panel is initialized and
93+
* isolation is not required.
94+
*/
95+
private function performSimpleOperationSwitch($controller, string $operation, CrudPanel $crud): CrudPanel
96+
{
97+
self::setActiveController($controller::class);
8498

85-
return $this->cruds[$controller::class];
86-
}
99+
$crud->setOperation($operation);
100+
$this->setupSpecificOperation($controller, $operation, $crud);
87101

88-
// Check if we need to initialize this specific operation
89-
if (! $crud->isInitialized() || ! $this->isOperationInitialized($controller::class, $operation)) {
90-
self::setActiveController($controller::class);
102+
// Mark this operation as initialized
103+
$this->storeInitializedOperation($controller::class, $operation);
91104

92-
// If the panel isn't initialized at all, do full initialization
93-
if (! $crud->isInitialized()) {
94-
// Set the operation for full initialization
95-
$crud->setOperation($operation);
96-
$crud->initialized = false;
97-
$controller->initializeCrudPanel($primaryControllerRequest, $crud);
98-
} else {
99-
// Panel is initialized, just setup this specific operation
100-
// Use operation isolation for non-primary operations
101-
if ($shouldIsolate) {
102-
$this->setupIsolatedOperation($controller, $operation, $crud);
103-
} else {
104-
// Set the operation for standard setup
105-
$crud->setOperation($operation);
106-
$this->setupSpecificOperation($controller, $operation, $crud);
107-
}
108-
}
105+
self::unsetActiveController();
109106

110-
// Mark this operation as initialized
111-
$this->storeInitializedOperation($controller::class, $operation);
107+
return $this->cruds[$controller::class];
108+
}
112109

113-
self::unsetActiveController();
114-
$crud = $this->cruds[$controller::class];
110+
/**
111+
* Perform full or operation-specific initialization when needed.
112+
*/
113+
private function performInitialization($controller, string $operation, CrudPanel $crud, $primaryControllerRequest, bool $shouldIsolate): CrudPanel
114+
{
115+
self::setActiveController($controller::class);
115116

116-
return $this->cruds[$controller::class];
117+
// If the panel isn't initialized at all, do full initialization
118+
if (! $crud->isInitialized()) {
119+
// Set the operation for full initialization
120+
$crud->setOperation($operation);
121+
$crud->initialized = false;
122+
$controller->initializeCrudPanel($primaryControllerRequest, $crud);
123+
} else {
124+
// Panel is initialized, just setup this specific operation
125+
if ($shouldIsolate) {
126+
$this->setupIsolatedOperation($controller, $operation, $crud);
127+
} else {
128+
// Set the operation for standard setup
129+
$crud->setOperation($operation);
130+
$this->setupSpecificOperation($controller, $operation, $crud);
131+
}
117132
}
118133

119-
// If we reach here, the panel and operation are both initialized
120-
// and the operation matches what was requested, so just return it
134+
// Mark this operation as initialized
135+
$this->storeInitializedOperation($controller::class, $operation);
136+
137+
self::unsetActiveController();
138+
121139
return $this->cruds[$controller::class];
122140
}
123141

0 commit comments

Comments
 (0)