Browse Source

begin middleware management

develop
Robin Thoni 8 years ago
parent
commit
7124c278f8

+ 70
- 42
src/Utils/Controller/LuRoute.php View File

11
 use FastRoute\Dispatcher\GroupCountBased as DispatcherGroupCountBased;
11
 use FastRoute\Dispatcher\GroupCountBased as DispatcherGroupCountBased;
12
 use FastRoute\RouteCollector;
12
 use FastRoute\RouteCollector;
13
 use FastRoute\RouteParser\Std;
13
 use FastRoute\RouteParser\Std;
14
+use Luticate\Utils\Dbo\LuMiddlewareDbo;
14
 use Luticate\Utils\Dbo\LuRouteDbo;
15
 use Luticate\Utils\Dbo\LuRouteDbo;
15
 use Luticate\Utils\Middleware\LuAbstractMiddleware;
16
 use Luticate\Utils\Middleware\LuAbstractMiddleware;
16
 
17
 
26
     private $_dispatcher = null;
27
     private $_dispatcher = null;
27
 
28
 
28
     /**
29
     /**
29
-     * @var string[]
30
+     * @var LuMiddlewareDbo[]
30
      */
31
      */
31
-    private $middleware = array('Luticate\Utils\ParametersMiddleware');
32
+    private $_middlewares = [];
32
 
33
 
33
     /**
34
     /**
34
      * @var LuRouteDbo[]
35
      * @var LuRouteDbo[]
45
     }
46
     }
46
 
47
 
47
     /**
48
     /**
48
-     * @param $middleware string
49
+     * @param $middleware mixed
49
      */
50
      */
50
-    public function addMiddleware($middleware)
51
+    public function addMiddleware(mixed $middleware)
51
     {
52
     {
52
-        $this->middleware[] = $middleware;
53
+        if (is_string($middleware)) {
54
+            $this->_middlewares[] = new LuMiddlewareDbo($middleware);
55
+        }
56
+        else if (is_array($middleware)) {
57
+            $this->_middlewares[] = LuMiddlewareDbo::jsonDeserialize($middleware);
58
+        }
59
+        else {
60
+            $this->_middlewares[] = $middleware;
61
+        }
53
     }
62
     }
54
     
63
     
55
     public function setup()
64
     public function setup()
70
     public function execute(LuRouteDbo $route, $parameters)
79
     public function execute(LuRouteDbo $route, $parameters)
71
     {
80
     {
72
         $middlewares = [];
81
         $middlewares = [];
73
-        foreach ($route->getMiddlewares() as $middlewareName) {
82
+        /**
83
+         * @var $middlewareDbo LuMiddlewareDbo
84
+         */
85
+        foreach (array_merge($this->_middlewares, $route->getMiddlewares()) as $middlewareDbo) {
86
+            $middlewareName = $middlewareDbo->getMiddleware();
74
             /**
87
             /**
75
              * @var $middleware LuAbstractMiddleware
88
              * @var $middleware LuAbstractMiddleware
76
              */
89
              */
77
             $middleware = new $middlewareName();
90
             $middleware = new $middlewareName();
91
+            $parameters["_middleware"] = array_merge($middlewareDbo->getParameters(), $route->getParameters());
78
             $parameters = $middleware->onBefore($route, $parameters);
92
             $parameters = $middleware->onBefore($route, $parameters);
79
-            $middlewares[] = $middleware;
93
+            $middlewares[] = [
94
+                'middleware' => $middleware,
95
+                'dbo' => $middlewareDbo
96
+            ];
80
         }
97
         }
98
+        unset($parameters["_middleware"]);
81
 
99
 
82
-        $reflect = new \ReflectionMethod($route->getControllerClass(), $route->getControllerMethod());
100
+        $reflect = new \ReflectionMethod($route->getController(), $route->getAction());
83
         $params = $reflect->getParameters();
101
         $params = $reflect->getParameters();
84
         $docs = new LuMethodDocParser($reflect->getDocComment());
102
         $docs = new LuMethodDocParser($reflect->getDocComment());
85
         $docs->parse();
103
         $docs->parse();
130
             }
148
             }
