Przeglądaj źródła

added luticate application

develop
Robin Thoni 8 lat temu
rodzic
commit
bd8016aa16

+ 2
- 1
composer.json Wyświetl plik

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

+ 3
- 3
src/Utils/Business/LuBusiness.php Wyświetl plik

@@ -60,7 +60,7 @@ abstract class LuBusiness {
60 60
      */
61 61
     public static function unauthorized($reason = 'Unauthorized')
62 62
     {
63
-        abort(401, $reason);
63
+        throw new LuBusinessException($reason, 401);
64 64
     }
65 65
 
66 66
     /**
@@ -68,7 +68,7 @@ abstract class LuBusiness {
68 68
      */
69 69
     public static function notFound($reason = 'Resource not found')
70 70
     {
71
-        abort(404, $reason);
71
+        throw new LuBusinessException($reason, 404);
72 72
     }
73 73
 
74 74
     /**
@@ -76,7 +76,7 @@ abstract class LuBusiness {
76 76
      */
77 77
     public static function badInput($reason = 'Invalid user input')
78 78
     {
79
-        abort(400, $reason);
79
+        throw new LuBusinessException($reason, 400);
80 80
     }
81 81
 
82 82
     /**

+ 20
- 15
src/Utils/Controller/LuRoute.php Wyświetl plik

@@ -3,7 +3,6 @@
3 3
 namespace Luticate\Utils\Controller;
4 4
 
5 5
 use FastRoute\Dispatcher;
6
-use Luticate\Utils\Business\LuBusiness;
7 6
 use Luticate\Utils\Business\LuBusinessException;
8 7
 use Luticate\Utils\Business\LuDocParser;
9 8
 use Luticate\Utils\Dbo\LuDboConstraintException;
@@ -71,17 +70,17 @@ class LuRoute {
71 70
         $routeCollector = new RouteCollector(new Std(), new DataGeneratorGroupCountBased());
72 71
 
73 72
         foreach ($this->getRoutes() as $route) {
74
-            $routeCollector->addRoute($route->getMethod(), $route->getUrl(), function() use($route)
73
+            $routeCollector->addRoute($route->getMethod(), $route->getUrl(), function($parameters) use($route)
75 74
             {
76 75
                 $router = static::getInstance();
77
-                return $router->execute($route);
76
+                return $router->execute($route, $parameters);
78 77
             });
79 78
         }
80 79
 
81 80
         $this->_dispatcher = new DispatcherGroupCountBased($routeCollector->getData());
82 81
     }
83 82
 
84
-    public function execute(LuRouteDbo $route)
83
+    public function execute(LuRouteDbo $route, $parameters)
85 84
     {
86 85
         $middlewares = [];
87 86
         foreach ($route->getMiddlewares() as $middlewareName) {
@@ -89,7 +88,7 @@ class LuRoute {
89 88
              * @var $middleware LuAbstractMiddleware
90 89
              */
91 90
             $middleware = new $middlewareName();
92
-            $middleware->onBefore($route);
91
+            $middleware->onBefore($route, $parameters);
93 92
             $middlewares[] = $middleware;
94 93
         }
95 94
 
