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 = null, $modelClass = null) |
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
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function getConfigs() |
84
|
|
|
{ |
85
|
|
|
return collect([ |
86
|
|
|
'primaryKey' => $this->getModel()->getKeyName(), |
87
|
|
|
'title' => $this->getTitle(), |
88
|
|
|
'permissions' => $this->getPermissions(), |
89
|
|
|
//'columns' => $this->getColumns()->values(), |
|
|
|
|
90
|
|
|
//'elements' => $this->getElements()->values(), |
|
|
|
|
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return \KodiComponents\Navigation\Contracts\NavigationInterface |
96
|
|
|
*/ |
97
|
|
|
public function getNavigation() |
98
|
|
|
{ |
99
|
|
|
return $this->app['admin.navigation']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 添加菜单 |
104
|
|
|
* |
105
|
|
|
* @param int $priority |
106
|
|
|
* @param null $badge |
107
|
|
|
* |
108
|
|
|
* @return \Sco\Admin\Navigation\Page |
109
|
|
|
*/ |
110
|
|
|
public function addToNavigation($priority = 100, $badge = null) |
111
|
|
|
{ |
112
|
|
|
$page = $this->makePage($priority, $badge); |
113
|
|
|
$this->getNavigation()->addPage($page); |
114
|
|
|
return $page; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* page |
119
|
|
|
* |
120
|
|
|
* @param int $priority |
121
|
|
|
* @param null $badge |
122
|
|
|
* |
123
|
|
|
* @return \Sco\Admin\Navigation\Page |
124
|
|
|
*/ |
125
|
|
|
protected function makePage($priority = 100, $badge = null) |
126
|
|
|
{ |
127
|
|
|
$page = new Page($this->getTitle()); |
128
|
|
|
$page->setPriority($priority); |
129
|
|
|
if ($badge) { |
130
|
|
|
if (!($badge instanceof BadgeInterface)) { |
131
|
|
|
$badge = new Badge($badge); |
132
|
|
|
} |
133
|
|
|
$page->addBadge($badge); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $page; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function bootIfNotBooted() |
140
|
|
|
{ |
141
|
|
|
if (!isset(static::$booted[static::class])) { |
142
|
|
|
static::$booted[static::class] = true; |
143
|
|
|
|
144
|
|
|
$this->fireEvent('booting', false); |
145
|
|
|
|
146
|
|
|
//static::bootTraits(); |
|
|
|
|
147
|
|
|
|
148
|
|
|
$this->boot(); |
149
|
|
|
|
150
|
|
|
$this->fireEvent('booted', false); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function boot() |
155
|
|
|
{ |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Boot all of the bootable traits on the model. |
160
|
|
|
* |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
protected static function bootTraits() |
164
|
|
|
{ |
165
|
|
|
$class = static::class; |
166
|
|
|
|
167
|
|
|
foreach (class_uses_recursive($class) as $trait) { |
168
|
|
|
if (method_exists($class, $method = 'boot' . class_basename($trait))) { |
169
|
|
|
forward_static_call([$class, $method]); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
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.