1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Component; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Application; |
6
|
|
|
use Sco\Admin\Component\Concerns\HasEvents; |
7
|
|
|
use Sco\Admin\Component\Concerns\HasNavigation; |
8
|
|
|
use Sco\Admin\Contracts\ComponentInterface; |
9
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
10
|
|
|
use Sco\Admin\Exceptions\BadMethodCallException; |
11
|
|
|
|
12
|
|
|
abstract class Component implements ComponentInterface |
13
|
|
|
{ |
14
|
|
|
use HasEvents, |
15
|
|
|
HasNavigation; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
protected $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Illuminate\Foundation\Application |
24
|
|
|
*/ |
25
|
|
|
protected $app; |
26
|
|
|
|
27
|
|
|
protected $title; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
31
|
|
|
*/ |
32
|
|
|
protected $repository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
36
|
|
|
*/ |
37
|
|
|
protected $model; |
38
|
|
|
|
39
|
|
|
protected static $booted = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
43
|
|
|
*/ |
44
|
|
|
protected static $dispatcher; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $permissionObserver; |
50
|
|
|
|
51
|
|
|
protected $permissions; |
52
|
|
|
|
53
|
|
|
protected $permissionMethods = [ |
54
|
|
|
'view', 'create', 'edit', |
55
|
|
|
'delete', 'destroy', 'restore', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
public function __construct(Application $app, $modelClass) |
59
|
|
|
{ |
60
|
|
|
$this->app = $app; |
61
|
|
|
|
62
|
|
|
$this->repository = $this->app->make(RepositoryInterface::class); |
63
|
|
|
$this->repository->setClass($modelClass); |
64
|
|
|
|
65
|
|
|
$this->model = $this->repository->getModel(); |
66
|
|
|
if (!$this->name) { |
67
|
|
|
$this->setDefaultName(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->registerObserver($this->permissionObserver); |
71
|
|
|
|
72
|
|
|
$this->bootIfNotBooted(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function setDefaultName() |
76
|
|
|
{ |
77
|
|
|
$this->name = $this->getModelClassName(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function getModelClassName() |
81
|
|
|
{ |
82
|
|
|
return snake_case(str_plural(class_basename(get_class($this->getModel())))); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getName() |
86
|
|
|
{ |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getTitle() |
91
|
|
|
{ |
92
|
|
|
return $this->title; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getModel() |
96
|
|
|
{ |
97
|
|
|
return $this->model; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getRepository() |
101
|
|
|
{ |
102
|
|
|
return $this->repository; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function get() |
106
|
|
|
{ |
107
|
|
|
$view = $this->fireView(); |
108
|
|
|
|
109
|
|
|
$this->getRepository(); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$view->setRepository($this->getRepository()); |
112
|
|
|
|
113
|
|
|
return $view->get(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function getConfigs() |
121
|
|
|
{ |
122
|
|
|
return collect([ |
123
|
|
|
'primaryKey' => $this->getModel()->getKeyName(), |
124
|
|
|
'title' => $this->getTitle(), |
125
|
|
|
'permissions' => $this->getPermissions(), |
126
|
|
|
'view' => $this->fireView(), |
127
|
|
|
//'elements' => $this->getElements()->values(), |
|
|
|
|
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function fireView() |
135
|
|
|
{ |
136
|
|
|
if (!method_exists($this, 'callView')) { |
137
|
|
|
throw new BadMethodCallException('Not Found Method "callView"'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$view = $this->app->call([$this, 'callView']); |
141
|
|
|
return $view; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function fireCreate() |
148
|
|
|
{ |
149
|
|
|
if (!method_exists($this, 'callCreate')) { |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$form = $this->app->call([$this, 'callCreate']); |
154
|
|
|
|
155
|
|
|
return $form; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function fireEdit($id) |
162
|
|
|
{ |
163
|
|
|
if (!method_exists($this, 'callEdit')) { |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$form = $this->app->call([$this, 'callEdit'], ['id' => $id]); |
168
|
|
|
|
169
|
|
|
return $form; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
protected function bootIfNotBooted() |
174
|
|
|
{ |
175
|
|
|
if (!isset(static::$booted[static::class])) { |
176
|
|
|
static::$booted[static::class] = true; |
177
|
|
|
|
178
|
|
|
$this->fireEvent('booting', false); |
179
|
|
|
|
180
|
|
|
$this->boot(); |
|
|
|
|
181
|
|
|
|
182
|
|
|
$this->fireEvent('booted', false); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function boot() |
187
|
|
|
{ |
188
|
|
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function isView() |
192
|
|
|
{ |
193
|
|
|
return method_exists($this, 'callView') && $this->can('view'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function isCreate() |
197
|
|
|
{ |
198
|
|
|
return method_exists($this, 'callCreate') && $this->can('create'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function isEdit() |
202
|
|
|
{ |
203
|
|
|
return method_exists($this, 'callEdit') && $this->can('edit'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function isDelete() |
207
|
|
|
{ |
208
|
|
|
return $this->can('delete'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function isDestroy() |
212
|
|
|
{ |
213
|
|
|
return $this->isRestorableModel() && $this->can('destroy'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function isRestore() |
217
|
|
|
{ |
218
|
|
|
return $this->isRestorableModel() && $this->can('restore'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
protected function isRestorableModel() |
222
|
|
|
{ |
223
|
|
|
return $this->getRepository()->isRestorable(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function registerObserver($class = null) |
227
|
|
|
{ |
228
|
|
|
if (!$class) { |
229
|
|
|
return; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$className = is_string($class) ? $class : get_class($class); |
233
|
|
|
|
234
|
|
|
foreach ($this->permissionMethods as $method) { |
235
|
|
|
if (method_exists($class, $method)) { |
236
|
|
|
$this->registerPermission($method, |
237
|
|
|
[$this->app->make($className), $method]); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function registerPermission($permission, $callback) |
243
|
|
|
{ |
244
|
|
|
$this->permissions[$permission] = $callback; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function can($permission) |
248
|
|
|
{ |
249
|
|
|
if (is_callable($this->permissions[$permission])) { |
250
|
|
|
return call_user_func_array($this->permissions[$permission], [$this]); |
251
|
|
|
} |
252
|
|
|
return $this->permissions[$permission] ? true : false; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function getPermissions() |
256
|
|
|
{ |
257
|
|
|
$data = collect(); |
258
|
|
|
foreach ($this->permissionMethods as $perm) { |
259
|
|
|
$method = 'is' . ucfirst($perm); |
260
|
|
|
if (!method_exists($this, $method)) { |
261
|
|
|
$method = 'can'; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$data->put($perm, $this->{$method}($perm)); |
265
|
|
|
} |
266
|
|
|
return $data; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: