1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Component; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Application; |
6
|
|
|
use KodiComponents\Navigation\Contracts\BadgeInterface; |
7
|
|
|
use Sco\Admin\Component\Concerns\HasEvents; |
8
|
|
|
use Sco\Admin\Component\Concerns\HasPermission; |
9
|
|
|
use Sco\Admin\Contracts\ComponentInterface; |
10
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
11
|
|
|
use Sco\Admin\Navigation\Badge; |
12
|
|
|
use Sco\Admin\Navigation\Page; |
13
|
|
|
|
14
|
|
|
abstract class Component implements ComponentInterface |
15
|
|
|
{ |
16
|
|
|
use HasEvents, HasPermission; |
17
|
|
|
|
18
|
|
|
protected $name; |
19
|
|
|
|
20
|
|
|
protected $app; |
21
|
|
|
|
22
|
|
|
protected $title; |
23
|
|
|
|
24
|
|
|
protected $repository; |
25
|
|
|
|
26
|
|
|
protected $model; |
27
|
|
|
|
28
|
|
|
protected static $booted = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
32
|
|
|
*/ |
33
|
|
|
protected static $dispatcher; |
34
|
|
|
|
35
|
|
|
public function __construct(Application $app, $modelClass) |
36
|
|
|
{ |
37
|
|
|
$this->app = $app; |
38
|
|
|
|
39
|
|
|
$this->repository = $this->app->make(RepositoryInterface::class); |
40
|
|
|
$this->repository->setClass($modelClass); |
41
|
|
|
|
42
|
|
|
$this->model = $this->repository->getModel(); |
43
|
|
|
if (!$this->name) { |
44
|
|
|
$this->setDefaultName(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->bootIfNotBooted(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function setDefaultName() |
51
|
|
|
{ |
52
|
|
|
$this->name = $this->getModelClassName(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function getModelClassName() |
56
|
|
|
{ |
57
|
|
|
return snake_case(str_plural(class_basename(get_class($this->getModel())))); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getName() |
61
|
|
|
{ |
62
|
|
|
return $this->name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getTitle() |
66
|
|
|
{ |
67
|
|
|
return $this->title; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getModel() |
71
|
|
|
{ |
72
|
|
|
return $this->model; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getRepository() |
76
|
|
|
{ |
77
|
|
|
return $this->repository; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function get() |
81
|
|
|
{ |
82
|
|
|
if (!method_exists($this, 'callView')) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$view = $this->app->call([$this, 'callView']); |
|
|
|
|
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getConfigs() |
94
|
|
|
{ |
95
|
|
|
return collect([ |
96
|
|
|
'primaryKey' => $this->getModel()->getKeyName(), |
97
|
|
|
'title' => $this->getTitle(), |
98
|
|
|
'permissions' => $this->getPermissions(), |
99
|
|
|
//'columns' => $this->getColumns()->values(), |
|
|
|
|
100
|
|
|
//'elements' => $this->getElements()->values(), |
|
|
|
|
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return \KodiComponents\Navigation\Contracts\NavigationInterface |
106
|
|
|
*/ |
107
|
|
|
public function getNavigation() |
108
|
|
|
{ |
109
|
|
|
return $this->app['admin.navigation']; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* 添加菜单 |
114
|
|
|
* |
115
|
|
|
* @param int $priority |
116
|
|
|
* @param null $badge |
117
|
|
|
* |
118
|
|
|
* @return \Sco\Admin\Navigation\Page |
119
|
|
|
*/ |
120
|
|
|
public function addToNavigation($priority = 100, $badge = null) |
121
|
|
|
{ |
122
|
|
|
$page = $this->makePage($priority, $badge); |
123
|
|
|
$this->getNavigation()->addPage($page); |
124
|
|
|
return $page; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* page |
129
|
|
|
* |
130
|
|
|
* @param int $priority |
131
|
|
|
* @param null $badge |
132
|
|
|
* |
133
|
|
|
* @return \Sco\Admin\Navigation\Page |
134
|
|
|
*/ |
135
|
|
|
protected function makePage($priority = 100, $badge = null) |
136
|
|
|
{ |
137
|
|
|
$page = new Page($this->getTitle()); |
138
|
|
|
$page->setPriority($priority); |
139
|
|
|
if ($badge) { |
140
|
|
|
if (!($badge instanceof BadgeInterface)) { |
141
|
|
|
$badge = new Badge($badge); |
142
|
|
|
} |
143
|
|
|
$page->addBadge($badge); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $page; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function bootIfNotBooted() |
150
|
|
|
{ |
151
|
|
|
if (!isset(static::$booted[static::class])) { |
152
|
|
|
static::$booted[static::class] = true; |
153
|
|
|
|
154
|
|
|
$this->fireEvent('booting', false); |
155
|
|
|
|
156
|
|
|
$this->boot(); |
|
|
|
|
157
|
|
|
|
158
|
|
|
$this->fireEvent('booted', false); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function boot() |
163
|
|
|
{ |
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.