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
|
|
|
final class CrudPanelManager |
11
|
|
|
{ |
12
|
|
|
private array $cruds = []; |
13
|
|
|
|
14
|
|
|
private array $initializedOperations = []; |
15
|
|
|
|
16
|
|
|
private ?string $currentlyActiveCrudController = null; |
17
|
|
|
|
18
|
|
|
public function getCrudPanel(CrudControllerContract|string $controller): CrudPanel |
19
|
|
|
{ |
20
|
|
|
$controllerClass = is_string($controller) ? $controller : get_class($controller); |
21
|
|
|
|
22
|
|
|
if (isset($this->cruds[$controllerClass])) { |
23
|
|
|
return $this->cruds[$controllerClass]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$instance = new CrudPanel(); |
27
|
|
|
|
28
|
|
|
$this->cruds[$controllerClass] = $instance; |
29
|
|
|
|
30
|
|
|
return $this->cruds[$controllerClass]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setupCrudPanel(string $controller, ?string $operation = null): CrudPanel |
34
|
|
|
{ |
35
|
|
|
$controller = $this->getActiveController() ?? $controller; |
36
|
|
|
|
37
|
|
|
$controller = is_string($controller) ? app($controller) : $controller; |
|
|
|
|
38
|
|
|
|
39
|
|
|
$crud = $this->getCrudPanel($controller); |
|
|
|
|
40
|
|
|
|
41
|
|
|
// Use provided operation or default to 'list' |
42
|
|
|
$operation = $operation ?? 'list'; |
43
|
|
|
$crud->setOperation($operation); |
44
|
|
|
|
45
|
|
|
$primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest(); |
46
|
|
|
if (! $crud->isInitialized()) { |
47
|
|
|
self::setActiveController($controller::class); |
|
|
|
|
48
|
|
|
$controller->initializeCrudPanel($primaryControllerRequest, $crud); |
49
|
|
|
self::unsetActiveController(); |
|
|
|
|
50
|
|
|
$crud = $this->cruds[$controller::class]; |
|
|
|
|
51
|
|
|
|
52
|
|
|
return $this->cruds[$controller::class]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->cruds[$controller::class]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function storeInitializedOperation(string $controller, string $operation): void |
59
|
|
|
{ |
60
|
|
|
$this->initializedOperations[$controller][] = $operation; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getInitializedOperations (string $controller): array |
64
|
|
|
{ |
65
|
|
|
return $this->initializedOperations[$controller] ?? []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function storeCrudPanel(string $controller, CrudPanel $crud): void |
69
|
|
|
{ |
70
|
|
|
$this->cruds[$controller] = $crud; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function hasCrudPanel(string $controller): bool |
74
|
|
|
{ |
75
|
|
|
return isset($this->cruds[$controller]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getActiveCrudPanel(string $controller): CrudPanel |
79
|
|
|
{ |
80
|
|
|
if (! isset($this->cruds[$controller])) { |
81
|
|
|
return $this->getCrudPanel($this->getActiveController() ?? $this->getParentController() ?? $controller); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->cruds[$controller]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getParentController(): ?string |
88
|
|
|
{ |
89
|
|
|
if (! empty($this->cruds)) { |
90
|
|
|
return array_key_first($this->cruds); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->getActiveController(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setActiveController(string $controller): void |
97
|
|
|
{ |
98
|
|
|
Facade::clearResolvedInstance('crud'); |
99
|
|
|
$this->currentlyActiveCrudController = $controller; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getActiveController(): ?string |
103
|
|
|
{ |
104
|
|
|
return $this->currentlyActiveCrudController; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function unsetActiveController(): void |
108
|
|
|
{ |
109
|
|
|
$this->currentlyActiveCrudController = null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function identifyCrudPanel(): CrudPanel |
113
|
|
|
{ |
114
|
|
|
if ($this->getActiveController()) { |
115
|
|
|
return $this->getCrudPanel($this->getActiveController()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Prioritize explicit controller context |
119
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
120
|
|
|
$controller = null; |
121
|
|
|
|
122
|
|
|
foreach ($trace as $step) { |
123
|
|
|
if (isset($step['class']) && |
124
|
|
|
is_a($step['class'], CrudControllerContract::class, true)) { |
125
|
|
|
$controller = (string) $step['class']; |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($controller) { |
131
|
|
|
$crudPanel = self::getActiveCrudPanel($controller); |
|
|
|
|
132
|
|
|
|
133
|
|
|
return $crudPanel; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$cruds = self::getCrudPanels(); |
|
|
|
|
137
|
|
|
|
138
|
|
|
if (! empty($cruds)) { |
139
|
|
|
$crudPanel = reset($cruds); |
140
|
|
|
|
141
|
|
|
return $crudPanel; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->cruds[CrudController::class] = new CrudPanel(); |
145
|
|
|
|
146
|
|
|
return $this->cruds[CrudController::class]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getCrudPanels(): array |
150
|
|
|
{ |
151
|
|
|
return $this->cruds; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|