131
         }
149
         }
132
 
150
 
133
-        $controller = $route->getControllerClass();
151
+        $controller = $route->getController();
134
         $controllerInstance = new $controller();
152
         $controllerInstance = new $controller();
135
-        $result = call_user_func_array(array($controllerInstance, $route->getControllerMethod()), $args);
153
+        $result = call_user_func_array(array($controllerInstance, $route->getAction()), $args);
136
 
154
 
137
-        foreach ($middlewares as $middleware) {
155
+        foreach ($middlewares as $middlewareArray) {
156
+            $middleware = $middlewareArray['middleware'];
157
+            $middlewareDbo = $middlewareArray['dbo'];
158
+            $parameters["_middleware"] = array_merge($middlewareDbo->getParameters(), $route->getParameters());
138
             $result = $middleware->onAfter($route, $parameters, $result);
159
             $result = $middleware->onAfter($route, $parameters, $result);
139
         }
160
         }
140
 
161
 
178
                 $json = json_decode($value, true);
199
                 $json = json_decode($value, true);
179
                 $typedValue = call_user_func_array(array($class->getName(), "jsonDeserialize"), array($json));
200
                 $typedValue = call_user_func_array(array($class->getName(), "jsonDeserialize"), array($json));
180
             }
201
             }
181
-            catch (LuDboDeserializeException $e)
182
-            {
183
-                throw $e;
184
-            }
185
-            catch (LuDboConstraintException $e)
186
-            {
187
-                throw $e;
188
-            }
189
             catch (\Exception $e)
202
             catch (\Exception $e)
190
             {
203
             {
204
+                if ($e instanceof LuDboDeserializeException || $e instanceof LuDboConstraintException) {
205
+                    throw $e;
206
+                }
191
                 throw new LuBusinessException("Unable to parse JSON value for '" . $param->getName() . "'", 400, $e);
207
                 throw new LuBusinessException("Unable to parse JSON value for '" . $param->getName() . "'", 400, $e);
192
             }
208
             }
193
         }
209
         }
194
         return $typedValue;
210
         return $typedValue;
195
     }
211
     }
196
-
197
-    public function addRoute(string $httpMethod, string $url, string $controller, string $method, $permissions = array(), $middlewares = array())
198
-    {
199
-        if (!is_array($permissions)) {
200
-            $permissions = array($permissions);
201
-        }
202
-        if (!is_array($middlewares)) {
203
-            $middlewares = array($middlewares);
204
-        }
205
-
212
+    
213
+    public function addRoute2(LuRouteDbo $route) {
214
+        $controller = $route->getController();
206
         if (strpos($controller, "\\") === false) {
215
         if (strpos($controller, "\\") === false) {
207
             $controller = "App\\Controller\\" . $controller . "Controller";
216
             $controller = "App\\Controller\\" . $controller . "Controller";
208
         }
217
         }
218
+        $route->setController($controller);
219
+        $this->routes[] = $route;
220
+    }
209
 
221
 
222
+    public function addRoute(string $httpMethod, string $url, string $controller, string $method, $parameters = array(), $middlewares = array())
223
+    {
224
+        $middlewaresDbo = [];
225
+        foreach ($middlewares as $middleware) {
226
+            if (is_string($middleware)) {
227
+                $middlewaresDbo[] = new LuMiddlewareDbo($middleware);
228
+            }
229
+            else if (is_array($middleware)) {
230
+                $middlewaresDbo[] = LuMiddlewareDbo::jsonDeserialize($middleware);
231
+            }
232
+            else {
233
+                $middlewaresDbo[] = $middleware;
234
+            }
235
+        }
236
+        
210
         $route = new LuRouteDbo();
237
         $route = new LuRouteDbo();
211
         $route->setUrl($url);
238
         $route->setUrl($url);
212
-        $route->setControllerClass($controller);
213
-        $route->setControllerMethod($method);
214
-        $route->setMiddlewares($middlewares);
215
-        $route->setPermissions($permissions);
239
+        $route->setController($controller);
240
+        $route->setAction($method);
241
+        $route->setMiddlewares($middlewaresDbo);
242
+        $route->setParameters($parameters);
216
         $route->setMethod($httpMethod);
243
         $route->setMethod($httpMethod);
217
-        $this->routes[] = $route;
244
+        
245
+        $this->addRoute2($route);
218
     }
246
     }
