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\Navigation\Badge; |
11
|
|
|
use Sco\Admin\Navigation\Page; |
12
|
|
|
|
13
|
|
|
abstract class Component implements ComponentInterface |
14
|
|
|
{ |
15
|
|
|
use HasEvents, HasPermission; |
16
|
|
|
|
17
|
|
|
protected $app; |
18
|
|
|
|
19
|
|
|
protected $title; |
20
|
|
|
|
21
|
|
|
protected $booted = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
25
|
|
|
*/ |
26
|
|
|
protected static $dispatcher; |
27
|
|
|
|
28
|
|
|
public function __construct(Application $app) |
29
|
|
|
{ |
30
|
|
|
$this->app = $app; |
31
|
|
|
|
32
|
|
|
$this->bootIfNotBooted(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getTitle() |
36
|
|
|
{ |
37
|
|
|
return $this->title; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function getConfigs() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
return collect([ |
46
|
|
|
'primaryKey' => $this->getModel()->getRepository()->getKeyName(), |
|
|
|
|
47
|
|
|
'title' => $this->getTitle(), |
48
|
|
|
'permissions' => $this->getPermissions(), |
49
|
|
|
'columns' => $this->getColumns()->values(), |
|
|
|
|
50
|
|
|
'elements' => $this->getElements()->values(), |
|
|
|
|
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return \KodiComponents\Navigation\Contracts\NavigationInterface |
56
|
|
|
*/ |
57
|
|
|
public function getNavigation() |
58
|
|
|
{ |
59
|
|
|
return $this->app['admin.navigation']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 添加菜单 |
64
|
|
|
* |
65
|
|
|
* @param int $priority |
66
|
|
|
* @param null $badge |
67
|
|
|
* |
68
|
|
|
* @return \Sco\Admin\Navigation\Page |
69
|
|
|
*/ |
70
|
|
|
public function addToNavigation($priority = 100, $badge = null) |
71
|
|
|
{ |
72
|
|
|
$page = $this->makePage($priority, $badge); |
73
|
|
|
$this->getNavigation()->addPage($page); |
74
|
|
|
return $page; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* page |
79
|
|
|
* |
80
|
|
|
* @param int $priority |
81
|
|
|
* @param null $badge |
82
|
|
|
* |
83
|
|
|
* @return \Sco\Admin\Navigation\Page |
84
|
|
|
*/ |
85
|
|
|
protected function makePage($priority = 100, $badge = null) |
86
|
|
|
{ |
87
|
|
|
$page = new Page($this->getTitle()); |
88
|
|
|
$page->setPriority($priority); |
89
|
|
|
if ($badge) { |
90
|
|
|
if (!($badge instanceof BadgeInterface)) { |
91
|
|
|
$badge = new Badge($badge); |
92
|
|
|
} |
93
|
|
|
$page->addBadge($badge); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $page; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function bootIfNotBooted() |
100
|
|
|
{ |
101
|
|
|
if ($this->booted) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->fireEvent('booting', false); |
106
|
|
|
|
107
|
|
|
$this->bootTraits(); |
108
|
|
|
|
109
|
|
|
$this->boot(); |
110
|
|
|
|
111
|
|
|
$this->fireEvent('booted', false); |
112
|
|
|
|
113
|
|
|
$this->booted = true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function boot() |
117
|
|
|
{ |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Boot all of the bootable traits on the model. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
protected function bootTraits() |
126
|
|
|
{ |
127
|
|
|
$class = get_class($this); |
128
|
|
|
|
129
|
|
|
foreach (class_uses_recursive($class) as $trait) { |
130
|
|
|
if (method_exists($class, $method = 'boot' . class_basename($trait))) { |
131
|
|
|
forward_static_call([$class, $method]); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.