1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Component; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Illuminate\Support\Collection; |
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
|
|
|
protected $title; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
38
|
|
|
*/ |
39
|
|
|
protected $repository; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
43
|
|
|
*/ |
44
|
|
|
protected $model; |
45
|
|
|
|
46
|
|
|
protected static $booted = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
50
|
|
|
*/ |
51
|
|
|
protected static $dispatcher; |
52
|
|
|
|
53
|
|
|
public function __construct(Application $app, $modelClass) |
54
|
|
|
{ |
55
|
|
|
$this->app = $app; |
56
|
|
|
|
57
|
|
|
$this->repository = $this->app->make(RepositoryInterface::class); |
58
|
|
|
$this->repository->setClass($modelClass); |
59
|
|
|
|
60
|
|
|
$this->model = $this->repository->getModel(); |
61
|
|
|
if (!$this->name) { |
62
|
|
|
$this->setDefaultName(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->bootIfNotBooted(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function setDefaultName() |
69
|
|
|
{ |
70
|
|
|
$this->name = $this->getModelClassName(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getModelClassName() |
74
|
|
|
{ |
75
|
|
|
return snake_case( |
76
|
|
|
str_plural( |
77
|
|
|
class_basename( |
78
|
|
|
get_class($this->getModel()) |
79
|
|
|
) |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getName() |
85
|
|
|
{ |
86
|
|
|
return $this->name; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getTitle() |
90
|
|
|
{ |
91
|
|
|
return $this->title; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getModel() |
95
|
|
|
{ |
96
|
|
|
return $this->model; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getRepository() |
100
|
|
|
{ |
101
|
|
|
return $this->repository; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getAccesses() |
105
|
|
|
{ |
106
|
|
|
return static::$abilities->mapWithKeys(function ($item, $key) { |
|
|
|
|
107
|
|
|
return [$key => $this->can($item)]; |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function getConfigs() |
115
|
|
|
{ |
116
|
|
|
return collect([ |
117
|
|
|
//'primaryKey' => $this->getModel()->getKeyName(), |
|
|
|
|
118
|
|
|
'title' => $this->getTitle(), |
119
|
|
|
'accesses' => $this->getAccesses(), |
120
|
|
|
'view' => $this->fireView(), |
121
|
|
|
//'elements' => $this->getElements()->values(), |
|
|
|
|
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function fireView() |
129
|
|
|
{ |
130
|
|
|
if (!method_exists($this, 'callView')) { |
131
|
|
|
throw new BadMethodCallException('Not Found Method "callView"'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$view = $this->app->call([$this, 'callView']); |
135
|
|
|
|
136
|
|
|
if (!$view instanceof ViewInterface) { |
137
|
|
|
throw new InvalidArgumentException( |
138
|
|
|
sprintf( |
139
|
|
|
'callView must be instanced of "%s".', |
140
|
|
|
ViewInterface::class |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $view; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function get() |
149
|
|
|
{ |
150
|
|
|
$view = $this->fireView(); |
151
|
|
|
|
152
|
|
|
$view->setRepository($this->getRepository()); |
153
|
|
|
|
154
|
|
|
return $view->get(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function fireCreate() |
161
|
|
|
{ |
162
|
|
|
if (!method_exists($this, 'callCreate')) { |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$form = $this->app->call([$this, 'callCreate']); |
167
|
|
|
if (!$form instanceof FormInterface) { |
168
|
|
|
throw new InvalidArgumentException( |
169
|
|
|
sprintf( |
170
|
|
|
'callCreate must be instanced of "%s".', |
171
|
|
|
FormInterface::class |
172
|
|
|
) |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$form->setModel($this->getModel()); |
177
|
|
|
|
178
|
|
|
return $form; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function store() |
185
|
|
|
{ |
186
|
|
|
$form = $this->fireCreate(); |
187
|
|
|
|
188
|
|
|
$form->validate()->save(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function fireEdit($id) |
195
|
|
|
{ |
196
|
|
|
if (!method_exists($this, 'callEdit')) { |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$form = $this->app->call([$this, 'callEdit'], ['id' => $id]); |
201
|
|
|
|
202
|
|
|
if (!$form instanceof FormInterface) { |
203
|
|
|
throw new InvalidArgumentException( |
204
|
|
|
sprintf( |
205
|
|
|
'callEdit must be instanced of "%s".', |
206
|
|
|
FormInterface::class |
207
|
|
|
) |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$model = $this->getRepository()->findOrFail($id); |
212
|
|
|
|
213
|
|
|
$form->setModel($model); |
214
|
|
|
|
215
|
|
|
return $form; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritdoc} |
220
|
|
|
*/ |
221
|
|
|
public function update($id) |
222
|
|
|
{ |
223
|
|
|
$form = $this->fireEdit($id); |
224
|
|
|
$form->validate()->save(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function delete($id) |
228
|
|
|
{ |
229
|
|
|
$this->getRepository()->findOrFail($id)->delete(); |
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function forceDelete($id) |
234
|
|
|
{ |
235
|
|
|
$this->getRepository()->forceDelete($id); |
236
|
|
|
return true; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function restore($id) |
240
|
|
|
{ |
241
|
|
|
$this->getRepository()->restore($id); |
242
|
|
|
return true; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
protected function bootIfNotBooted() |
246
|
|
|
{ |
247
|
|
|
if (!isset(static::$booted[static::class])) { |
248
|
|
|
static::$booted[static::class] = true; |
249
|
|
|
|
250
|
|
|
$this->fireEvent('booting', false); |
251
|
|
|
|
252
|
|
|
static::boot(); |
253
|
|
|
|
254
|
|
|
$this->fireEvent('booted', false); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* The "booting" method of the model. |
260
|
|
|
* |
261
|
|
|
* @return void |
262
|
|
|
*/ |
263
|
|
|
protected static function boot() |
264
|
|
|
{ |
265
|
|
|
static::bootTraits(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Boot all of the bootable traits on the model. |
270
|
|
|
* |
271
|
|
|
* @return void |
272
|
|
|
*/ |
273
|
|
|
protected static function bootTraits() |
274
|
|
|
{ |
275
|
|
|
$class = static::class; |
276
|
|
|
|
277
|
|
|
foreach (class_uses_recursive($class) as $trait) { |
278
|
|
|
if (method_exists($class, $method = 'boot'.class_basename($trait))) { |
279
|
|
|
forward_static_call([$class, $method]); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: