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
|
|
|
|
8
|
|
|
final class BackpackManager |
9
|
|
|
{ |
10
|
|
|
private array $cruds = []; |
11
|
|
|
|
12
|
|
|
private CrudPanel $crudPanelInstance; |
13
|
|
|
|
14
|
|
|
private $requestController = null; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->crudPanelInstance = new CrudPanel(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getCrudPanelInstance(): CrudPanel |
22
|
|
|
{ |
23
|
|
|
return $this->crudPanelInstance; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function crud(CrudControllerContract $controller): CrudPanel |
27
|
|
|
{ |
28
|
|
|
$controllerClass = get_class($controller); |
29
|
|
|
|
30
|
|
|
$this->requestController = $controllerClass; |
31
|
|
|
|
32
|
|
|
if (isset($this->cruds[$controllerClass])) { |
33
|
|
|
return $this->cruds[$controllerClass]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$instance = new CrudPanel(); |
37
|
|
|
|
38
|
|
|
$this->cruds[$controllerClass] = $instance; |
39
|
|
|
|
40
|
|
|
return $this->cruds[$controllerClass]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function crudFromController(string $controller): CrudPanel |
44
|
|
|
{ |
45
|
|
|
$controller = new $controller(); |
46
|
|
|
|
47
|
|
|
$crud = $this->crud($controller); |
48
|
|
|
|
49
|
|
|
$crud->setOperation('list'); |
50
|
|
|
|
51
|
|
|
$primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest(); |
52
|
|
|
|
53
|
|
|
$controller->initializeCrud($primaryControllerRequest, $crud, 'list'); |
54
|
|
|
|
55
|
|
|
return $crud; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function hasCrudController(string $controller): bool |
59
|
|
|
{ |
60
|
|
|
return isset($this->cruds[$controller]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getControllerCrud(string $controller): CrudPanel |
64
|
|
|
{ |
65
|
|
|
if (! isset($this->cruds[$controller])) { |
66
|
|
|
return $this->crudFromController($this->requestController ?? $controller); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->cruds[$controller]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getCruds(): array |
73
|
|
|
{ |
74
|
|
|
return $this->cruds; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|