|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\CBCoreModule; |
|
4
|
|
|
|
|
5
|
|
|
use Route; |
|
6
|
|
|
|
|
7
|
|
|
class CbRouter |
|
8
|
|
|
{ |
|
9
|
|
|
public static function routeController($prefix, $controller, $namespace = null) |
|
10
|
|
|
{ |
|
11
|
|
|
$prefix = trim($prefix, '/').'/'; |
|
12
|
|
|
|
|
13
|
|
|
try { |
|
14
|
|
|
Route::get($prefix, ['uses' => $controller.'@getIndex', 'as' => $controller.'GetIndex']); |
|
15
|
|
|
$ctrl = self::getControllerPath($controller, $namespace); |
|
16
|
|
|
foreach (self::getControllerMethods($ctrl) as $method) { |
|
17
|
|
|
$wildcards = self::getWildCard($method->getNumberOfParameters()); |
|
18
|
|
|
self::setRoute($prefix, $controller, $method->name, $wildcards); |
|
19
|
|
|
} |
|
20
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param $prefix |
|
27
|
|
|
* @param $controller |
|
28
|
|
|
* @param $method |
|
29
|
|
|
* @param $wildcards |
|
30
|
|
|
*/ |
|
31
|
|
|
private static function routePost($prefix, $controller, $method, $wildcards) |
|
32
|
|
|
{ |
|
33
|
|
|
$methodName = str_after($method, 'post'); |
|
34
|
|
|
$slug = self::makeSlug($methodName); |
|
35
|
|
|
Route::post($prefix.$slug.$wildcards, $controller.'@'.$method) |
|
36
|
|
|
->name($controller.'Post'.$methodName); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $prefix |
|
41
|
|
|
* @param $controller |
|
42
|
|
|
* @param $method |
|
43
|
|
|
* @param $wildcards |
|
44
|
|
|
*/ |
|
45
|
|
|
private static function routeGet($prefix, $controller, $method, $wildcards) |
|
46
|
|
|
{ |
|
47
|
|
|
$methodName = str_after($method, 'get'); |
|
48
|
|
|
$slug = self::makeSlug($methodName); |
|
49
|
|
|
$slug = ($slug == 'index') ? '' : $slug; |
|
50
|
|
|
Route::get($prefix.$slug.$wildcards, $controller.'@'.$method) |
|
51
|
|
|
->name($controller.'Get'.$methodName); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param $ctrl |
|
56
|
|
|
* @return array|\ReflectionMethod[] |
|
57
|
|
|
* @throws \ReflectionException |
|
58
|
|
|
*/ |
|
59
|
|
|
private static function getControllerMethods($ctrl) |
|
60
|
|
|
{ |
|
61
|
|
|
$methods = (new \ReflectionClass($ctrl))->getMethods(\ReflectionMethod::IS_PUBLIC); |
|
62
|
|
|
|
|
63
|
|
|
return array_filter($methods, function ($method) { |
|
64
|
|
|
return ($method->class !== 'Illuminate\Routing\Controller' && $method->name !== 'getIndex'); |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $controller |
|
70
|
|
|
* @param $namespace |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
private static function getControllerPath($controller, $namespace) |
|
74
|
|
|
{ |
|
75
|
|
|
$ns = $namespace ?: ctrlNamespace(); |
|
76
|
|
|
$ctrl = $ns.'\\'.$controller; |
|
77
|
|
|
|
|
78
|
|
|
return $ctrl; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param $prefix |
|
83
|
|
|
* @param $controller |
|
84
|
|
|
* @param $methodName |
|
85
|
|
|
* @param $wildcards |
|
86
|
|
|
*/ |
|
87
|
|
|
private static function setRoute($prefix, $controller, $methodName, $wildcards) |
|
88
|
|
|
{ |
|
89
|
|
|
if (starts_with($methodName, 'get')) { |
|
90
|
|
|
self::routeGet($prefix, $controller, $methodName, $wildcards); |
|
91
|
|
|
} elseif (starts_with($methodName, 'post')) { |
|
92
|
|
|
self::routePost($prefix, $controller, $methodName, $wildcards); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $methodName |
|
98
|
|
|
* @return array|string |
|
99
|
|
|
*/ |
|
100
|
|
|
private static function makeSlug($methodName) |
|
101
|
|
|
{ |
|
102
|
|
|
$slug = preg_split('/(?=[A-Z])/', $methodName) ?: []; |
|
103
|
|
|
$slug = array_filter($slug); |
|
104
|
|
|
$slug = strtolower(implode('-', $slug)); |
|
105
|
|
|
|
|
106
|
|
|
return $slug; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private static function getWildCard($count) |
|
110
|
|
|
{ |
|
111
|
|
|
$wildcards = explode('/', '{one?}/{two?}/{three?}/{four?}/{five?}'); |
|
112
|
|
|
$fr = array_splice($wildcards, 0, $count); |
|
113
|
|
|
return implode('/', $fr); |
|
114
|
|
|
} |
|
115
|
|
|
} |