1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Controllers\Contracts\CrudControllerContract; |
6
|
|
|
use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime; |
7
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
8
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; |
9
|
|
|
use Backpack\CRUD\CrudManager; |
10
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
11
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
12
|
|
|
use Illuminate\Routing\Controller; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class CrudController. |
17
|
|
|
* |
18
|
|
|
* @property-read CrudPanel $crud |
19
|
|
|
* @property array $data |
20
|
|
|
*/ |
21
|
|
|
class CrudController extends Controller implements CrudControllerContract |
22
|
|
|
{ |
23
|
|
|
use DispatchesJobs, ValidatesRequests; |
24
|
|
|
|
25
|
|
|
public $data = []; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
// --------------------------- |
30
|
|
|
// Create the CrudPanel object |
31
|
|
|
// --------------------------- |
32
|
|
|
// Used by developers inside their ProductCrudControllers as |
33
|
|
|
// $this->crud or using the CRUD facade. |
34
|
|
|
// |
35
|
|
|
// It's done inside a middleware closure in order to have |
36
|
|
|
// the complete request inside the CrudPanel object. |
37
|
|
|
$this->middleware(function ($request, $next) { |
38
|
|
|
if (! CrudManager::hasCrudPanel(get_class($this))) { |
|
|
|
|
39
|
|
|
$this->initializeCrudPanel($request); |
40
|
|
|
|
41
|
|
|
return $next($request); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->setupCrudController(); |
45
|
|
|
|
46
|
|
|
CrudManager::getCrudPanel($this)->setRequest($request); |
|
|
|
|
47
|
|
|
|
48
|
|
|
return $next($request); |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function initializeCrudPanel($request, $crudPanel = null): void |
53
|
|
|
{ |
54
|
|
|
$crudPanel ??= CrudManager::getCrudPanel($this); |
55
|
|
|
|
56
|
|
|
$crudPanel = $crudPanel->initialize(get_class($this), $request); |
57
|
|
|
|
58
|
|
|
CrudManager::storeInitializedOperation( |
|
|
|
|
59
|
|
|
get_class($this), |
60
|
|
|
$crudPanel->getCurrentOperation() |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
if (! $crudPanel->isInitialized()) { |
64
|
|
|
$crudPanel->initialized = true; |
65
|
|
|
|
66
|
|
|
$this->setupCrudController($crudPanel->getCurrentOperation()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
CrudManager::storeCrudPanel(get_class($this), $crudPanel); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function setupCrudController($operation = null) |
73
|
|
|
{ |
74
|
|
|
LifecycleHook::trigger('crud:before_setup_defaults', [$this]); |
75
|
|
|
$this->setupDefaults(); |
76
|
|
|
LifecycleHook::trigger('crud:after_setup_defaults', [$this]); |
77
|
|
|
LifecycleHook::trigger('crud:before_setup', [$this]); |
78
|
|
|
$this->setup(); |
79
|
|
|
LifecycleHook::trigger('crud:after_setup', [$this]); |
80
|
|
|
$this->setupConfigurationForCurrentOperation($operation); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Allow developers to set their configuration options for a CrudPanel. |
85
|
|
|
*/ |
86
|
|
|
public function setup() |
87
|
|
|
{ |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Load routes for all operations. |
92
|
|
|
* Allow developers to load extra routes by creating a method that looks like setupOperationNameRoutes. |
93
|
|
|
* |
94
|
|
|
* @param string $segment Name of the current entity (singular). |
95
|
|
|
* @param string $routeName Route name prefix (ends with .). |
96
|
|
|
* @param string $controller Name of the current controller. |
97
|
|
|
*/ |
98
|
|
|
#[DeprecatedIgnoreOnRuntime('we dont call this method anymore unless you had it overwritten in your CrudController')] |
99
|
|
|
public function setupRoutes($segment, $routeName, $controller) |
100
|
|
|
{ |
101
|
|
|
preg_match_all('/(?<=^|;)setup([^;]+?)Routes(;|$)/', implode(';', get_class_methods($this)), $matches); |
102
|
|
|
|
103
|
|
|
if (count($matches[1])) { |
104
|
|
|
foreach ($matches[1] as $methodName) { |
105
|
|
|
$this->{'setup'.$methodName.'Routes'}($segment, $routeName, $controller); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Load defaults for all operations. |
112
|
|
|
* Allow developers to insert default settings by creating a method |
113
|
|
|
* that looks like setupOperationNameDefaults. |
114
|
|
|
*/ |
115
|
|
|
protected function setupDefaults() |
116
|
|
|
{ |
117
|
|
|
preg_match_all('/(?<=^|;)setup([^;]+?)Defaults(;|$)/', implode(';', get_class_methods($this)), $matches); |
118
|
|
|
if (count($matches[1])) { |
119
|
|
|
foreach ($matches[1] as $methodName) { |
120
|
|
|
$this->{'setup'.$methodName.'Defaults'}(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Load configurations for the current operation. |
127
|
|
|
* |
128
|
|
|
* Allow developers to insert default settings by creating a method |
129
|
|
|
* that looks like setupOperationNameOperation (aka setupXxxOperation). |
130
|
|
|
*/ |
131
|
|
|
protected function setupConfigurationForCurrentOperation(?string $operation = null) |
132
|
|
|
{ |
133
|
|
|
$operationName = $operation ?? $this->crud->getCurrentOperation(); |
134
|
|
|
if (! $operationName) { |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
$setupClassName = 'setup'.Str::studly($operationName).'Operation'; |
138
|
|
|
|
139
|
|
|
/* |
140
|
|
|
* FIRST, run all Operation Closures for this operation. |
141
|
|
|
* |
142
|
|
|
* It's preferred for this to run closures first, because |
143
|
|
|
* (1) setup() is usually higher in a controller than any other method, so it's more intuitive, |
144
|
|
|
* since the first thing you write is the first thing that is being run; |
145
|
|
|
* (2) operations use operation closures themselves, inside their setupXxxDefaults(), and |
146
|
|
|
* you'd like the defaults to be applied before anything you write. That way, anything you |
147
|
|
|
* write is done after the default, so you can remove default settings, etc; |
148
|
|
|
*/ |
149
|
|
|
LifecycleHook::trigger($operationName.':before_setup', [$this]); |
150
|
|
|
|
151
|
|
|
$this->crud->applyConfigurationFromSettings($operationName); |
152
|
|
|
/* |
153
|
|
|
* THEN, run the corresponding setupXxxOperation if it exists. |
154
|
|
|
*/ |
155
|
|
|
if (method_exists($this, $setupClassName)) { |
156
|
|
|
$this->{$setupClassName}(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
LifecycleHook::trigger($operationName.':after_setup', [$this]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function __get($name) |
163
|
|
|
{ |
164
|
|
|
if ($name === 'crud') { |
165
|
|
|
return CrudManager::getActiveCrudPanel(get_class($this)); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->{$name}; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|