Переглянути джерело

begin luticate 2 changes

develop
Robin Thoni 8 роки тому
джерело
коміт
75bf7ad3c6

+ 3
- 1
composer.json Переглянути файл

@@ -8,7 +8,9 @@
8 8
         }
9 9
     ],
10 10
     "require": {
11
-    "guzzlehttp/guzzle": "^6.1"
11
+    "guzzlehttp/guzzle": "^6.1",
12
+    "nikic/fast-route": "^1.0",
13
+        "nesbot/carbon": "^1.21"
12 14
     },
13 15
     "require-dev": {
14 16
         "phpunit/phpunit": "5.3.*"

src/Utils/LuBusiness.php → src/Utils/Business/LuBusiness.php Переглянути файл

@@ -1,6 +1,9 @@
1 1
 <?php
2 2
 
3
-namespace Luticate\Utils;
3
+namespace Luticate\Utils\Business;
4
+
5
+use Luticate\Utils\Dbo\LuDbo;
6
+use Luticate\Utils\LuDataAccess;
4 7
 
5 8
 abstract class LuBusiness {
6 9
     public static $parameters = array();

+ 18
- 0
src/Utils/Business/LuBusinessException.php Переглянути файл

@@ -0,0 +1,18 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 6/5/16
6
+ * Time: 2:38 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Business;
10
+
11
+
12
+class LuBusinessException extends \Exception
13
+{
14
+    public function __construct($message, $code, \Exception $previous = null)
15
+    {
16
+        parent::__construct($message, $code, $previous);
17
+    }
18
+}

src/Utils/LuLog.php → src/Utils/Business/LuLog.php Переглянути файл

@@ -1,12 +1,10 @@
1 1
 <?php
2 2
 
3
-namespace Luticate\Utils;
4
-use Log;
3
+namespace Luticate\Utils\Business;
5 4
 
6 5
 class LuLog {
7 6
 
8 7
     public static function log($data)
9 8
     {
10
-        Log::info($data);
11 9
     }
12 10
 }

src/Utils/LuStringUtils.php → src/Utils/Business/LuStringUtils.php Переглянути файл

@@ -6,11 +6,10 @@
6 6
  * Time: 11:28 PM
7 7
  */
8 8
 
9
-namespace Luticate\Utils;
9
+namespace Luticate\Utils\Business;
10 10
 
11 11
 use Carbon\Carbon;
12 12
 
13
-
14 13
 class LuStringUtils
15 14
 {
16 15
     public static function stringSnakeToCamelCase($string, $capitalizeFirstCharacter) {

+ 13
- 0
src/Utils/Controller/LuController.php Переглянути файл

@@ -0,0 +1,13 @@
1
+<?php
2
+
3
+namespace Luticate\Utils\Controller;
4
+
5
+use Luticate\Utils\Business\LuBusiness;
6
+
7
+abstract class LuController
8
+{
9
+    /**
10
+     * @return LuBusiness
11
+     */
12
+    protected abstract function getBusiness();
13
+}

+ 207
- 0
src/Utils/Controller/LuRoute.php Переглянути файл

@@ -0,0 +1,207 @@
1
+<?php
2
+
3
+namespace Luticate\Utils\Controller;
4
+
5
+use Luticate\Utils\Business\LuDocParser;
6
+use Luticate\Utils\Dbo\LuDboConstraintException;
7
+use Luticate\Utils\Dbo\LuDboDeserializeException;
8
+use FastRoute\DataGenerator\GroupCountBased as DataGeneratorGroupCountBased;
9
+use FastRoute\Dispatcher\GroupCountBased as DispatcherGroupCountBased;
10
+use FastRoute\RouteCollector;
11
+use FastRoute\RouteParser\Std;
12
+use Luticate\Utils\LuRouteDbo;
13
+
14
+class LuRoute {
15
+
16
+    const REG_UINT = "[0-9]+";
17
+    const REG_INT = "\\-?" . self::REG_UINT;
18
+    const REG_BOOL = "true|false";
19
+
20
+    /**
21
+     * @var DispatcherGroupCountBased
22
+     */
23
+    private $_dispatcher = null;
24
+
25
+    /**
26
+     * @var string[]
27
+     */
28
+    private $middleware = array('Luticate\Utils\ParametersMiddleware');
29
+
30
+    /**
31
+     * @var LuRouteDbo[]
32
+     */
33
+    private $routes = array();
34
+
35
+    /**
36
+     * @var LuRoute
37
+     */
38
+    private static $instance = null;
39
+
40
+    private function __construct()
41
+    {
42
+    }
43
+
44
+    public static function getInstance()
45
+    {
46
+        if (is_null(self::$instance)) {
47
+            self::$instance = new static();
48
+        }
49
+        return self::$instance;
50
+    }
51
+
52
+    public function getRoutes()
53
+    {
54
+        return $this->routes;
55
+    }
56
+
57
+    /**
58
+     * @param $middleware string
59
+     */
60
+    public function addMiddleware($middleware)
61
+    {
62
+        $this->middleware[] = $middleware;
63
+    }
64
+    
65
+    public function setup()
66
+    {
67
+        $routeCollector = new RouteCollector(new Std(), new DataGeneratorGroupCountBased());
68
+
69
+        foreach ($this->getRoutes() as $route) {
70
+            $routeCollector->addRoute($route->getMethod(), $route->getUrl(), function() use($route)
71
+            {
72
+                $router = static::getInstance();
73
+                $router->execute($route);
74
+            });
75
+        }
76
+
77
+        $this->_dispatcher = new DispatcherGroupCountBased($routeCollector->getData());
78
+    }
79
+
80
+    public function execute(LuRouteDbo $route)
81
+    {
82
+        $reflect = new \ReflectionMethod($route->getControllerClass(), $route->getControllerMethod());
83
+        $params = $reflect->getParameters();
84
+        $doc = new LuDocParser($reflect->getDocComment());
85
+        $doc->parse();
86
+
87
+        $args = array();
88
+        foreach ($params as $param) {
89
+
90
+            try {
91
+                if ($param->isOptional()) {
92
+                    $value = null;
93
+                    if (LuBusiness::hasParam([$param->getName()])) {
94
+                        $value = $this->getParam($param, LuBusiness::getParam($param->getName()));
95
+                    }
96
+                    else {
97
+                        $value = $param->getDefaultValue();
98
+                    }
99
+                    $args[$param->getName()] = $value;
100
+                }
101
+                else {
102
+                    $args[$param->getName()] = $this->getParam($param, LuBusiness::checkParam($param->getName()));
103
+                }
104
+                if (array_key_exists($param->getName(), $doc->getParams())) {
105
+                    foreach ($doc->getParams()[$param->getName()]->getConstraints() as $constraint) {
106
+                        call_user_func_array([$args[$param->getName()], $constraint->getMethod()],
107
+                            $constraint->getArguments());
108
+                    }
109
+                }
110
+            }
111
+            catch (LuDboConstraintException $e)
112
+            {
113
+                $paramName = $param->getName();
114
+                LuBusiness::badInput("Invalid value for '${paramName}': " . $e->getMessage());
115
+            }
116
+        }
117
+
118
+        $controller = $route->getControllerClass();
119
+        $controllerInstance = new $controller();
120
+        $result = call_user_func_array(array($controllerInstance, $route->getControllerMethod()), $args);
121
+
122
+        return LuOutputFormatter::formatSuccess($result);
123
+    }
124
+
125
+    public function dispatch(string $httpMethod, string $url)
126
+    {
127
+        $this->_dispatcher->dispatch($httpMethod, $url);
128
+    }
129
+    
130
+    private function getClassName(\ReflectionParameter $param) {
131
+        preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
132
+        return isset($matches[1]) ? $matches[1] : null;
133
+    }
134
+
135
+    private function getParam(\ReflectionParameter $param, $value)
136
+    {
137
+        $typedValue = null;
138
+        $className = $this->getClassName($param);
139
+        if (is_null($className)) {
140
+            $typedValue = $value;
141
+        }
142
+        else {
143
+            $class = $param->getClass();
144
+            try
145
+            {
146
+                $json = json_decode($value, true);
147
+                if (is_null($json)) {
148
+                    $json = $value . "";
149
+                }
150
+                $typedValue = call_user_func_array(array($class->getName(), "jsonDeserialize"), array($json));
151
+            }
152
+            catch (LuDboDeserializeException $e)
153
+            {
154
+                throw $e;
155
+            }
156
+            catch (\Exception $e)
157
+            {
158
+                LuLog::log($e);
159
+                LuBusiness::badInput("Unable to parse JSON value for '" . $param->getName() . "'");
160
+            }
161
+        }
162
+        return $typedValue;
163
+    }
164
+
165
+    public function addRoute(string $httpMethod, string $url, string $controller, string $method, $permissions = array(), $middlewares = array())
166
+    {
167
+        if (!is_array($permissions)) {
168
+            $permissions = array($permissions);
169
+        }
170
+        if (!is_array($middlewares)) {
171
+            $middlewares = array($middlewares);
172
+        }
173
+
174
+        if (strpos($controller, "\\") === false) {
175
+            $controller = "App\\Controller\\" . $controller . "Controller";
176
+        }
177
+
178
+        $route = new LuRouteDbo();
179
+        $route->setUrl($url);
180
+        $route->setControllerClass($controller);
181
+        $route->setControllerMethod($method);
182
+        $route->setMiddlewares($middlewares);
183
+        $route->setPermissions($permissions);
184
+        $route->setMethod($httpMethod);
185
+        $this->routes[] = $route;
186
+    }
187
+    
188
+    public function get(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
189
+    {
190
+        $this->addRoute("GET", $url, $controller, $method, $permissions, $middleware);
191
+    }
192
+
193
+    public function post(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
194
+    {
195
+        $this->addRoute("POST", $url, $controller, $method, $permissions, $middleware);
196
+    }
197
+
198
+    public function put(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
199
+    {
200
+        $this->addRoute("PUT", $url, $controller, $method, $permissions, $middleware);
201
+    }
202
+
203
+    public function delete(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
204
+    {
205
+        $this->addRoute("DELETE", $url, $controller, $method, $permissions, $middleware);
206
+    }
207
+}

src/Utils/LuDataAccess.php → src/Utils/DataAccess/LuDataAccess.php Переглянути файл


src/Utils/LuModel.php → src/Utils/DataAccess/LuModel.php Переглянути файл


src/Utils/LuRequest.php → src/Utils/DataAccess/LuRequest.php Переглянути файл


+ 0
- 4
src/Utils/Dbo/LuBoolDbo.php Переглянути файл

@@ -8,10 +8,6 @@
8 8
 
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11
-
12
-use Luticate\Utils\LuBusiness;
13
-use Luticate\Utils\LuDbo;
14
-
15 11
 class LuBoolDbo extends LuDbo
16 12
 {
17 13
     /**

+ 0
- 4
src/Utils/Dbo/LuBoolDboArray.php Переглянути файл

@@ -8,10 +8,6 @@
8 8
 
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11
-
12
-use Luticate\Utils\LuBusiness;
13
-use Luticate\Utils\LuDbo;
14
-
15 11
 class LuBoolDboArray extends LuDbo
16 12
 {
17 13
     /**

src/Utils/LuDbo.php → src/Utils/Dbo/LuDbo.php Переглянути файл

@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3
-namespace Luticate\Utils;
3
+namespace Luticate\Utils\Dbo;
4 4
 
5 5
 abstract class LuDbo implements \JsonSerializable {
6 6
     /**

+ 0
- 3
src/Utils/Dbo/LuFloatDbo.php Переглянути файл

@@ -9,9 +9,6 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
-use Luticate\Utils\LuBusiness;
13
-use Luticate\Utils\LuDbo;
14
-
15 12
 class LuFloatDbo extends LuDbo
16 13
 {
17 14
     /**

+ 0
- 3
src/Utils/Dbo/LuFloatDboArray.php Переглянути файл

@@ -9,9 +9,6 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
-use Luticate\Utils\LuBusiness;
13
-use Luticate\Utils\LuDbo;
14
-
15 12
 class LuFloatDboArray extends LuDbo
16 13
 {
17 14
     /**

+ 0
- 3
src/Utils/Dbo/LuIntDbo.php Переглянути файл

@@ -9,9 +9,6 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
-use Luticate\Utils\LuBusiness;
13
-use Luticate\Utils\LuDbo;
14
-
15 12
 class LuIntDbo extends LuDbo
16 13
 {
17 14
     /**

+ 0
- 3
src/Utils/Dbo/LuIntDboArray.php Переглянути файл

@@ -9,9 +9,6 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
-use Luticate\Utils\LuBusiness;
13
-use Luticate\Utils\LuDbo;
14
-
15 12
 class LuIntDboArray extends LuDbo
16 13
 {
17 14
     /**

src/Utils/LuMultipleDbo.php → src/Utils/Dbo/LuMultipleDbo.php Переглянути файл

@@ -1,8 +1,8 @@
1 1
 <?php
2 2
 
3
-namespace Luticate\Utils;
3
+namespace Luticate\Utils\Dbo;
4 4
 
5
-class LuMultipleDbo implements \JsonSerializable {
5
+class LuMultipleDbo extends LuDbo {
6 6
 
7 7
     /**
8 8
      * @var int
@@ -52,4 +52,17 @@ class LuMultipleDbo implements \JsonSerializable {
52 52
         }
53 53
         return new LuMultipleDbo($this->_count, $values);
54 54
     }
55
+    
56
+    public static function jsonDeserialize($json)
57
+    {
58
+        return new static($json["Count"], $json["Data"]);
59
+    }
60
+
61
+    public static function generateSample()
62
+    {
63
+        return [
64
+            "Count" => 42,
65
+            "Data" => []
66
+        ];
67
+    }
55 68
 }

+ 0
- 2
src/Utils/Dbo/LuParameterConstraintDbo.php Переглянути файл

@@ -9,8 +9,6 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
-use Luticate\Utils\LuDbo;
13
-
14 12
 class LuParameterConstraintDbo extends LuDbo
15 13
 {
16 14
     /**

+ 0
- 2
src/Utils/Dbo/LuParameterDbo.php Переглянути файл

@@ -8,8 +8,6 @@
8 8
 
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11
-use Luticate\Utils\LuDbo;
12
-
13 11
 class LuParameterDbo extends LuDbo
14 12
 {
15 13
     /**

src/Utils/LuRequestDbo.php → src/Utils/Dbo/LuRequestDbo.php Переглянути файл

@@ -6,8 +6,7 @@
6 6
  * Time: 10:47 PM
7 7
  */
8 8
 
9
-namespace Luticate\Utils;
10
-
9
+namespace Luticate\Utils\Dbo;
11 10
 
12 11
 class LuRequestDbo extends LuDbo
13 12
 {

src/Utils/LuRouteDbo.php → src/Utils/Dbo/LuRouteDbo.php Переглянути файл

@@ -8,7 +8,6 @@
8 8
 
9 9
 namespace Luticate\Utils;
10 10
 
11
-
12 11
 class LuRouteDbo
13 12
 {
14 13
     /**
@@ -19,121 +18,121 @@ class LuRouteDbo
19 18
     /**
20 19
      * @var string
21 20
      */
22
-    private $_businessClass;
21
+    private $_controllerClass;
23 22
 
24 23
     /**
25 24
      * @var string
26 25
      */
27
-    private $_businessMethod;
28
-
29
-    /**
30
-     * @var string[]
31
-     */
32
-    private $_permissions;
26
+    private $_controllerMethod;
33 27
 
34 28
     /**
35
-     * @var string[]
29
+     * @return string
36 30
      */
37
-    private $_middlware;
31
+    public function getControllerClass()
32
+    {
33
+        return $this->_controllerClass;
34
+    }
38 35
 
39 36
     /**
40
-     * @var string
37
+     * @param string $controllerClass
41 38
      */
42
-    private $_method;
39
+    public function setControllerClass($controllerClass)
40
+    {
41
+        $this->_controllerClass = $controllerClass;
42
+    }
43 43
 
44 44
     /**
45 45
      * @return string
46 46
      */
47
-    public function getMethod()
47
+    public function getControllerMethod()
48 48
     {
49
-        return $this->_method;
49
+        return $this->_controllerMethod;
50 50
     }
51 51
 
52 52
     /**
53
-     * @param string $method
53
+     * @param string $controllerMethod
54 54
      */
55
-    public function setMethod($method)
55
+    public function setControllerMethod($controllerMethod)
56 56
     {
57
-        $this->_method = $method;
57
+        $this->_controllerMethod = $controllerMethod;
58 58
     }
59 59
 
60 60
     /**
61
-     * @return string
61
+     * @var string[]
62 62
      */
63
-    public function getUrl()
64
-    {
65
-        return $this->_url;
66
-    }
63
+    private $_permissions;
67 64
 
68 65
     /**
69
-     * @param string $url
66
+     * @var string[]
70 67
      */
71
-    public function setUrl($url)
72
-    {
73
-        $this->_url = $url;
74
-    }
68
+    private $_middlewares;
75 69
 
76 70
     /**
77
-     * @return string
71
+     * @return \string[]
78 72
      */
79
-    public function getBusinessClass()
73
+    public function getMiddlewares()
80 74
     {
81
-        return $this->_businessClass;
75
+        return $this->_middlewares;
82 76
     }
83 77
 
84 78
     /**
85
-     * @param string $businessClass
79
+     * @param \string[] $middlewares
86 80
      */
87
-    public function setBusinessClass($businessClass)
81
+    public function setMiddlewares($middlewares)
88 82
     {
89
-        $this->_businessClass = $businessClass;
83
+        $this->_middlewares = $middlewares;
90 84
     }
91 85
 
86
+    /**
87
+     * @var string
88
+     */
89
+    private $_method;
90
+
92 91
     /**
93 92
      * @return string
94 93
      */
95
-    public function getBusinessMethod()
94
+    public function getMethod()
96 95
     {
97
-        return $this->_businessMethod;
96
+        return $this->_method;
98 97
     }
99 98
 
100 99
     /**
101
-     * @param string $businessMethod
100
+     * @param string $method
102 101
      */
103
-    public function setBusinessMethod($businessMethod)
102
+    public function setMethod($method)
104 103
     {
105
-        $this->_businessMethod = $businessMethod;
104
+        $this->_method = $method;
106 105
     }
107 106
 
108 107
     /**
109
-     * @return \string[]
108
+     * @return string
110 109
      */
111
-    public function getPermissions()
110
+    public function getUrl()
112 111
     {
113
-        return $this->_permissions;
112
+        return $this->_url;
114 113
     }
115 114
 
116 115
     /**
117
-     * @param \string[] $permissions
116
+     * @param string $url
118 117
      */
119
-    public function setPermissions($permissions)
118
+    public function setUrl($url)
120 119
     {
121
-        $this->_permissions = $permissions;
120
+        $this->_url = $url;
122 121
     }
123 122
 
124 123
     /**
125 124
      * @return \string[]
126 125
      */
127
-    public function getMiddlware()
126
+    public function getPermissions()
128 127
     {
129
-        return $this->_middlware;
128
+        return $this->_permissions;
130 129
     }
131 130
 
132 131
     /**
133
-     * @param \string[] $middlware
132
+     * @param \string[] $permissions
134 133
      */
135
-    public function setMiddlware($middlware)
134
+    public function setPermissions($permissions)
136 135
     {
137
-        $this->_middlware = $middlware;
136
+        $this->_permissions = $permissions;
138 137
     }
139 138
 }

src/Utils/LuSpDbo.php → src/Utils/Dbo/LuSpDbo.php Переглянути файл

@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3
-namespace Luticate\Utils;
3
+namespace Luticate\Utils\Dbo;
4 4
 
5 5
 abstract class LuSpDbo extends LuDbo
6 6
 {

+ 0
- 4
src/Utils/Dbo/LuStringDbo.php Переглянути файл

@@ -8,10 +8,6 @@
8 8
 
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11
-
12
-use Luticate\Utils\LuBusiness;
13
-use Luticate\Utils\LuDbo;
14
-
15 11
 class LuStringDbo extends LuDbo
16 12
 {
17 13
     /**

+ 0
- 4
src/Utils/Dbo/LuStringDboArray.php Переглянути файл

@@ -8,10 +8,6 @@
8 8
 
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11
-
12
-use Luticate\Utils\LuBusiness;
13
-use Luticate\Utils\LuDbo;
14
-
15 11
 class LuStringDboArray extends LuDbo
16 12
 {
17 13
     /**

+ 0
- 13
src/Utils/LuController.php Переглянути файл

@@ -1,13 +0,0 @@
1
-<?php
2
-
3
-namespace Luticate\Utils;
4
-
5
-use Laravel\Lumen\Routing\Controller as BaseController;
6
-
7
-abstract class LuController extends BaseController
8
-{
9
-    /**
10
-     * @return LuBusiness
11
-     */
12
-    protected abstract function getBusiness();
13
-}

+ 0
- 221
src/Utils/LuRoute.php Переглянути файл

@@ -1,221 +0,0 @@
1
-<?php
2
-
3
-namespace Luticate\Utils;
4
-
5
-use Luticate\Utils\Business\LuDocParser;
6
-use Luticate\Utils\Dbo\LuDboConstraintException;
7
-use Luticate\Utils\Dbo\LuDboDeserializeException;
8
-
9
-class LuRoute {
10
-
11
-    const REG_UINT = "[0-9]+";
12
-    const REG_INT = "\\-?" . self::REG_UINT;
13
-    const REG_BOOL = "true|false";
14
-
15
-    /**
16
-     * @var string[]
17
-     */
18
-    private $middleware = array('Luticate\Utils\ParametersMiddleware');
19
-
20
-    /**
21
-     * @var LuRouteDbo[]
22
-     */
23
-    private $routes = array();
24
-
25
-    /**
26
-     * @var LuRoute
27
-     */
28
-    private static $instance = null;
29
-
30
-    private function __construct()
31
-    {
32
-
33
-    }
34
-
35
-    public static function getInstance()
36
-    {
37
-        if (is_null(self::$instance)) {
38
-            self::$instance = new LuRoute();
39
-        }
40
-        return self::$instance;
41
-    }
42
-
43
-    public function getRoutes()
44
-    {
45
-        return $this->routes;
46
-    }
47
-
48
-    /**
49
-     * @param $middleware string
50
-     */
51
-    public function addMiddleware($middleware)
52
-    {
53
-        $this->middleware[] = $middleware;
54
-    }
55
-
56
-    private function getClassName(\ReflectionParameter $param) {
57
-        preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
58
-        return isset($matches[1]) ? $matches[1] : null;
59
-    }
60
-
61
-    private function getParam(\ReflectionParameter $param, $value)
62
-    {
63
-        $typedValue = null;
64
-        $className = $this->getClassName($param);
65
-        if (is_null($className)) {
66
-            $typedValue = $value;
67
-        }
68
-        else {
69
-            $class = $param->getClass();
70
-            try
71
-            {
72
-                $json = json_decode($value, true);
73
-                if (is_null($json)) {
74
-                    $json = $value . "";
75
-                }
76
-                $typedValue = call_user_func_array(array($class->getName(), "jsonDeserialize"), array($json));
77
-            }
78
-            catch (LuDboDeserializeException $e)
79
-            {
80
-                throw $e;
81
-            }
82
-            catch (\Exception $e)
83
-            {
84
-                LuLog::log($e);
85
-                LuBusiness::badInput("Unable to parse JSON value for '" . $param->getName() . "'");
86
-            }
87
-        }
88
-        return $typedValue;
89
-    }
90
-
91
-    private function getOptions($httpMethod, $url, $controller, $method, $permissions, $middleware)
92
-    {
93
-        if (!is_array($permissions)) {
94
-            $permissions = array($permissions);
95
-        }
96
-        if (!is_array($middleware)) {
97
-            $middleware = array($middleware);
98
-        }
99
-        $permissions_string = implode(",", $permissions);
100
-        $middleware_string = [];
101
-        foreach (array_merge($this->middleware, $middleware) as $mid) {
102
-
103
-            $middleware_string[] = $mid . (strpos($mid, ":") !== false ? "," : ":") . $permissions_string;
104
-        }
105
-
106
-        if (strpos($controller, "\\") === false) {
107
-            $controller = "App\\Http\\Controller\\" . $controller . "Controller";
108
-        }
109
-
110
-        $route = new LuRouteDbo();
111
-        $route->setUrl($url);
112
-        $route->setBusinessClass($controller);
113
-        $route->setBusinessMethod($method);
114
-        $route->setMiddlware($middleware_string);
115
-        $route->setPermissions($permissions);
116
-        $route->setMethod($httpMethod);
117
-        $this->routes[] = $route;
118
-
119
-        $ctrl = $this;
120
-
121
-        return [function() use($controller, $method, $ctrl)
122
-        {
123
-            $reflect = new \ReflectionMethod($controller, $method);
124
-            $params = $reflect->getParameters();
125
-            $doc = new LuDocParser($reflect->getDocComment());
126
-            $doc->parse();
127
-
128
-            $args = array();
129
-            foreach ($params as $param) {
130
-
131
-                try {
132
-                    if ($param->isOptional()) {
133
-                        $value = null;
134
-                        if (LuBusiness::hasParam([$param->getName()])) {
135
-                            $value = $ctrl->getParam($param, LuBusiness::getParam($param->getName()));
136
-                        }
137
-                        else {
138
-                            $value = $param->getDefaultValue();
139
-                        }
140
-                        $args[$param->getName()] = $value;
141
-                    }
142
-                    else {
143
-                        $args[$param->getName()] = $ctrl->getParam($param, LuBusiness::checkParam($param->getName()));
144
-                    }
145
-                    if (array_key_exists($param->getName(), $doc->getParams())) {
146
-                        foreach ($doc->getParams()[$param->getName()]->getConstraints() as $constraint) {
147
-                            call_user_func_array([$args[$param->getName()], $constraint->getMethod()],
148
-                                $constraint->getArguments());
149
-                        }
150
-                    }
151
-                }
152
-                catch (LuDboConstraintException $e)
153
-                {
154
-                    $paramName = $param->getName();
155
-                    LuBusiness::badInput("Invalid value for '${paramName}': " . $e->getMessage());
156
-                }
157
-            }
158
-
159
-            $controllerInstance = new $controller();
160
-            $result = call_user_func_array(array($controllerInstance, $method), $args);
161
-
162
-            return LuOutputFormatter::formatSuccess($result);
163
-        }, 'middleware' => $middleware_string];
164
-    }
165
-
166
-    /**
167
-     * @param $url string
168
-     * @param $controller string
169
-     * @param $method string
170
-     * @param array $permissions string|string[]
171
-     * @param array $middleware string|string[]
172
-     * @return mixed
173
-     */
174
-    public function get($url, $controller, $method, $permissions = array(), $middleware = array())
175
-    {
176
-        global $app;
177
-        return $app->get($url, $this->getOptions("GET", $url, $controller, $method, $permissions, $middleware));
178
-    }
179
-
180
-    /**
181
-     * @param $url string
182
-     * @param $controller string
183
-     * @param $method string
184
-     * @param array $permissions string|string[]
185
-     * @param array $middleware string|string[]
186
-     * @return mixed
187
-     */
188
-    public function post($url, $controller, $method, $permissions = array(), $middleware = array())
189
-    {
190
-        global $app;
191
-        return $app->post($url, $this->getOptions("POST", $url, $controller, $method, $permissions, $middleware));
192
-    }
193
-
194
-    /**
195
-     * @param $url string
196
-     * @param $controller string
197
-     * @param $method string
198
-     * @param array $permissions string|string[]
199
-     * @param array $middleware string|string[]
200
-     * @return mixed
201
-     */
202
-    public function put($url, $controller, $method, $permissions = array(), $middleware = array())
203
-    {
204
-        global $app;
205
-        return $app->put($url, $this->getOptions("PUT", $url, $controller, $method, $permissions, $middleware));
206
-    }
207
-
208
-    /**
209
-     * @param $url string
210
-     * @param $controller string
211
-     * @param $method string
212
-     * @param array $permissions string|string[]
213
-     * @param array $middleware string|string[]
214
-     * @return mixed
215
-     */
216
-    public function delete($url, $controller, $method, $permissions = array(), $middleware = array())
217
-    {
218
-        global $app;
219
-        return $app->delete($url, $this->getOptions("DELETE", $url, $controller, $method, $permissions, $middleware));
220
-    }
221
-}

+ 23
- 0
src/Utils/Middleware/LuAbstractMiddleware.php Переглянути файл

@@ -0,0 +1,23 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 6/5/16
6
+ * Time: 2:58 PM
7
+ */
8
+
9
+namespace Luticate\Utils;
10
+
11
+
12
+abstract class LuAbstractMiddleware
13
+{
14
+    public function onBefore(LuRouteDbo $route, ...$params)
15
+    {
16
+        
17
+    }
18
+    
19
+    public function onAfter(LuRouteDbo $route, $output, ...$params)
20
+    {
21
+        
22
+    }
23
+}

src/Utils/ParametersMiddleware.php → src/Utils/Middleware/ParametersMiddleware.php Переглянути файл


Завантаження…
Відмінити
Зберегти