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\View\ViewInterface; |
16
|
|
|
use Sco\Admin\Contracts\WithNavigation; |
17
|
|
|
|
18
|
|
|
abstract class Component implements |
19
|
|
|
ComponentInterface, |
20
|
|
|
WithNavigation |
21
|
|
|
{ |
22
|
|
|
use HasAccess, HasEvents, HasNavigation; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var |
26
|
|
|
*/ |
27
|
|
|
protected $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Illuminate\Foundation\Application |
31
|
|
|
*/ |
32
|
|
|
protected $app; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The component display name |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $title; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
43
|
|
|
*/ |
44
|
|
|
protected $repository; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
48
|
|
|
*/ |
49
|
|
|
protected $model; |
50
|
|
|
|
51
|
|
|
protected static $booted = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
55
|
|
|
*/ |
56
|
|
|
protected static $dispatcher; |
57
|
|
|
|
58
|
|
|
abstract public function model(); |
59
|
|
|
|
60
|
|
|
public function __construct(Application $app, RepositoryInterface $repository) |
61
|
|
|
{ |
62
|
|
|
$this->app = $app; |
63
|
|
|
$this->repository = $repository; |
64
|
|
|
|
65
|
|
|
$this->makeModel(); |
66
|
|
|
|
67
|
|
|
$this->repository->setModel($this->getModel()); |
68
|
|
|
|
69
|
|
|
if (!$this->name) { |
70
|
|
|
$this->setDefaultName(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->bootIfNotBooted(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function initialize() |
77
|
|
|
{ |
78
|
|
|
// |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function setDefaultName() |
82
|
|
|
{ |
83
|
|
|
$this->name = $this->getModelClassName(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
protected function getModelClassName() |
90
|
|
|
{ |
91
|
|
|
return snake_case( // 蛇形命名 |
92
|
|
|
str_plural( // 复数 |
93
|
|
|
class_basename( |
94
|
|
|
get_class($this->getModel()) |
95
|
|
|
) |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function makeModel() |
101
|
|
|
{ |
102
|
|
|
$class = $this->model(); |
103
|
|
|
if (empty($class)) { |
104
|
|
|
throw new InvalidArgumentException( |
105
|
|
|
sprintf( |
106
|
|
|
'The component(%s) method "model()" not found value', |
107
|
|
|
get_class($this) |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$model = $this->app->make($this->model()); |
113
|
|
|
|
114
|
|
|
if (!($model instanceof Model)) { |
115
|
|
|
throw new InvalidArgumentException( |
116
|
|
|
sprintf( |
117
|
|
|
"Class %s must be an instance of %s", |
118
|
|
|
$this->model(), |
119
|
|
|
Model::class |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->model = $model; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getName() |
128
|
|
|
{ |
129
|
|
|
return $this->name; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getTitle() |
133
|
|
|
{ |
134
|
|
|
return $this->title; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getModel() |
138
|
|
|
{ |
139
|
|
|
return $this->model; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getRepository() |
143
|
|
|
{ |
144
|
|
|
return $this->repository; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function getConfigs() |
151
|
|
|
{ |
152
|
|
|
return collect([ |
153
|
|
|
//'primaryKey' => $this->getModel()->getKeyName(), |
|
|
|
|
154
|
|
|
'title' => $this->getTitle(), |
155
|
|
|
'accesses' => $this->getAccesses(), |
156
|
|
|
'view' => $this->fireView(), |
157
|
|
|
//'elements' => $this->getElements()->values(), |
|
|
|
|
158
|
|
|
]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
public function fireView() |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
if (!method_exists($this, 'callView')) { |
167
|
|
|
throw new BadMethodCallException('Not Found Method "callView"'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$view = $this->app->call([$this, 'callView']); |
171
|
|
|
|
172
|
|
|
if (!$view instanceof ViewInterface) { |
173
|
|
|
throw new InvalidArgumentException( |
174
|
|
|
sprintf( |
175
|
|
|
'callView must be instanced of "%s".', |
176
|
|
|
ViewInterface::class |
177
|
|
|
) |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
//$view->setComponent($this); |
|
|
|
|
182
|
|
|
$view->initialize(); |
183
|
|
|
|
184
|
|
|
return $view; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function get() |
188
|
|
|
{ |
189
|
|
|
$view = $this->fireView(); |
190
|
|
|
|
191
|
|
|
$view->setRepository($this->getRepository()); |
192
|
|
|
|
193
|
|
|
return $view->get(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
View Code Duplication |
public function fireCreate() |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
if (!method_exists($this, 'callCreate')) { |
202
|
|
|
return; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$form = $this->app->call([$this, 'callCreate']); |
206
|
|
|
if (!$form instanceof FormInterface) { |
207
|
|
|
throw new InvalidArgumentException( |
208
|
|
|
sprintf( |
209
|
|
|
'callCreate must be instanced of "%s".', |
210
|
|
|
FormInterface::class |
211
|
|
|
) |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$form->setModel($this->getModel()); |
216
|
|
|
|
217
|
|
|
return $form; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function store() |
224
|
|
|
{ |
225
|
|
|
$form = $this->fireCreate(); |
226
|
|
|
|
227
|
|
|
$form->validate()->save(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
*/ |
233
|
|
|
public function fireEdit($id) |
234
|
|
|
{ |
235
|
|
|
if (!method_exists($this, 'callEdit')) { |
236
|
|
|
return; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$form = $this->app->call([$this, 'callEdit'], ['id' => $id]); |
240
|
|
|
|
241
|
|
|
if (!$form instanceof FormInterface) { |
242
|
|
|
throw new InvalidArgumentException( |
243
|
|
|
sprintf( |
244
|
|
|
'callEdit must be instanced of "%s".', |
245
|
|
|
FormInterface::class |
246
|
|
|
) |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$model = $this->getRepository()->findOrFail($id); |
251
|
|
|
|
252
|
|
|
$form->setModel($model); |
253
|
|
|
|
254
|
|
|
return $form; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* {@inheritdoc} |
259
|
|
|
*/ |
260
|
|
|
public function update($id) |
261
|
|
|
{ |
262
|
|
|
$form = $this->fireEdit($id); |
263
|
|
|
$form->validate()->save(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function delete($id) |
267
|
|
|
{ |
268
|
|
|
$this->getRepository()->findOrFail($id)->delete(); |
269
|
|
|
return true; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function forceDelete($id) |
273
|
|
|
{ |
274
|
|
|
$this->getRepository()->forceDelete($id); |
275
|
|
|
return true; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function restore($id) |
279
|
|
|
{ |
280
|
|
|
$this->getRepository()->restore($id); |
281
|
|
|
return true; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
protected function bootIfNotBooted() |
285
|
|
|
{ |
286
|
|
|
if (!isset(static::$booted[static::class])) { |
287
|
|
|
static::$booted[static::class] = true; |
288
|
|
|
|
289
|
|
|
$this->fireEvent('booting', false); |
290
|
|
|
|
291
|
|
|
$this->boot(); |
292
|
|
|
|
293
|
|
|
$this->fireEvent('booted', false); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* The "booting" method of the model. |
299
|
|
|
* |
300
|
|
|
* @return void |
301
|
|
|
*/ |
302
|
|
|
protected function boot() |
303
|
|
|
{ |
304
|
|
|
$this->bootTraits(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Boot all of the bootable traits on the model. |
309
|
|
|
* |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
|
|
protected function bootTraits() |
313
|
|
|
{ |
314
|
|
|
foreach (class_uses_recursive($this) as $trait) { |
315
|
|
|
if (method_exists($this, $method = 'boot' . class_basename($trait))) { |
316
|
|
|
$this->$method(); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.