1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\ControlPanel; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Ui\ControlPanel\Command\BuildControlPanel; |
4
|
|
|
use Anomaly\Streams\Platform\Ui\ControlPanel\Component\Button\ButtonHandler; |
5
|
|
|
use Anomaly\Streams\Platform\Ui\ControlPanel\Component\Navigation\NavigationHandler; |
6
|
|
|
use Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section\SectionHandler; |
7
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ControlPanelBuilder |
11
|
|
|
* |
12
|
|
|
* @link http://pyrocms.com/ |
13
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
14
|
|
|
* @author Ryan Thompson <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ControlPanelBuilder |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
use DispatchesJobs; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The section buttons. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $buttons = ButtonHandler::class; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The module sections. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $sections = SectionHandler::class; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The navigation links. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $navigation = NavigationHandler::class; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The control_panel object. |
44
|
|
|
* |
45
|
|
|
* @var ControlPanel |
46
|
|
|
*/ |
47
|
|
|
protected $controlPanel; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a new ControlPanelBuilder instance. |
51
|
|
|
* |
52
|
|
|
* @param ControlPanel $controlPanel |
53
|
|
|
*/ |
54
|
|
|
public function __construct(ControlPanel $controlPanel) |
55
|
|
|
{ |
56
|
|
|
$this->controlPanel = $controlPanel; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Build the control_panel. |
61
|
|
|
*/ |
62
|
|
|
public function build() |
63
|
|
|
{ |
64
|
|
|
$this->dispatch(new BuildControlPanel($this)); |
65
|
|
|
|
66
|
|
|
return $this->controlPanel; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the control_panel. |
71
|
|
|
* |
72
|
|
|
* @return ControlPanel |
73
|
|
|
*/ |
74
|
|
|
public function getControlPanel() |
75
|
|
|
{ |
76
|
|
|
return $this->controlPanel; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the section buttons. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getButtons() |
85
|
|
|
{ |
86
|
|
|
return $this->buttons; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the section buttons. |
91
|
|
|
* |
92
|
|
|
* @param array $buttons |
93
|
|
|
*/ |
94
|
|
|
public function setButtons($buttons) |
95
|
|
|
{ |
96
|
|
|
$this->buttons = $buttons; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the module sections. |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getSections() |
105
|
|
|
{ |
106
|
|
|
return $this->sections; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the sections. |
111
|
|
|
* |
112
|
|
|
* @param array $sections |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function setSections($sections) |
116
|
|
|
{ |
117
|
|
|
$this->sections = $sections; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Add a section. |
124
|
|
|
* |
125
|
|
|
* @param $slug |
126
|
|
|
* @param array $section |
127
|
|
|
* @param null $position |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function addSection($slug, array $section, $position = null) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
if ($position === null) { |
133
|
|
|
$position = count($this->sections) + 1; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$front = array_slice($this->sections, 0, $position, true); |
137
|
|
|
$back = array_slice($this->sections, $position, count($this->sections) - $position, true); |
138
|
|
|
|
139
|
|
|
$this->sections = $front + [$slug => $section] + $back; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Add a section button. |
146
|
|
|
* |
147
|
|
|
* @param $section |
148
|
|
|
* @param $slug |
149
|
|
|
* @param array $button |
150
|
|
|
* @param null $position |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
public function addSectionButton($section, $slug, array $button, $position = null) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$buttons = (array)array_get($this->sections, "{$section}.buttons"); |
156
|
|
|
|
157
|
|
|
if ($position === null) { |
158
|
|
|
$position = count($buttons) + 1; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$front = array_slice($buttons, 0, $position, true); |
162
|
|
|
$back = array_slice($buttons, $position, count($buttons) - $position, true); |
163
|
|
|
|
164
|
|
|
$buttons = $front + [$slug => $button] + $back; |
165
|
|
|
|
166
|
|
|
array_set($this->sections, "{$section}.buttons", $buttons); |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the module navigation. |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
|
|
public function getNavigation() |
177
|
|
|
{ |
178
|
|
|
return $this->navigation; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Set the navigation. |
183
|
|
|
* |
184
|
|
|
* @param array $navigation |
185
|
|
|
*/ |
186
|
|
|
public function setNavigation($navigation) |
187
|
|
|
{ |
188
|
|
|
$this->navigation = $navigation; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Return the active control panel section. |
193
|
|
|
* |
194
|
|
|
* @return Component\Section\Contract\SectionInterface|null |
195
|
|
|
*/ |
196
|
|
|
public function getControlPanelActiveSection() |
197
|
|
|
{ |
198
|
|
|
$sections = $this->getControlPanelSections(); |
199
|
|
|
|
200
|
|
|
return $sections->active(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the control panel sections. |
205
|
|
|
* |
206
|
|
|
* @return Component\Section\SectionCollection |
207
|
|
|
*/ |
208
|
|
|
public function getControlPanelSections() |
209
|
|
|
{ |
210
|
|
|
return $this->controlPanel->getSections(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the control panel navigation. |
215
|
|
|
* |
216
|
|
|
* @return Component\Navigation\NavigationCollection |
217
|
|
|
*/ |
218
|
|
|
public function getControlPanelNavigation() |
219
|
|
|
{ |
220
|
|
|
return $this->controlPanel->getNavigation(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
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.