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