Complex classes like Admin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Admin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Admin |
||
20 | { |
||
21 | use HasAssets; |
||
22 | |||
23 | /** |
||
24 | * The Laravel admin version. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | const VERSION = '1.7.0'; |
||
29 | |||
30 | /** |
||
31 | * @var Navbar |
||
32 | */ |
||
33 | protected $navbar; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $menu = []; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | public static $metaTitle; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | public static $favicon; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | public static $extensions = []; |
||
54 | |||
55 | /** |
||
56 | * @var []Closure |
||
57 | */ |
||
58 | protected static $bootingCallbacks = []; |
||
59 | |||
60 | /** |
||
61 | * @var []Closure |
||
62 | */ |
||
63 | protected static $bootedCallbacks = []; |
||
64 | |||
65 | /** |
||
66 | * Returns the long version of Laravel-admin. |
||
67 | * |
||
68 | * @return string The long application version |
||
69 | */ |
||
70 | public static function getLongVersion() |
||
74 | |||
75 | /** |
||
76 | * @param $model |
||
77 | * @param Closure $callable |
||
78 | * |
||
79 | * @return \Encore\Admin\Grid |
||
80 | * |
||
81 | * @deprecated since v1.6.1 |
||
82 | */ |
||
83 | public function grid($model, Closure $callable) |
||
87 | |||
88 | /** |
||
89 | * @param $model |
||
90 | * @param Closure $callable |
||
91 | * |
||
92 | * @return \Encore\Admin\Form |
||
93 | * |
||
94 | * @deprecated since v1.6.1 |
||
95 | */ |
||
96 | public function form($model, Closure $callable) |
||
100 | |||
101 | /** |
||
102 | * Build a tree. |
||
103 | * |
||
104 | * @param $model |
||
105 | * @param Closure|null $callable |
||
106 | * |
||
107 | * @return \Encore\Admin\Tree |
||
108 | */ |
||
109 | public function tree($model, Closure $callable = null) |
||
110 | { |
||
111 | return new Tree($this->getModel($model), $callable); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Build show page. |
||
116 | * |
||
117 | * @param $model |
||
118 | * @param mixed $callable |
||
119 | * |
||
120 | * @return Show |
||
121 | * |
||
122 | * @deprecated since v1.6.1 |
||
123 | */ |
||
124 | public function show($model, $callable = null) |
||
125 | { |
||
126 | return new Show($this->getModel($model), $callable); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param Closure $callable |
||
131 | * |
||
132 | * @return \Encore\Admin\Layout\Content |
||
133 | * |
||
134 | * @deprecated since v1.6.1 |
||
135 | */ |
||
136 | public function content(Closure $callable = null) |
||
137 | { |
||
138 | return new Content($callable); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param $model |
||
143 | * |
||
144 | * @return mixed |
||
145 | */ |
||
146 | public function getModel($model) |
||
147 | { |
||
148 | if ($model instanceof Model) { |
||
149 | return $model; |
||
150 | } |
||
151 | |||
152 | if (is_string($model) && class_exists($model)) { |
||
153 | return $this->getModel(new $model()); |
||
154 | } |
||
155 | |||
156 | throw new InvalidArgumentException("$model is not a valid model"); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Left sider-bar menu. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function menu() |
||
165 | { |
||
166 | if (!empty($this->menu)) { |
||
167 | return $this->menu; |
||
168 | } |
||
169 | |||
170 | $menuClass = config('admin.database.menu_model'); |
||
171 | |||
172 | /** @var Menu $menuModel */ |
||
173 | $menuModel = new $menuClass(); |
||
174 | |||
175 | return $this->menu = $menuModel->toTree(); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param array $menu |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | public function menuLinks($menu = []) |
||
184 | { |
||
185 | if (empty($menu)) { |
||
186 | $menu = $this->menu(); |
||
187 | } |
||
188 | |||
189 | $links = []; |
||
190 | |||
191 | foreach ($menu as $item) { |
||
192 | if (!empty($item['children'])) { |
||
193 | $links = array_merge($links, $this->menuLinks($item['children'])); |
||
194 | } else { |
||
195 | $links[] = Arr::only($item, ['title', 'uri', 'icon']); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | return $links; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Set admin title. |
||
204 | * |
||
205 | * @param string $title |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | public static function setTitle($title) |
||
210 | { |
||
211 | self::$metaTitle = $title; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get admin title. |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | public function title() |
||
220 | { |
||
221 | return self::$metaTitle ? self::$metaTitle : config('admin.title'); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param null|string $favicon |
||
226 | * |
||
227 | * @return string|void |
||
228 | */ |
||
229 | public function favicon($favicon = null) |
||
230 | { |
||
231 | if (is_null($favicon)) { |
||
232 | return static::$favicon; |
||
233 | } |
||
234 | |||
235 | static::$favicon = $favicon; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Get the currently authenticated user. |
||
240 | * |
||
241 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
242 | */ |
||
243 | public function user() |
||
247 | |||
248 | /** |
||
249 | * Attempt to get the guard from the local cache. |
||
250 | * |
||
251 | * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard |
||
252 | */ |
||
253 | public function guard() |
||
254 | { |
||
255 | $guard = config('admin.auth.guard') ?: 'admin'; |
||
256 | |||
259 | |||
260 | /** |
||
261 | * Set navbar. |
||
262 | * |
||
263 | * @param Closure|null $builder |
||
264 | * |
||
265 | * @return Navbar |
||
266 | */ |
||
267 | public function navbar(Closure $builder = null) |
||
275 | |||
276 | /** |
||
277 | * Get navbar object. |
||
278 | * |
||
279 | * @return \Encore\Admin\Widgets\Navbar |
||
280 | */ |
||
281 | public function getNavbar() |
||
289 | |||
290 | /** |
||
291 | * Register the laravel-admin builtin routes. |
||
292 | * |
||
293 | * @return void |
||
294 | * |
||
295 | * @deprecated Use Admin::routes() instead(); |
||
296 | */ |
||
297 | public function registerAuthRoutes() |
||
301 | |||
302 | /** |
||
303 | * Register the laravel-admin builtin routes. |
||
304 | * |
||
305 | * @return void |
||
306 | */ |
||
307 | public function routes() |
||
339 | |||
340 | /** |
||
341 | * Extend a extension. |
||
342 | * |
||
343 | * @param string $name |
||
344 | * @param string $class |
||
345 | * |
||
346 | * @return void |
||
347 | */ |
||
348 | public static function extend($name, $class) |
||
352 | |||
353 | /** |
||
354 | * @param callable $callback |
||
355 | */ |
||
356 | public static function booting(callable $callback) |
||
360 | |||
361 | /** |
||
362 | * @param callable $callback |
||
363 | */ |
||
364 | public static function booted(callable $callback) |
||
368 | |||
369 | /** |
||
370 | * Bootstrap the admin application. |
||
371 | */ |
||
372 | public function bootstrap() |
||
391 | |||
392 | /** |
||
393 | * Call the booting callbacks for the admin application. |
||
394 | */ |
||
395 | protected function fireBootingCallbacks() |
||
401 | |||
402 | /** |
||
403 | * Call the booted callbacks for the admin application. |
||
404 | */ |
||
405 | protected function fireBootedCallbacks() |
||
411 | |||
412 | /* |
||
413 | * Disable Pjax for current Request |
||
414 | * |
||
415 | * @return void |
||
416 | */ |
||
417 | public function disablePjax() |
||
423 | } |
||
424 |