219
     
247
     
220
-    public function get(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
248
+    public function get(string $url, string $controller, string $method, $parameters = array(), $middleware = array())
221
     {
249
     {
222
-        $this->addRoute("GET", $url, $controller, $method, $permissions, $middleware);
250
+        $this->addRoute("GET", $url, $controller, $method, $parameters, $middleware);
223
     }
251
     }
224
 
252
 
225
-    public function post(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
253
+    public function post(string $url, string $controller, string $method, $parameters = array(), $middleware = array())
226
     {
254
     {
227
-        $this->addRoute("POST", $url, $controller, $method, $permissions, $middleware);
255
+        $this->addRoute("POST", $url, $controller, $method, $parameters, $middleware);
228
     }
256
     }
229
 
257
 
230
-    public function put(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
258
+    public function put(string $url, string $controller, string $method, $parameters = array(), $middleware = array())
231
     {
259
     {
232
-        $this->addRoute("PUT", $url, $controller, $method, $permissions, $middleware);
260
+        $this->addRoute("PUT", $url, $controller, $method, $parameters, $middleware);
233
     }
261
     }
234
 
262
 
235
-    public function delete(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
263
+    public function delete(string $url, string $controller, string $method, $parameters = array(), $middleware = array())
236
     {
264
     {
237
-        $this->addRoute("DELETE", $url, $controller, $method, $permissions, $middleware);
265
+        $this->addRoute("DELETE", $url, $controller, $method, $parameters, $middleware);
238
     }
266
     }
239
 }
267
 }

+ 1
- 1
src/Utils/Controller/LuticateApplication.php View File

78
     public function setupRoutes()
78
     public function setupRoutes()
79
     {
79
     {
80
         $app = $this;
80
         $app = $this;
81
-        require_once __DIR__ . '/../../../../../../app/Route/routes.php';
81
+        require_once __DIR__ . '/../../../../../../app/routes.php';
82
         
82
         
83
         $this->_router->setup();
83
         $this->_router->setup();
84
     }
84
     }

+ 1
- 1
src/Utils/Dbo/LuDbo.php View File

161
             }
161
             }
162
             call_user_func_array([$this, $constraint->getMethod()], $constraint->getArguments());
162
             call_user_func_array([$this, $constraint->getMethod()], $constraint->getArguments());
163
         }
163
         }
164
-    } 
164
+    }
165
 
165
 
166
     /**
166
     /**
167
      * Generate a sample JSON object for the DBO
167
      * Generate a sample JSON object for the DBO

+ 9
- 3
src/Utils/Dbo/LuMiddlewareDbo.php View File

11
 
11
 
12
 class LuMiddlewareDbo extends LuDbo
12
 class LuMiddlewareDbo extends LuDbo
13
 {
13
 {
14
-    function jsonSerialize()
14
+    /**
15
+     * LuMiddlewareDbo constructor.
16
+     * @param $middleware string
17
+     * @param $parameters array
18
+     */
19
+    public function __construct($middleware, $parameters = [])
15
     {
20
     {
16
-        // TODO: Implement jsonSerialize() method.
21
+        $this->_middleware = $middleware;
22
+        $this->_parameters = $parameters;
17
     }
23
     }
18
 
24
 
19
     /**
25
     /**
20
-     * @var $_name string
26
+     * @var $_middleware string
21
      */
27
      */
22
     private $_middleware;
28
     private $_middleware;
23
 
29
 

+ 53
- 0
src/Utils/Dbo/LuMiddlewareDboArray.php View File

1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 6/5/16
6
+ * Time: 6:24 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+
12
+class LuMiddlewareDboArray extends LuDbo
13
+{
14
+    /**
15
+     * @var LuMiddlewareDbo[]
16
+     */
17
+    protected $_array;
18
+    public function getArray()
19
+    {
20
+        return $this->_array;
21
+    }
22
+    public function setArray($value)
23
+    {
24
+        $this->_array = $value;
25
+    }
26
+
27
+    public function jsonSerialize()
28
+    {
29
+        return $this->_array;
30
+    }
31
+
32
+    public static function jsonDeserialize($json)
33
+    {
34
+        if (!is_array($json)) {
35
+            throw new LuDboDeserializeException("Invalid array value");
36
+        }
37
+        $dbo = new self();
38
+        $array = [];
39
+        foreach ($json as $data) {
40
+            $array[] = LuMiddlewareDbo::jsonDeserialize($data);
41
+        }
42
+        $dbo->setArray($array);
43
+        return $dbo;
44
+    }
45
+
46
+    public static function generateSample()
47
+    {
48
+        return [
49
+            LuMiddlewareDbo::generateSample(),
50
+            LuMiddlewareDbo::generateSample()
51
+        ];
52
+    }
53
+}

+ 46
- 38
src/Utils/Dbo/LuRouteDbo.php View File

8
 
8
 
9
 namespace Luticate\Utils\Dbo;
9
 namespace Luticate\Utils\Dbo;
10
 
10
 
11
-class LuRouteDbo
11
+class LuRouteDbo extends LuDbo
12
 {
12
 {
13
+    /**
14
+     * @var string
15
+     */
16
+    private $_method;
17
+    
13
     /**
18
     /**
14
      * @var string
19
      * @var string
15
      */
20
      */
18
     /**
23
     /**
19
      * @var string
24
      * @var string
20
      */
25
      */
21
-    private $_controllerClass;
26
+    private $_controller;
22
 
27
 
23
     /**
28
     /**
24
      * @var string
29
      * @var string
25
      */
30
      */
26
-    private $_controllerMethod;
31
+    private $_action;
27
 
32
 
28
     /**
33
     /**
29
-     * @return string
34
+     * @var LuMiddlewareDbo[]
35
+     */
36
+    private $_middlewares;
37
+
38
+    /**
39
+     * @var array
40
+     */
41
+    private $_parameters;
42
+
43
+    /**
44
+     * @return array
30
      */
45
      */
31
-    public function getControllerClass()
46
+    public function getParameters()
32
     {
47
     {
33
-        return $this->_controllerClass;
48
+        return $this->_parameters;
34
     }
49
     }
35
 
50
 
36
     /**
51
     /**
37
-     * @param string $controllerClass
52
+     * @param array $parameters
38
      */
53
      */
39
-    public function setControllerClass($controllerClass)
54
+    public function setParameters($parameters)
40
     {
55
     {
41
-        $this->_controllerClass = $controllerClass;
56
+        $this->_parameters = $parameters;
42
     }
57
     }
43
 
58
 
44
     /**
59
     /**
45
      * @return string
60
      * @return string
46
      */
61
      */
47
-    public function getControllerMethod()
62
+    public function getController()
48
     {
63
     {
49
-        return $this->_controllerMethod;
64
+        return $this->_controller;
50
     }
65
     }
51
 
66
 
52
     /**
67
     /**
53
-     * @param string $controllerMethod
68
+     * @param string $controller
54
      */
69
      */
55
-    public function setControllerMethod($controllerMethod)
70
+    public function setController($controller)
56
     {
71
     {
57
-        $this->_controllerMethod = $controllerMethod;
72
+        $this->_controller = $controller;
58
     }
73
     }
59
 
74
 
60
     /**
75
     /**
61
-     * @var string[]
76
+     * @return string
62
      */
77
      */
63
-    private $_permissions;
78
+    public function getAction()
79
+    {
80
+        return $this->_action;
81
+    }
64
 
82
 
65
     /**
83
     /**
66
-     * @var string[]
84
+     * @param string $action
67
      */
85
      */
68
-    private $_middlewares;
86
+    public function setAction($action)
87
+    {
88
+        $this->_action = $action;
89
+    }
69
 
90
 
70
     /**
91
     /**
71
-     * @return \string[]
92
+     * @return LuMiddlewareDbo[]
72
      */
93
      */
73
     public function getMiddlewares()
94
     public function getMiddlewares()
74
     {
95
     {
76
     }
97
     }
77
 
98
 
78
     /**
99
     /**
79
-     * @param \string[] $middlewares
100
+     * @param LuMiddlewareDbo[] $middlewares
80
      */
101
      */
81
     public function setMiddlewares($middlewares)
102
     public function setMiddlewares($middlewares)
82
     {
103
     {
84
     }
105
     }
85
 
106
 
86
     /**
107
     /**
87
-     * @var string
108
+     * @param LuMiddlewareDbo $middleware
88
      */
109
      */
89
-    private $_method;
110
+    public function addMiddleware($middleware)
111
+    {
112
+        $this->_middlewares[] = $middleware;
113
+    }
90
 
114
 
91
     /**
115
     /**
92
      * @return string
116
      * @return string
119
     {
143
     {
120
         $this->_url = $url;
144
         $this->_url = $url;
121
     }
145
     }
122
-
123
-    /**
124
-     * @return \string[]
125
-     */
126
-    public function getPermissions()
127
-    {
128
-        return $this->_permissions;
129
-    }
130
-
131
-    /**
132
-     * @param \string[] $permissions
133
-     */
134
-    public function setPermissions($permissions)
135
-    {
136
-        $this->_permissions = $permissions;
137
-    }
138
 }
146
 }

+ 53
- 0
src/Utils/Dbo/LuRouteDboArray.php View File

1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 6/5/16
6
+ * Time: 6:24 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+
12
+class LuRouteDboArray extends LuDbo
13
+{
14
+    /**
15
+     * @var LuRouteDbo[]
16
+     */
17
+    protected $_array;
18
+    public function getArray()
19
+    {
20
+        return $this->_array;
21
+    }
22
+    public function setArray($value)
23
+    {
24
+        $this->_array = $value;
25
+    }
26
+
27
+    public function jsonSerialize()
28
+    {
29
+        return $this->_array;
30
+    }
31
+
32
+    public static function jsonDeserialize($json)
33
+    {
34
+        if (!is_array($json)) {
35
+            throw new LuDboDeserializeException("Invalid array value");
36
+        }
37
+        $dbo = new self();
38
+        $array = [];
39
+        foreach ($json as $data) {
40
+            $array[] = LuRouteDbo::jsonDeserialize($data);
41
+        }
42
+        $dbo->setArray($array);
43
+        return $dbo;
44
+    }
45
+
46
+    public static function generateSample()
47
+    {
48
+        return [
49
+            LuRouteDbo::generateSample(),
50
+            LuRouteDbo::generateSample()
51
+        ];
52
+    }
53
+}

+ 14
- 3
src/Utils/Middleware/LuAbstractMiddleware.php View File

12
 
12
 
13
 abstract class LuAbstractMiddleware
13
 abstract class LuAbstractMiddleware
14
 {
14
 {
15
-    public abstract function onBefore(LuRouteDbo $route, $parameters, ...$params);
16
-    
17
-    public abstract function onAfter(LuRouteDbo $route, $parameters, $result, ...$params);
15
+    /**
16
+     * @param LuRouteDbo $route The route being executed
17
+     * @param $parameters array The request parameters
18
+     * @return mixed The requests parameters
19
+     */
20
+    public abstract function onBefore(LuRouteDbo $route, $parameters);
21
+
22
+    /**
23
+     * @param LuRouteDbo $route The route being executed
24
+     * @param $parameters array The request parameters
25
+     * @param $result mixed The request result
26
+     * @return mixed The request result
27
+     */
28
+    public abstract function onAfter(LuRouteDbo $route, $parameters, $result);
18
 }
29
 }

Loading…
Cancel
Save