|
@@ -2,65 +2,99 @@
|
2
|
2
|
|
3
|
3
|
namespace Luticate\Utils;
|
4
|
4
|
|
5
|
|
-use Illuminate\Http\Request;
|
6
|
|
-
|
7
|
5
|
class LuRoute {
|
8
|
6
|
/**
|
9
|
|
- * @var callable
|
|
7
|
+ * @var string[]
|
10
|
8
|
*/
|
11
|
|
- private $_middleware;
|
|
9
|
+ private $middleware = array();
|
12
|
10
|
|
13
|
11
|
/**
|
14
|
|
- * @param $middleware callable
|
|
12
|
+ * @param $middleware string
|
15
|
13
|
*/
|
16
|
|
- public function setMiddleware($middleware)
|
|
14
|
+ public function addMiddleware($middleware)
|
17
|
15
|
{
|
18
|
|
- $this->_middleware = $middleware;
|
|
16
|
+ $this->middleware[] = $middleware;
|
19
|
17
|
}
|
20
|
18
|
|
21
|
|
- private function getOptions($url, $business, $method, $permissions = array())
|
|
19
|
+ private function getOptions($url, $business, $method, $permissions, $middleware)
|
22
|
20
|
{
|
23
|
|
- $middleware = $this->_middleware;
|
24
|
|
- return [function(Request $request) use($business, $method, $permissions, $middleware)
|
|
21
|
+ if (!is_array($permissions)) {
|
|
22
|
+ $permissions = array($permissions);
|
|
23
|
+ }
|
|
24
|
+ if (!is_array($middleware)) {
|
|
25
|
+ $middleware = array($middleware);
|
|
26
|
+ }
|
|
27
|
+ $permissions_string = implode(",", $permissions);
|
|
28
|
+ $middleware_string = [];
|
|
29
|
+ foreach (array_merge($this->middleware, $middleware) as $mid) {
|
|
30
|
+ $middleware_string[] = $mid . ":" . $permissions_string;
|
|
31
|
+ }
|
|
32
|
+ return [function() use($business, $method, $permissions)
|
25
|
33
|
{
|
26
|
|
- if (!is_array($permissions)) {
|
27
|
|
- $permissions = array($permissions);
|
28
|
|
- }
|
29
|
|
-
|
30
|
|
- if ($middleware != null && !$middleware($permissions, $request)) {
|
31
|
|
- abort(401);
|
32
|
|
- }
|
33
|
|
-
|
34
|
34
|
$controller = new LuController();
|
35
|
35
|
|
36
|
|
- if (strpos($business, "\\") === false)
|
|
36
|
+ if (strpos($business, "\\") === false) {
|
37
|
37
|
$business = "App\\Http\\Business\\" . $business . "Business";
|
|
38
|
+ }
|
38
|
39
|
|
39
|
40
|
return $controller->execute($business, $method);
|
40
|
|
- }];
|
|
41
|
+ },
|
|
42
|
+ 'middleware' => $middleware_string];
|
41
|
43
|
}
|
42
|
44
|
|
43
|
|
- public function get($url, $business, $method, $permissions = array())
|
|
45
|
+ /**
|
|
46
|
+ * @param $url string
|
|
47
|
+ * @param $business string
|
|
48
|
+ * @param $method string
|
|
49
|
+ * @param array $permissions string|string[]
|
|
50
|
+ * @param array $middleware string|string[]
|
|
51
|
+ * @return mixed
|
|
52
|
+ */
|
|
53
|
+ public function get($url, $business, $method, $permissions = array(), $middleware = array())
|
44
|
54
|
{
|
45
|
55
|
global $app;
|
46
|
|
- return $app->get($url, $this->getOptions($url, $business, $method, $permissions));
|
|
56
|
+ return $app->get($url, $this->getOptions($url, $business, $method, $permissions, $middleware));
|
47
|
57
|
}
|
48
|
58
|
|
49
|
|
- public function post($url, $business, $method, $permissions = array())
|
|
59
|
+ /**
|
|
60
|
+ * @param $url string
|
|
61
|
+ * @param $business string
|
|
62
|
+ * @param $method string
|
|
63
|
+ * @param array $permissions string|string[]
|
|
64
|
+ * @param array $middleware string|string[]
|
|
65
|
+ * @return mixed
|
|
66
|
+ */
|
|
67
|
+ public function post($url, $business, $method, $permissions = array(), $middleware = array())
|
50
|
68
|
{
|
51
|
69
|
global $app;
|
52
|
|
- return $app->post($url, $this->getOptions($url, $business, $method, $permissions));
|
|
70
|
+ return $app->post($url, $this->getOptions($url, $business, $method, $permissions, $middleware));
|
53
|
71
|
}
|
54
|
72
|
|
55
|
|
- public function put($url, $business, $method, $permissions = array())
|
|
73
|
+ /**
|
|
74
|
+ * @param $url string
|
|
75
|
+ * @param $business string
|
|
76
|
+ * @param $method string
|
|
77
|
+ * @param array $permissions string|string[]
|
|
78
|
+ * @param array $middleware string|string[]
|
|
79
|
+ * @return mixed
|
|
80
|
+ */
|
|
81
|
+ public function put($url, $business, $method, $permissions = array(), $middleware = array())
|
56
|
82
|
{
|
57
|
83
|
global $app;
|
58
|
|
- return $app->put($url, $this->getOptions($url, $business, $method, $permissions));
|
|
84
|
+ return $app->put($url, $this->getOptions($url, $business, $method, $permissions, $middleware));
|
59
|
85
|
}
|
60
|
86
|
|
61
|
|
- public function delete($url, $business, $method, $permissions = array())
|
|
87
|
+ /**
|
|
88
|
+ * @param $url string
|
|
89
|
+ * @param $business string
|
|
90
|
+ * @param $method string
|
|
91
|
+ * @param array $permissions string|string[]
|
|
92
|
+ * @param array $middleware string|string[]
|
|
93
|
+ * @return mixed
|
|
94
|
+ */
|
|
95
|
+ public function delete($url, $business, $method, $permissions = array(), $middleware = array())
|
62
|
96
|
{
|
63
|
97
|
global $app;
|
64
|
|
- return $app->delete($url, $this->getOptions($url, $business, $method, $permissions));
|
|
98
|
+ return $app->delete($url, $this->getOptions($url, $business, $method, $permissions, $middleware));
|
65
|
99
|
}
|
66
|
100
|
}
|