|
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\Component\Concerns\HasPermission; |
|
9
|
|
|
use Sco\Admin\Contracts\ComponentInterface; |
|
10
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
|
11
|
|
|
use Sco\Admin\Exceptions\BadMethodCallException; |
|
12
|
|
|
|
|
13
|
|
|
abstract class Component implements ComponentInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use HasEvents, |
|
16
|
|
|
HasPermission, |
|
17
|
|
|
HasNavigation; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $name; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Illuminate\Foundation\Application |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $app; |
|
28
|
|
|
|
|
29
|
|
|
protected $title; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $repository; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $model; |
|
40
|
|
|
|
|
41
|
|
|
protected static $booted = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
|
45
|
|
|
*/ |
|
46
|
|
|
protected static $dispatcher; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct(Application $app, $modelClass) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->app = $app; |
|
51
|
|
|
|
|
52
|
|
|
$this->repository = $this->app->make(RepositoryInterface::class); |
|
53
|
|
|
$this->repository->setClass($modelClass); |
|
54
|
|
|
|
|
55
|
|
|
$this->model = $this->repository->getModel(); |
|
56
|
|
|
if (!$this->name) { |
|
57
|
|
|
$this->setDefaultName(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->registerObserver($this->permissionObserver); |
|
61
|
|
|
|
|
62
|
|
|
$this->bootIfNotBooted(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function setDefaultName() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->name = $this->getModelClassName(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function getModelClassName() |
|
71
|
|
|
{ |
|
72
|
|
|
return snake_case(str_plural(class_basename(get_class($this->getModel())))); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getName() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->name; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getTitle() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->title; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getModel() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->model; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getRepository() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->repository; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function get() |
|
96
|
|
|
{ |
|
97
|
|
|
$view = $this->fireView(); |
|
98
|
|
|
|
|
99
|
|
|
$this->getRepository(); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
$view->setRepository($this->getRepository()); |
|
102
|
|
|
|
|
103
|
|
|
return $view->get(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getConfigs() |
|
111
|
|
|
{ |
|
112
|
|
|
return collect([ |
|
113
|
|
|
'primaryKey' => $this->getModel()->getKeyName(), |
|
114
|
|
|
'title' => $this->getTitle(), |
|
115
|
|
|
'permissions' => $this->getPermissions(), |
|
116
|
|
|
'view' => $this->fireView(), |
|
117
|
|
|
//'elements' => $this->getElements()->values(), |
|
|
|
|
|
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return \Sco\Admin\Contracts\ViewInterface |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function fireView() |
|
125
|
|
|
{ |
|
126
|
|
|
if (!method_exists($this, 'callView')) { |
|
127
|
|
|
throw new BadMethodCallException('Not Found Method "callView"'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$view = $this->app->call([$this, 'callView']); |
|
131
|
|
|
return $view; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
protected function bootIfNotBooted() |
|
135
|
|
|
{ |
|
136
|
|
|
if (!isset(static::$booted[static::class])) { |
|
137
|
|
|
static::$booted[static::class] = true; |
|
138
|
|
|
|
|
139
|
|
|
$this->fireEvent('booting', false); |
|
140
|
|
|
|
|
141
|
|
|
$this->boot(); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
$this->fireEvent('booted', false); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function boot() |
|
148
|
|
|
{ |
|
149
|
|
|
return true; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
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: