1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Controllers\Contracts\CrudControllerContract; |
6
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
7
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
8
|
|
|
use Illuminate\Support\Facades\Facade; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* CrudPanelManager - Central registry and factory for CRUD panels. |
12
|
|
|
* |
13
|
|
|
* This class manages multiple CrudPanel instances across different controllers. |
14
|
|
|
* It acts as a singleton registry that: |
15
|
|
|
* - Creates and stores CrudPanel instances for each controller |
16
|
|
|
* - Tracks which operations have been initialized for each controller |
17
|
|
|
* - Manages the currently active controller context |
18
|
|
|
* - Provides methods to retrieve the appropriate CrudPanel based on context |
19
|
|
|
* |
20
|
|
|
* This allows multiple CRUD controllers to coexist and share state properly |
21
|
|
|
* within a single request lifecycle. |
22
|
|
|
*/ |
23
|
|
|
final class CrudPanelManager |
24
|
|
|
{ |
25
|
|
|
/** @var array<string, CrudPanel> Registry of CrudPanel instances indexed by controller class name */ |
26
|
|
|
private array $cruds = []; |
27
|
|
|
|
28
|
|
|
/** @var array<string, array<string>> Tracks which operations have been initialized for each controller */ |
29
|
|
|
private array $initializedOperations = []; |
30
|
|
|
|
31
|
|
|
/** @var string|null The currently active controller class name */ |
32
|
|
|
private ?string $currentlyActiveCrudController = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get or create a CrudPanel instance for the given controller. |
36
|
|
|
*/ |
37
|
|
|
public function getCrudPanel(CrudControllerContract|string $controller): CrudPanel |
38
|
|
|
{ |
39
|
|
|
$controllerClass = is_string($controller) ? $controller : get_class($controller); |
40
|
|
|
|
41
|
|
|
if (isset($this->cruds[$controllerClass])) { |
42
|
|
|
return $this->cruds[$controllerClass]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$instance = new CrudPanel(); |
46
|
|
|
|
47
|
|
|
$this->cruds[$controllerClass] = $instance; |
48
|
|
|
|
49
|
|
|
return $this->cruds[$controllerClass]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Setup and initialize a CrudPanel for the given controller and operation. |
54
|
|
|
* |
55
|
|
|
* @param string $controller The controller class name |
56
|
|
|
* @param string|null $operation The operation to set (defaults to 'list') |
57
|
|
|
* @return CrudPanel The initialized CrudPanel instance |
58
|
|
|
*/ |
59
|
|
|
public function setupCrudPanel(string $controller, ?string $operation = null): CrudPanel |
60
|
|
|
{ |
61
|
|
|
$controller = $this->getActiveController() ?? $controller; |
62
|
|
|
|
63
|
|
|
$controller = is_string($controller) ? app($controller) : $controller; |
|
|
|
|
64
|
|
|
|
65
|
|
|
$crud = $this->getCrudPanel($controller); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// Use provided operation or default to 'list' |
68
|
|
|
$operation = $operation ?? 'list'; |
69
|
|
|
$crud->setOperation($operation); |
70
|
|
|
|
71
|
|
|
$primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest(); |
72
|
|
|
if (! $crud->isInitialized() || ! $this->isOperationInitialized($controller::class, $operation)) { |
73
|
|
|
self::setActiveController($controller::class); |
|
|
|
|
74
|
|
|
$crud->initialized = false; |
75
|
|
|
$controller->initializeCrudPanel($primaryControllerRequest, $crud); |
76
|
|
|
self::unsetActiveController(); |
|
|
|
|
77
|
|
|
$crud = $this->cruds[$controller::class]; |
|
|
|
|
78
|
|
|
|
79
|
|
|
return $this->cruds[$controller::class]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->cruds[$controller::class]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Record that an operation has been initialized for a controller. |
87
|
|
|
* |
88
|
|
|
* @param string $controller The controller class name |
89
|
|
|
* @param string $operation The operation name (e.g., 'list', 'create', 'update') |
90
|
|
|
*/ |
91
|
|
|
public function storeInitializedOperation(string $controller, ?string $operation): void |
92
|
|
|
{ |
93
|
|
|
if (! $operation) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
$this->initializedOperations[$controller][] = $operation; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the list of operations that have been initialized for a controller. |
101
|
|
|
* |
102
|
|
|
* @param string $controller The controller class name |
103
|
|
|
* @return array<string> Array of initialized operation names |
104
|
|
|
*/ |
105
|
|
|
public function getInitializedOperations(string $controller): array |
106
|
|
|
{ |
107
|
|
|
return $this->initializedOperations[$controller] ?? []; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check if a specific operation has been initialized for a controller. |
112
|
|
|
*/ |
113
|
|
|
public function isOperationInitialized(string $controller, string $operation): bool |
114
|
|
|
{ |
115
|
|
|
return in_array($operation, $this->getInitializedOperations($controller), true); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Store a CrudPanel instance for a specific controller. |
120
|
|
|
*/ |
121
|
|
|
public function storeCrudPanel(string $controller, CrudPanel $crud): void |
122
|
|
|
{ |
123
|
|
|
$this->cruds[$controller] = $crud; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check if a CrudPanel exists for the given controller. |
128
|
|
|
*/ |
129
|
|
|
public function hasCrudPanel(string $controller): bool |
130
|
|
|
{ |
131
|
|
|
return isset($this->cruds[$controller]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the active CrudPanel for a controller, with fallback logic. |
136
|
|
|
* |
137
|
|
|
* @param string $controller The controller class name |
138
|
|
|
* @return CrudPanel The CrudPanel instance, creating one if necessary |
139
|
|
|
*/ |
140
|
|
|
public function getActiveCrudPanel(string $controller): CrudPanel |
141
|
|
|
{ |
142
|
|
|
if (! isset($this->cruds[$controller])) { |
143
|
|
|
return $this->getCrudPanel($this->getActiveController() ?? $this->getParentController() ?? $controller); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->cruds[$controller]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the parent (first registered) controller class name. |
151
|
|
|
* |
152
|
|
|
* @return string|null The parent controller class name or null if none exists |
153
|
|
|
*/ |
154
|
|
|
public function getParentController(): ?string |
155
|
|
|
{ |
156
|
|
|
if (! empty($this->cruds)) { |
157
|
|
|
return array_key_first($this->cruds); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->getActiveController(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set the currently active controller and clear the CRUD facade cache. |
165
|
|
|
* |
166
|
|
|
* @param string $controller The controller class name to set as active |
167
|
|
|
*/ |
168
|
|
|
public function setActiveController(string $controller): void |
169
|
|
|
{ |
170
|
|
|
Facade::clearResolvedInstance('crud'); |
171
|
|
|
$this->currentlyActiveCrudController = $controller; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the currently active controller class name. |
176
|
|
|
* |
177
|
|
|
* @return string|null The active controller class name or null if none is set |
178
|
|
|
*/ |
179
|
|
|
public function getActiveController(): ?string |
180
|
|
|
{ |
181
|
|
|
return $this->currentlyActiveCrudController; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Clear the currently active controller. |
186
|
|
|
*/ |
187
|
|
|
public function unsetActiveController(): void |
188
|
|
|
{ |
189
|
|
|
$this->currentlyActiveCrudController = null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Intelligently identify and return the appropriate CrudPanel based on context. |
194
|
|
|
* |
195
|
|
|
* This method uses multiple strategies to find the correct CrudPanel: |
196
|
|
|
* 1. Use the currently active controller if set |
197
|
|
|
* 2. Analyze the call stack to find a CRUD controller in the backtrace |
198
|
|
|
* 3. Return the first available CrudPanel if any exist |
199
|
|
|
* 4. Create a default CrudPanel as a last resort |
200
|
|
|
* |
201
|
|
|
* @return CrudPanel The identified or created CrudPanel instance |
202
|
|
|
*/ |
203
|
|
|
public function identifyCrudPanel(): CrudPanel |
204
|
|
|
{ |
205
|
|
|
if ($this->getActiveController()) { |
206
|
|
|
return $this->getCrudPanel($this->getActiveController()); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// Prioritize explicit controller context |
210
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
211
|
|
|
$controller = null; |
212
|
|
|
|
213
|
|
|
foreach ($trace as $step) { |
214
|
|
|
if (isset($step['class']) && |
215
|
|
|
is_a($step['class'], CrudControllerContract::class, true)) { |
216
|
|
|
$controller = (string) $step['class']; |
217
|
|
|
break; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if ($controller) { |
222
|
|
|
$crudPanel = $this->getActiveCrudPanel($controller); |
223
|
|
|
|
224
|
|
|
return $crudPanel; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$cruds = $this->getCrudPanels(); |
228
|
|
|
|
229
|
|
|
if (! empty($cruds)) { |
230
|
|
|
$crudPanel = reset($cruds); |
231
|
|
|
|
232
|
|
|
return $crudPanel; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$this->cruds[CrudController::class] = new CrudPanel(); |
236
|
|
|
|
237
|
|
|
return $this->cruds[CrudController::class]; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get all registered CrudPanel instances. |
242
|
|
|
* |
243
|
|
|
* @return array<string, CrudPanel> Array of CrudPanel instances indexed by controller class name |
244
|
|
|
*/ |
245
|
|
|
public function getCrudPanels(): array |
246
|
|
|
{ |
247
|
|
|
return $this->cruds; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|