1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Component; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Illuminate\Foundation\Application; |
9
|
|
|
use Sco\Admin\Component\Concerns\HasAccess; |
10
|
|
|
use Sco\Admin\Component\Concerns\HasEvents; |
11
|
|
|
use Sco\Admin\Component\Concerns\HasNavigation; |
12
|
|
|
use Sco\Admin\Contracts\ComponentInterface; |
13
|
|
|
use Sco\Admin\Contracts\Form\FormInterface; |
14
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
15
|
|
|
use Sco\Admin\Contracts\Display\DisplayInterface; |
16
|
|
|
|
17
|
|
|
abstract class Component implements ComponentInterface |
18
|
|
|
{ |
19
|
|
|
use HasAccess, HasEvents, HasNavigation; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
protected $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Illuminate\Foundation\Application |
28
|
|
|
*/ |
29
|
|
|
protected $app; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The component display name |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $title; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
40
|
|
|
*/ |
41
|
|
|
protected $repository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
45
|
|
|
*/ |
46
|
|
|
protected $model; |
47
|
|
|
|
48
|
|
|
protected static $booted = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
52
|
|
|
*/ |
53
|
|
|
protected static $dispatcher; |
54
|
|
|
|
55
|
|
|
abstract public function model(); |
56
|
|
|
|
57
|
39 |
|
public function __construct(Application $app) |
58
|
|
|
{ |
59
|
39 |
|
$this->app = $app; |
60
|
|
|
|
61
|
39 |
|
$this->bootIfNotBooted(); |
62
|
39 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
3 |
|
public function getName() |
68
|
|
|
{ |
69
|
3 |
|
if (is_null($this->name)) { |
70
|
3 |
|
$this->setName($this->getDefaultName()); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
return $this->name; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
3 |
|
public function setName(string $value) |
80
|
|
|
{ |
81
|
3 |
|
$this->name = $value; |
82
|
|
|
|
83
|
3 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
6 |
|
public function getTitle() |
90
|
|
|
{ |
91
|
6 |
|
return $this->title; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
3 |
|
public function setTitle(string $value) |
98
|
|
|
{ |
99
|
3 |
|
$this->title = $value; |
100
|
|
|
|
101
|
3 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
protected function getDefaultName() |
105
|
|
|
{ |
106
|
3 |
|
return $this->getModelClassName(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
3 |
|
protected function getModelClassName() |
113
|
|
|
{ |
114
|
3 |
|
return snake_case( // 蛇形命名 |
115
|
3 |
|
str_plural( // 复数 |
116
|
3 |
|
class_basename( |
117
|
3 |
|
get_class($this->getModel()) |
118
|
|
|
) |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
18 |
|
public function setModel(Model $model) |
127
|
|
|
{ |
128
|
18 |
|
$this->model = $model; |
129
|
|
|
|
130
|
18 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
24 |
|
public function getModel() |
137
|
|
|
{ |
138
|
24 |
|
if (is_null($this->model)) { |
139
|
24 |
|
$this->setModel($this->makeModel()); |
140
|
|
|
} |
141
|
|
|
|
142
|
18 |
|
return $this->model; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return \Illuminate\Database\Eloquent\Model|mixed |
147
|
|
|
*/ |
148
|
24 |
|
protected function makeModel() |
149
|
|
|
{ |
150
|
24 |
|
$class = $this->model(); |
151
|
24 |
|
if (empty($class)) { |
152
|
3 |
|
throw new InvalidArgumentException( |
153
|
3 |
|
sprintf( |
154
|
3 |
|
'The component(%s) method "model()" not found value', |
155
|
3 |
|
get_class($this) |
156
|
|
|
) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
21 |
|
$model = $this->app->make($this->model()); |
161
|
|
|
|
162
|
21 |
|
if (! ($model instanceof Model)) { |
163
|
3 |
|
throw new InvalidArgumentException( |
164
|
3 |
|
sprintf( |
165
|
3 |
|
"Class %s must be an instance of %s", |
166
|
3 |
|
$this->model(), |
167
|
3 |
|
Model::class |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
18 |
|
return $model; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
3 |
|
public function getRepository() |
179
|
|
|
{ |
180
|
3 |
|
if (is_null($this->repository)) { |
181
|
3 |
|
$this->setRepository($this->makeRepository()); |
182
|
|
|
} |
183
|
|
|
|
184
|
3 |
|
return $this->repository; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
3 |
|
public function setRepository(RepositoryInterface $repository) |
191
|
|
|
{ |
192
|
3 |
|
$this->repository = $repository; |
193
|
|
|
|
194
|
3 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
protected function makeRepository() |
198
|
|
|
{ |
199
|
3 |
|
$repository = $this->app->make(RepositoryInterface::class); |
200
|
3 |
|
$repository->setModel($this->getModel()); |
201
|
|
|
|
202
|
3 |
|
return $repository; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
3 |
|
public function getConfigs() |
209
|
|
|
{ |
210
|
3 |
|
return collect([ |
211
|
3 |
|
'title' => $this->getTitle(), |
212
|
3 |
|
'accesses' => $this->getAccesses(), |
213
|
3 |
|
'display' => $this->fireDisplay() ?: [], |
214
|
|
|
]); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
12 |
View Code Duplication |
final public function fireDisplay() |
221
|
|
|
{ |
222
|
12 |
|
if (! method_exists($this, 'callDisplay')) { |
223
|
3 |
|
return; |
224
|
|
|
} |
225
|
|
|
|
226
|
9 |
|
$display = $this->app->call([$this, 'callDisplay']); |
227
|
|
|
|
228
|
9 |
|
if (! ($display instanceof DisplayInterface)) { |
229
|
3 |
|
throw new InvalidArgumentException( |
230
|
3 |
|
sprintf( |
231
|
3 |
|
'callDisplay must be instanced of "%s".', |
232
|
3 |
|
DisplayInterface::class |
233
|
|
|
) |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
|
237
|
6 |
|
$display->setModel($this->getModel()); |
|
|
|
|
238
|
|
|
|
239
|
6 |
|
return $display; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function get() |
243
|
|
|
{ |
244
|
|
|
$display = $this->fireDisplay(); |
245
|
|
|
if ($display) { |
246
|
|
|
return $display->get(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritdoc} |
254
|
|
|
*/ |
255
|
9 |
View Code Duplication |
final public function fireCreate() |
256
|
|
|
{ |
257
|
9 |
|
if (! method_exists($this, 'callCreate')) { |
258
|
3 |
|
return; |
259
|
|
|
} |
260
|
|
|
|
261
|
6 |
|
$form = $this->app->call([$this, 'callCreate']); |
262
|
6 |
|
if (! $form instanceof FormInterface) { |
263
|
3 |
|
throw new InvalidArgumentException( |
264
|
3 |
|
sprintf( |
265
|
3 |
|
'callCreate must be instanced of "%s".', |
266
|
3 |
|
FormInterface::class |
267
|
|
|
) |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
3 |
|
$form->setModel($this->getModel()); |
272
|
|
|
|
273
|
3 |
|
return $form; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* {@inheritdoc} |
278
|
|
|
*/ |
279
|
|
|
public function store() |
280
|
|
|
{ |
281
|
|
|
$form = $this->fireCreate(); |
282
|
|
|
if ($form) { |
283
|
|
|
return $form->validate()->save(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* {@inheritdoc} |
291
|
|
|
*/ |
292
|
6 |
|
final public function fireEdit($id) |
293
|
|
|
{ |
294
|
6 |
|
if (! method_exists($this, 'callEdit')) { |
295
|
3 |
|
return; |
296
|
|
|
} |
297
|
|
|
|
298
|
3 |
|
$form = $this->app->call([$this, 'callEdit'], ['id' => $id]); |
299
|
|
|
|
300
|
3 |
|
if (! $form instanceof FormInterface) { |
301
|
3 |
|
throw new InvalidArgumentException( |
302
|
3 |
|
sprintf( |
303
|
3 |
|
'callEdit must be instanced of "%s".', |
304
|
3 |
|
FormInterface::class |
305
|
|
|
) |
306
|
|
|
); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$model = $this->getRepository()->findOrFail($id); |
310
|
|
|
|
311
|
|
|
$form->setModel($model); |
312
|
|
|
|
313
|
|
|
return $form; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* {@inheritdoc} |
318
|
|
|
*/ |
319
|
|
|
public function update($id) |
320
|
|
|
{ |
321
|
|
|
$form = $this->fireEdit($id); |
322
|
|
|
if ($form) { |
323
|
|
|
return $form->validate()->save(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function delete($id) |
330
|
|
|
{ |
331
|
|
|
$this->getRepository()->delete($id); |
332
|
|
|
|
333
|
|
|
return true; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function forceDelete($id) |
337
|
|
|
{ |
338
|
|
|
$this->getRepository()->forceDelete($id); |
339
|
|
|
|
340
|
|
|
return true; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function restore($id) |
344
|
|
|
{ |
345
|
|
|
$this->getRepository()->restore($id); |
346
|
|
|
|
347
|
|
|
return true; |
348
|
|
|
} |
349
|
|
|
|
350
|
39 |
|
protected function bootIfNotBooted() |
351
|
|
|
{ |
352
|
39 |
|
if (! isset(static::$booted[static::class])) { |
353
|
36 |
|
static::$booted[static::class] = true; |
354
|
|
|
|
355
|
36 |
|
$this->fireEvent('booting', false); |
356
|
|
|
|
357
|
36 |
|
$this->boot(); |
358
|
|
|
|
359
|
36 |
|
$this->fireEvent('booted', false); |
360
|
|
|
} |
361
|
39 |
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* The "booting" method of the model. |
365
|
|
|
* |
366
|
|
|
* @return void |
367
|
|
|
*/ |
368
|
36 |
|
protected function boot() |
369
|
|
|
{ |
370
|
36 |
|
$this->bootTraits(); |
371
|
36 |
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Boot all of the bootable traits on the model. |
375
|
|
|
* |
376
|
|
|
* @return void |
377
|
|
|
*/ |
378
|
36 |
|
protected function bootTraits() |
379
|
|
|
{ |
380
|
36 |
|
foreach (class_uses_recursive($this) as $trait) { |
381
|
36 |
|
if (method_exists($this, $method = 'boot' . class_basename($trait))) { |
382
|
36 |
|
$this->$method(); |
383
|
|
|
} |
384
|
|
|
} |
385
|
36 |
|
} |
386
|
|
|
} |
387
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: