|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terranet\Administrator; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
|
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Terranet\Administrator\Contracts\ActionsManager as ActionsManagerContract; |
|
8
|
|
|
use Terranet\Administrator\Contracts\AutoTranslatable; |
|
9
|
|
|
use Terranet\Administrator\Contracts\Module; |
|
10
|
|
|
use Terranet\Administrator\Contracts\Services\Finder as FinderContract; |
|
11
|
|
|
use Terranet\Administrator\Contracts\Services\TemplateProvider; |
|
12
|
|
|
use Terranet\Administrator\Services\Breadcrumbs; |
|
13
|
|
|
use Terranet\Administrator\Services\CrudActions; |
|
14
|
|
|
use Terranet\Administrator\Services\Finder; |
|
15
|
|
|
use Terranet\Administrator\Services\Saver; |
|
16
|
|
|
use Terranet\Administrator\Services\Template; |
|
17
|
|
|
use Terranet\Administrator\Traits\AutoTranslatesInstances; |
|
18
|
|
|
use Terranet\Administrator\Traits\Module\AllowsNavigation; |
|
19
|
|
|
use Terranet\Administrator\Traits\Module\HasColumns; |
|
20
|
|
|
|
|
21
|
|
|
class Scaffolding implements Module, AutoTranslatable |
|
22
|
|
|
{ |
|
23
|
|
|
use AllowsNavigation, HasColumns, AutoTranslatesInstances; |
|
24
|
|
|
|
|
25
|
|
|
const PAGE_INDEX = 'index'; |
|
26
|
|
|
const PAGE_VIEW = 'view'; |
|
27
|
|
|
const PAGE_EDIT = 'edit'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The module Eloquent model class. |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $model; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Breadcrumbs provider. |
|
38
|
|
|
* |
|
39
|
|
|
* @var Breadcrumbs |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $breadcrumbs = Breadcrumbs::class; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Service layer responsible for searching items. |
|
45
|
|
|
* |
|
46
|
|
|
* @var FinderContract |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $finder = Finder::class; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Service layer responsible for persisting request. |
|
52
|
|
|
* |
|
53
|
|
|
* @var \Terranet\Administrator\Contracts\Services\Saver |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $saver = Saver::class; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Actions manager class. |
|
59
|
|
|
* |
|
60
|
|
|
* @var \Terranet\Administrator\Contracts\Services\CrudActions |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $actions = CrudActions::class; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* View templates provider. |
|
66
|
|
|
* |
|
67
|
|
|
* @var TemplateProvider |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $template = Template::class; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Include or not columns of Date type in index listing. |
|
73
|
|
|
* |
|
74
|
|
|
* @var bool |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $includeDateColumns; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Global ACL Manager. |
|
80
|
|
|
* |
|
81
|
|
|
* @var mixed null |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $guard; |
|
84
|
|
|
|
|
85
|
|
|
/** @var array */ |
|
86
|
|
|
protected static $methods = []; |
|
87
|
|
|
|
|
88
|
|
|
/** @var Request */ |
|
89
|
|
|
protected $request; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Scaffolding constructor. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __construct(Request $request) |
|
95
|
|
|
{ |
|
96
|
|
|
if (null === $this->includeDateColumns) { |
|
97
|
|
|
$this->includeDateColumns = $this->defaultIncludeDateColumnsValue(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->request = $request; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $method |
|
105
|
|
|
* @param $arguments |
|
106
|
|
|
* @return null|mixed |
|
107
|
|
|
*/ |
|
108
|
|
|
public function __call($method, $arguments) |
|
109
|
|
|
{ |
|
110
|
|
|
// Call user-defined method if exists. |
|
111
|
|
|
if ($closure = array_get(static::$methods, $method)) { |
|
|
|
|
|
|
112
|
|
|
return \call_user_func_array($closure, $arguments); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Extend functionality by adding new methods. |
|
120
|
|
|
* |
|
121
|
|
|
* @param $name |
|
122
|
|
|
* @param $closure |
|
123
|
|
|
* @throws Exception |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function addMethod($name, $closure) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!(new static())->hasMethod($name)) { |
|
|
|
|
|
|
128
|
|
|
static::$methods[$name] = $closure; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Disable Actions column totally for Readonly Resources. |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function readonly() |
|
138
|
|
|
{ |
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Check if method exists. |
|
144
|
|
|
* |
|
145
|
|
|
* @param $name |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public function hasMethod($name) |
|
149
|
|
|
{ |
|
150
|
|
|
return method_exists($this, $name) || array_has(static::$methods, $name); |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* The module Templates manager. |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function template() |
|
159
|
|
|
{ |
|
160
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Templates'))) { |
|
161
|
|
|
return $file; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $this->template; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @return Model |
|
169
|
|
|
*/ |
|
170
|
|
|
public function model() |
|
171
|
|
|
{ |
|
172
|
|
|
static $model = null; |
|
173
|
|
|
|
|
174
|
|
|
if (null === $model && ($class = $this->getModelClass())) { |
|
175
|
|
|
$model = new $class(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $model; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param $model |
|
183
|
|
|
* @return $this |
|
184
|
|
|
*/ |
|
185
|
|
|
public function setModel($model) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->model = $model; |
|
188
|
|
|
|
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Define the class responsive for fetching items. |
|
194
|
|
|
* |
|
195
|
|
|
* @return mixed |
|
196
|
|
|
*/ |
|
197
|
|
|
public function finder() |
|
198
|
|
|
{ |
|
199
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Finders'))) { |
|
200
|
|
|
return $file; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $this->finder; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @return Request |
|
208
|
|
|
*/ |
|
209
|
|
|
public function request() |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->request; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return mixed |
|
216
|
|
|
*/ |
|
217
|
|
|
public function finderInstance(): FinderContract |
|
218
|
|
|
{ |
|
219
|
|
|
$className = $this->finder(); |
|
220
|
|
|
|
|
221
|
|
|
return once(function () use ($className) { |
|
222
|
|
|
$this->columns(); |
|
223
|
|
|
|
|
224
|
|
|
return new $className($this); |
|
225
|
|
|
}); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Define the class responsive for persisting items. |
|
230
|
|
|
* |
|
231
|
|
|
* @return mixed |
|
232
|
|
|
*/ |
|
233
|
|
|
public function saver() |
|
234
|
|
|
{ |
|
235
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Savers'))) { |
|
236
|
|
|
return $file; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
return $this->saver; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Breadcrumbs provider |
|
244
|
|
|
* First parse Module doc block for provider declaration. |
|
245
|
|
|
* |
|
246
|
|
|
* @return mixed |
|
247
|
|
|
*/ |
|
248
|
|
|
public function breadcrumbs() |
|
249
|
|
|
{ |
|
250
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Breadcrumbs'))) { |
|
251
|
|
|
return $file; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return $this->breadcrumbs; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Define the Actions provider - object responsive for |
|
259
|
|
|
* CRUD operations, Export, etc... |
|
260
|
|
|
* as like as checks action permissions. |
|
261
|
|
|
* |
|
262
|
|
|
* @return mixed |
|
263
|
|
|
*/ |
|
264
|
|
|
public function actions() |
|
265
|
|
|
{ |
|
266
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Actions'))) { |
|
267
|
|
|
return $file; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return $this->actions; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @return ActionsManager |
|
275
|
|
|
* @throws Exception |
|
276
|
|
|
*/ |
|
277
|
|
|
public function actionsManager(): ActionsManagerContract |
|
278
|
|
|
{ |
|
279
|
|
|
$handler = $this->actions(); |
|
280
|
|
|
$handler = new $handler($this); |
|
281
|
|
|
|
|
282
|
|
|
if (!$handler instanceof CrudActions) { |
|
283
|
|
|
throw new Exception('Actions handler must implement '.CrudActions::class.' contract'); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return new ActionsManager($handler, $this); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* The module Eloquent model. |
|
291
|
|
|
* |
|
292
|
|
|
* @return mixed |
|
293
|
|
|
* @throws \Exception |
|
294
|
|
|
*/ |
|
295
|
|
|
protected function getModelClass() |
|
296
|
|
|
{ |
|
297
|
|
|
return $this->model; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Get the full path to class of special type. |
|
302
|
|
|
* |
|
303
|
|
|
* @param $type |
|
304
|
|
|
* @return string |
|
305
|
|
|
*/ |
|
306
|
|
|
protected function getQualifiedClassNameOfType($type) |
|
307
|
|
|
{ |
|
308
|
|
|
return app()->getNamespace()."Http\\Terranet\\Administrator\\{$type}\\".class_basename($this); |
|
|
|
|
|
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @return mixed |
|
313
|
|
|
*/ |
|
314
|
|
|
protected function defaultIncludeDateColumnsValue() |
|
315
|
|
|
{ |
|
316
|
|
|
return config( |
|
|
|
|
|
|
317
|
|
|
"administrator.grid.timestamps.{$this->url()}", |
|
318
|
|
|
config('administrator.grid.timestamps.enabled') |
|
319
|
|
|
); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths