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