@@ -104,8 +103,8 @@ class LuRoute {
104 103
             try {
105 104
                 if ($param->isOptional()) {
106 105
                     $value = null;
107
-                    if (LuBusiness::hasParam([$param->getName()])) {
108
-                        $value = $this->getParam($param, LuBusiness::getParam($param->getName()));
106
+                    if (array_key_exists($param->getName(), $parameters)) {
107
+                        $value = $this->getParam($param, $parameters[$param->getName()]);
109 108
                     }
110 109
                     else {
111 110
                         $value = $param->getDefaultValue();
@@ -113,8 +112,14 @@ class LuRoute {
113 112
                     $args[$param->getName()] = $value;
114 113
                 }
115 114
                 else {
116
-                    $args[$param->getName()] = $this->getParam($param, LuBusiness::checkParam($param->getName()));
115
+                    if (array_key_exists($param->getName(), $parameters)) {
116
+                        $args[$param->getName()] = $this->getParam($param, $parameters[$param->getName()]);
117
+                    }
118
+                    else {
119
+                        throw new LuBusinessException("Missing parameter '" . $param->getName() . "'", 400);
120
+                    }
117 121
                 }
122
+                
118 123
                 if (array_key_exists($param->getName(), $doc->getParams())) {
119 124
                     foreach ($doc->getParams()[$param->getName()]->getConstraints() as $constraint) {
120 125
                         call_user_func_array([$args[$param->getName()], $constraint->getMethod()],
@@ -125,7 +130,7 @@ class LuRoute {
125 130
             catch (LuDboConstraintException $e)
126 131
             {
127 132
                 $paramName = $param->getName();
128
-                LuBusiness::badInput("Invalid value for '${paramName}': " . $e->getMessage());
133
+                throw new LuBusinessException("Invalid value for '${paramName}': " . $e->getMessage(), 400, $e);
129 134
             }
130 135
         }
131 136
 
@@ -134,13 +139,13 @@ class LuRoute {
134 139
         $result = call_user_func_array(array($controllerInstance, $route->getControllerMethod()), $args);
135 140
 
136 141
         foreach ($middlewares as $middleware) {
137
-            $middleware->onAfter($route, $result);
142
+            $middleware->onAfter($route, $parameters, $result);
138 143
         }
139 144
 
140 145
         return $result;
141 146
     }
142 147
 
143
-    public function dispatch(string $httpMethod, string $url)
148
+    public function dispatch(string $httpMethod, string $url, $parameters)
144 149
     {
145 150
         $routeInfo = $this->_dispatcher->dispatch($httpMethod, $url);
146 151
 
@@ -152,8 +157,9 @@ class LuRoute {
152 157
         }
153 158
         else {
154 159
             $handler = $routeInfo[1];
155
-            $vars = $routeInfo[2];
156
-            $handler($vars);
160
+            $urlVars = $routeInfo[2];
161
+            $parameters = array_merge($parameters, $urlVars);
162
+            $handler($parameters);
157 163
         }
158 164
     }
159 165
     
@@ -185,8 +191,7 @@ class LuRoute {
185 191
             }
186 192
             catch (\Exception $e)
187 193
             {
188
-                LuLog::log($e);
189
-                LuBusiness::badInput("Unable to parse JSON value for '" . $param->getName() . "'");
194
+                throw new LuBusinessException("Unable to parse JSON value for '" . $param->getName() . "'", 400, $e);
190 195
             }
191 196
         }
192 197
         return $typedValue;

+ 110
- 0
src/Utils/Controller/LuticateApplication.php Wyświetl plik

@@ -0,0 +1,110 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 6/4/16
6
+ * Time: 10:06 PM
7
+ */
8
+
9
+namespace App\WebSocket;
10
+
11
+use Luticate\Utils\Controller\LuRoute;
12
+use Ratchet\ConnectionInterface;
13
+use Ratchet\MessageComponentInterface;
14
+use Ratchet\Http\HttpServer;
15
+use Ratchet\Server\IoServer;
16
+use Ratchet\WebSocket\WsServer;
17
+
18
+class LuticateApplication implements MessageComponentInterface
19
+{
20
+    private $_version = 1.0;
21
+    private $_config = null;
22
+    private $_clients = null;
23
+    
24
+    private static $_app = null;
25
+    
26
+    public static function getInstance()
27
+    {
28
+        return self::$_app;
29
+    }
30
+
31
+    /**
32
+     * @param $configuration array
33
+     */
34
+    public function __construct($configuration)
35
+    {
36
+        $this->_config = $configuration;
37
+        $this->_clients = new \SplObjectStorage;
38
+        self::$_app = $this;
39
+    }
40
+
41
+    /**
42
+     * @param $version float
43
+     */
44
+    public function setVersion($version)
45
+    {
46
+        $this->_version = $version;
47
+    }
48
+
49
+    /**
50
+     * @return float
51
+     */
52
+    public function getVersion()
53
+    {
54
+        return $this->_version;
55
+    }
56
+    
57
+    public function setupRoutes()
58
+    {
59
+        require_once __DIR__ . '/../../../../../../routes.php';
60
+        $router = LuRoute::getInstance();
61
+        
62
+        $router->setup();
63
+    }
64
+    
65
+    public function runHttp()
66
+    {
67
+        $httpMethod = $_SERVER['REQUEST_METHOD'];
68
+        $url = $_SERVER['REQUEST_URI'];
69
+        $router = LuRoute::getInstance();
70
+
71
+        $parameters = array_merge($_GET, $_POST);
72
+
73
+        $router->dispatch($httpMethod, $url, $parameters);
74
+    }
75
+    
76
+    public function runWs()
77
+    {
78
+        $server = IoServer::factory(
79
+            new HttpServer(
80
+                new WsServer(
81
+                    $this
82
+                )
83
+            ),
84
+            $this->_config['websocket']['port'],
85
+            $this->_config['websocket']['address']
86
+        );
87
+
88
+        $server->run();
89
+    }
90
+
91
+    function onMessage(ConnectionInterface $from, $msg)
92
+    {
93
+        // TODO: Implement onMessage() method.
94
+    }
95
+
96
+    function onOpen(ConnectionInterface $conn)
97
+    {
98
+        $this->_clients->attach($conn);
99
+    }
100
+
101
+    function onClose(ConnectionInterface $conn)
102
+    {
103
+        $this->_clients->detach($conn);
104
+    }
105
+
106
+    function onError(ConnectionInterface $conn, \Exception $e)
107
+    {
108
+        $conn->close();
109
+    }
110
+}

+ 2
- 2
src/Utils/Middleware/LuAbstractMiddleware.php Wyświetl plik

@@ -12,12 +12,12 @@ use Luticate\Utils\Dbo\LuRouteDbo;
12 12
 
13 13
 abstract class LuAbstractMiddleware
14 14
 {
15
-    public function onBefore(LuRouteDbo $route, ...$params)
15
+    public function onBefore(LuRouteDbo $route, $parameters, ...$params)
16 16
     {
17 17
         
18 18
     }
19 19
     
20
-    public function onAfter(LuRouteDbo $route, $result, ...$params)
20
+    public function onAfter(LuRouteDbo $route, $parameters, $result, ...$params)
21 21
     {
22 22
         
23 23
     }

+ 0
- 27
src/Utils/Middleware/ParametersMiddleware.php Wyświetl plik

@@ -1,27 +0,0 @@
1
-<?php
2
-/**
3
- * Created by PhpStorm.
4
- * User: robin
5
- * Date: 10/19/15
6
- * Time: 10:20 PM
7
- */
8
-
9
-namespace Luticate\Utils;
10
-
11
-use Closure;
12
-use Illuminate\Http\Request;
13
-
14
-class ParametersMiddleware
15
-{
16
-    /**
17
-     * @param $request Request
18
-     * @param Closure $next
19
-     * @param ...$permissions
20
-     * @return mixed
21
-     */
22
-    public function handle($request, Closure $next, ...$permissions)
23
-    {
24
-        LuBusiness::$parameters = array_merge($_GET, $_POST, $request->route()[2], LuBusiness::$parameters);
25
-        return $next($request);
26
-    }
27
-}

Ładowanie…
Anuluj
Zapisz