Total Complexity | 88 |
Total Lines | 429 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CRUDBooster 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.
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 CRUDBooster, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CRUDBooster |
||
18 | { |
||
19 | use PrivilegeHelpers; |
||
20 | |||
21 | public static function get($table, $string_conditions = null, $orderby = null, $limit = null, $skip = null) |
||
22 | { |
||
23 | $table = self::parseSqlTable($table); |
||
24 | $table = $table['table']; |
||
25 | $query = DB::table($table); |
||
26 | if ($string_conditions) { |
||
27 | $query->whereraw($string_conditions); |
||
28 | } |
||
29 | if ($orderby) { |
||
30 | $query->orderbyraw($orderby); |
||
31 | } |
||
32 | if ($limit) { |
||
33 | $query->take($limit); |
||
34 | } |
||
35 | if ($skip) { |
||
36 | $query->skip($skip); |
||
37 | } |
||
38 | |||
39 | return $query->get(); |
||
40 | } |
||
41 | |||
42 | public static function parseSqlTable($table) |
||
43 | { |
||
44 | $f = explode('.', $table); |
||
45 | |||
46 | if (count($f) == 1) { |
||
47 | return ["table" => $f[0], "database" => cbConfig('MAIN_DB_DATABASE')]; |
||
48 | } |
||
49 | if (count($f) == 2) { |
||
50 | return ["database" => $f[0], "table" => $f[1]]; |
||
51 | } |
||
52 | |||
53 | if (count($f) == 3) { |
||
54 | return ["table" => $f[0], "schema" => $f[1], "table" => $f[2]]; |
||
55 | } |
||
56 | |||
57 | return false; |
||
58 | } |
||
59 | |||
60 | public static function me() |
||
61 | { |
||
62 | return CbUsersRepo::find(session('admin_id')); |
||
63 | } |
||
64 | |||
65 | public static function myName() |
||
66 | { |
||
67 | return session('admin_name'); |
||
68 | } |
||
69 | |||
70 | public static function myPhoto() |
||
71 | { |
||
72 | return session('admin_photo'); |
||
73 | } |
||
74 | |||
75 | public static function isLocked() |
||
76 | { |
||
77 | return session('admin_lock'); |
||
78 | } |
||
79 | |||
80 | public static function getCurrentModule() |
||
81 | { |
||
82 | return GetCurrentX::getCurrentModule(); |
||
83 | } |
||
84 | |||
85 | public static function getCurrentDashboardId() |
||
86 | { |
||
87 | return GetCurrentX::getCurrentDashboardId(); |
||
88 | } |
||
89 | |||
90 | public static function getCurrentMenuId() |
||
91 | { |
||
92 | return GetCurrentX::getCurrentMenuId(); |
||
93 | } |
||
94 | |||
95 | public static function adminPath($path = null) |
||
96 | { |
||
97 | return url(cbAdminPath().'/'.$path); |
||
98 | } |
||
99 | |||
100 | public static function deleteConfirm($redirectTo) |
||
112 | } |
||
113 | |||
114 | public static function getCurrentId() |
||
115 | { |
||
116 | return GetCurrentX::getCurrentId(); |
||
117 | } |
||
118 | |||
119 | public static function getCurrentMethod() |
||
120 | { |
||
121 | return GetCurrentX::getCurrentMethod(); |
||
122 | } |
||
123 | |||
124 | public static function isColumnNULL($table, $field) |
||
125 | { |
||
126 | return DbInspector::isColNull($table, $field); |
||
127 | } |
||
128 | |||
129 | public static function getValueFilter($field) |
||
130 | { |
||
131 | $filter = request('filter_column'); |
||
132 | if ($filter[$field]) { |
||
133 | return $filter[$field]['value']; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | private static function getFilter($field, $index) |
||
138 | { |
||
139 | $filter = request('filter_column'); |
||
140 | if ($filter[$field]) { |
||
141 | return $filter[$field][$index]; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | public static function getSortingFilter($field) |
||
146 | { |
||
147 | return self::getFilter($field, 'sorting'); |
||
148 | } |
||
149 | |||
150 | public static function getTypeFilter($field) |
||
153 | } |
||
154 | |||
155 | public static function stringBetween($string, $start, $end) |
||
156 | { |
||
157 | $string = ' '.$string; |
||
158 | $ini = strpos($string, $start); |
||
159 | if ($ini == 0) { |
||
160 | return ''; |
||
161 | } |
||
162 | $ini += strlen($start); |
||
163 | $len = strpos($string, $end, $ini) - $ini; |
||
164 | |||
165 | return substr($string, $ini, $len); |
||
166 | } |
||
167 | |||
168 | public static function first($table, $id) |
||
169 | { |
||
170 | $table = self::parseSqlTable($table)['table']; |
||
171 | if (! is_array($id)) { |
||
172 | $pk = self::pk($table); |
||
173 | |||
174 | return DB::table($table)->where($pk, $id)->first(); |
||
175 | } |
||
176 | |||
177 | $first = DB::table($table); |
||
178 | foreach ($id as $k => $v) { |
||
179 | $first->where($k, $v); |
||
180 | } |
||
181 | |||
182 | return $first->first(); |
||
183 | } |
||
184 | |||
185 | public static function pk($table) |
||
188 | } |
||
189 | |||
190 | public static function valid($rules = [], $type = 'json') |
||
191 | { |
||
192 | $validator = Validator::make(request()->all(), $rules); |
||
193 | |||
194 | if (!$validator->fails()) { |
||
195 | return true; |
||
196 | } |
||
197 | |||
198 | $message = $validator->errors()->all(); |
||
199 | |||
200 | if ($type == 'json') { |
||
201 | $result = []; |
||
202 | $result['api_status'] = 0; |
||
203 | $result['api_message'] = implode(', ', $message); |
||
204 | sendAndTerminate(response()->json($result, 200)); |
||
205 | } |
||
206 | |||
207 | $res = redirect()->back()->with(['message' => implode('<br/>', $message), 'message_type' => 'warning'])->withInput(); |
||
208 | sendAndTerminate($res); |
||
209 | } |
||
210 | |||
211 | public static function getForeignKey($parent_table, $child_table) |
||
212 | { |
||
213 | $parent_table = CRUDBooster::parseSqlTable($parent_table)['table']; |
||
214 | $child_table = CRUDBooster::parseSqlTable($child_table)['table']; |
||
215 | |||
216 | if (\Schema::hasColumn($child_table, 'id_'.$parent_table)) { |
||
217 | return 'id_'.$parent_table; |
||
218 | } |
||
219 | return $parent_table.'_id'; |
||
220 | } |
||
221 | |||
222 | public static function getTableForeignKey($fieldName) |
||
223 | { |
||
224 | if (substr($fieldName, 0, 3) == 'id_' || substr($fieldName, -3) == '_id') { |
||
225 | return str_replace(['_id', 'id_'], '', $fieldName); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | public static function isForeignKey($fieldName) |
||
230 | { |
||
231 | return DbInspector::isForeignKeey($fieldName); |
||
232 | } |
||
233 | |||
234 | public static function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
||
235 | { |
||
236 | $params = request()->all(); |
||
237 | $mainpath = trim(self::mainpath(), '/'); |
||
238 | |||
239 | if ($params['filter_column'] && $singleSorting) { |
||
240 | foreach ($params['filter_column'] as $k => $filter) { |
||
241 | foreach ($filter as $t => $val) { |
||
242 | if ($t == 'sorting') { |
||
243 | unset($params['filter_column'][$k]['sorting']); |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | |||
249 | $params['filter_column'][$key][$type] = $value; |
||
250 | |||
251 | if (isset($params)) { |
||
252 | return $mainpath.'?'.http_build_query($params); |
||
253 | } |
||
254 | return $mainpath.'?filter_column['.$key.']['.$type.']='.$value; |
||
255 | |||
256 | } |
||
257 | |||
258 | public static function mainpath($path = null) |
||
259 | { |
||
260 | $controllerName = strtok(Route::currentRouteAction(), '@'); |
||
261 | // $controllerName = array_pop(explode('\\', $controllerName)); |
||
262 | $controllerName = basename($controllerName); |
||
263 | $route_url = route($controllerName.'GetIndex'); |
||
264 | |||
265 | if (! $path) { |
||
266 | return trim($route_url, '/'); |
||
267 | } |
||
268 | |||
269 | if (substr($path, 0, 1) == '?') { |
||
270 | return trim($route_url, '/').$path; |
||
271 | } |
||
272 | |||
273 | return $route_url.'/'.$path; |
||
274 | } |
||
275 | |||
276 | public static function insertLog($description) |
||
279 | } |
||
280 | |||
281 | public static function insertTryLog($action, $name = '') |
||
282 | { |
||
283 | self::insertLog(trans("logging.log_try_".$action, ['name' => $name, 'module' => self::getCurrentModule()])); |
||
284 | } |
||
285 | |||
286 | public static function myId() |
||
287 | { |
||
288 | return session('admin_id'); |
||
289 | } |
||
290 | |||
291 | public static function referer() |
||
292 | { |
||
293 | return Request::server('HTTP_REFERER'); |
||
294 | } |
||
295 | |||
296 | public static function listTables() |
||
297 | { |
||
298 | return DbInspector::listTables(); |
||
299 | } |
||
300 | |||
301 | public static function listCbTables() |
||
302 | { |
||
303 | $tablesList = []; |
||
304 | foreach (self::listTables() as $tableObj) { |
||
305 | |||
306 | $tableName = $tableObj->TABLE_NAME; |
||
307 | if ($tableName == config('database.migrations')) { |
||
308 | continue; |
||
309 | } |
||
310 | if (substr($tableName, 0, 4) == 'cms_' && $tableName != 'cms_users') { |
||
311 | continue; |
||
312 | } |
||
313 | |||
314 | $tablesList[] = $tableName; |
||
315 | } |
||
316 | |||
317 | return $tablesList; |
||
318 | } |
||
319 | |||
320 | public static function getUrlParameters($exception = null) |
||
343 | } |
||
344 | |||
345 | public static function isExistsController($table) |
||
346 | { |
||
347 | $ctrlName = ucwords(str_replace('_', ' ', $table)); |
||
348 | $ctrlName = str_replace(' ', '', $ctrlName).'Controller.php'; |
||
349 | $path = base_path(controllers_dir()); |
||
350 | $path2 = base_path(controllers_dir()."ControllerMaster/"); |
||
351 | |||
352 | if (file_exists($path.'Admin'.$ctrlName) || file_exists($path2.'Admin'.$ctrlName) || file_exists($path2.$ctrlName)) { |
||
353 | return true; |
||
354 | } |
||
355 | |||
356 | return false; |
||
357 | } |
||
358 | |||
359 | public static function getTableColumns($table) |
||
360 | { |
||
361 | return DbInspector::getTableCols($table); |
||
362 | } |
||
363 | |||
364 | public static function getNameTable($columns) |
||
365 | { |
||
366 | return DbInspector::colName($columns); |
||
367 | } |
||
368 | |||
369 | public static function routeController($prefix, $controller, $namespace = null) |
||
370 | { |
||
371 | $prefix = trim($prefix, '/').'/'; |
||
372 | |||
373 | $namespace = ($namespace) ?: ctrlNamespace(); |
||
374 | |||
375 | try { |
||
376 | Route::get($prefix, ['uses' => $controller.'@getIndex', 'as' => $controller.'GetIndex']); |
||
377 | |||
378 | $controller_class = new \ReflectionClass($namespace.'\\'.$controller); |
||
379 | $controller_methods = $controller_class->getMethods(\ReflectionMethod::IS_PUBLIC); |
||
380 | $wildcards = '/{one?}/{two?}/{three?}/{four?}/{five?}'; |
||
381 | foreach ($controller_methods as $method) { |
||
382 | |||
383 | if ($method->class == 'Illuminate\Routing\Controller' || $method->name == 'getIndex') { |
||
384 | continue; |
||
385 | } |
||
386 | if (substr($method->name, 0, 3) == 'get') { |
||
387 | $method_name = substr($method->name, 3); |
||
388 | $slug = array_filter(preg_split('/(?=[A-Z])/', $method_name)); |
||
389 | $slug = strtolower(implode('-', $slug)); |
||
390 | $slug = ($slug == 'index') ? '' : $slug; |
||
391 | Route::get($prefix.$slug.$wildcards, ['uses' => $controller.'@'.$method->name, 'as' => $controller.'Get'.$method_name]); |
||
392 | } elseif (substr($method->name, 0, 4) == 'post') { |
||
393 | $method_name = substr($method->name, 4); |
||
394 | $slug = array_filter(preg_split('/(?=[A-Z])/', $method_name)); |
||
395 | Route::post($prefix.strtolower(implode('-', $slug)).$wildcards, [ |
||
396 | 'uses' => $controller.'@'.$method->name, |
||
397 | 'as' => $controller.'Post'.$method_name, |
||
398 | ]); |
||
399 | } |
||
400 | } |
||
401 | } catch (\Exception $e) { |
||
402 | |||
403 | } |
||
404 | } |
||
405 | |||
406 | /* |
||
407 | | ------------------------------------------------------------- |
||
408 | | Alternate route for Laravel Route::controller |
||
409 | | ------------------------------------------------------------- |
||
410 | | $prefix = path of route |
||
411 | | $controller = controller name |
||
412 | | $namespace = namespace of controller (optional) |
||
413 | | |
||
414 | */ |
||
415 | |||
416 | public static function denyAccess() |
||
417 | { |
||
418 | static::redirect(static::adminPath(), cbTrans('denied_access')); |
||
419 | } |
||
420 | |||
421 | public static function redirect($to, $message, $type = 'warning') |
||
428 | } |
||
429 | |||
430 | public static function icon($icon) |
||
431 | { |
||
432 | return '<i class=\'fa fa-'.$icon.'\'></i>'; |
||
433 | } |
||
434 | |||
435 | public static function componentsPath($type = '') |
||
436 | { |
||
437 | $componentPath = implode(DIRECTORY_SEPARATOR, ['vendor', 'crocodicstudio', 'crudbooster', 'src', 'views', 'default', 'type_components', $type]); |
||
438 | return base_path($componentPath); |
||
439 | |||
440 | } |
||
441 | |||
442 | public static function PublishedComponentsPath($type = '') |
||
446 | } |
||
447 | } |
||
448 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: