Completed
Push — master ( 46d40e...8f6f9d )
by Sinnarasa
02:37
created
src/Routing/Dispatcher/TemplateDispatcher.php 1 patch
Braces   +6 added lines, -5 removed lines patch added patch discarded remove patch
@@ -47,11 +47,12 @@
 block discarded – undo
47 47
      */
48 48
     public function call()
49 49
     {
50
-        if ($this->response->getStatusCode() == 202)
51
-            $this->setContentType($this->route->getTarget('extension'));
52
-        if (isset($this->route->getTarget('callback')[$this->route->getTarget('extension')]))
53
-            $this->response->setContent(call_user_func_array($this->route->getTarget('callback')[$this->route->getTarget('extension')], [$this->route]));
54
-        else {
50
+        if ($this->response->getStatusCode() == 202) {
51
+                    $this->setContentType($this->route->getTarget('extension'));
52
+        }
53
+        if (isset($this->route->getTarget('callback')[$this->route->getTarget('extension')])) {
54
+                    $this->response->setContent(call_user_func_array($this->route->getTarget('callback')[$this->route->getTarget('extension')], [$this->route]));
55
+        } else {
55 56
             ob_start();
56 57
             require($this->route->getTarget('template'));
57 58
             $this->response->setContent(ob_get_clean());
Please login to merge, or discard this patch.
src/Routing/Matcher/ArrayMatcher.php 1 patch
Braces   +36 added lines, -21 removed lines patch added patch discarded remove patch
@@ -118,8 +118,9 @@  discard block
 block discarded – undo
118 118
     {
119 119
         if (substr($this->request['route'], -1) == '*') {
120 120
             $pos = strpos($this->request['route'], '*');
121
-            if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos))
122
-                if (isset($this->request)) return true;
121
+            if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos)) {
122
+                            if (isset($this->request)) return true;
123
+            }
123 124
         }
124 125
         if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) {
125 126
             array_shift($this->request['parameters']);
@@ -134,13 +135,16 @@  discard block
 block discarded – undo
134 135
     private function generateTarget()
135 136
     {
136 137
         if($this->validMethod()) {
137
-            foreach($this->matcher as $match) if(call_user_func([$this,$match])) break;
138
+            foreach($this->matcher as $match) {
139
+                if(call_user_func([$this,$match])) break;
140
+            }
138 141
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
139 142
             $this->router->route->addTarget('block',$this->router->collection->getRoutes('block_'.$index));
140 143
             $this->router->route->addTarget('view_dir',$this->router->collection->getRoutes('view_dir_'.$index));
141 144
             $this->router->response->setStatusCode(202);
142
-        }else
143
-            $this->router->response->setStatusCode(405);
145
+        } else {
146
+                    $this->router->response->setStatusCode(405);
147
+        }
144 148
         return $this->router->route->hasTarget();
145 149
     }
146 150
 
@@ -149,14 +153,18 @@  discard block
 block discarded – undo
149 153
      */
150 154
     private function setRoute(){
151 155
         if (isset($this->request['params'])) {
152
-            if(is_callable($this->request['params']))
153
-                $this->router->route->setCallback($this->request['params']);
154
-            else {
156
+            if(is_callable($this->request['params'])) {
157
+                            $this->router->route->setCallback($this->request['params']);
158
+            } else {
155 159
                 (is_array($this->request['params']) && isset($this->request['params']['use']))
156 160
                     ? $this->router->route->setCallback($this->request['params']['use'])
157 161
                     : $this->router->route->setCallback($this->request['params']);
158
-                if (isset($this->request['params']['name'])) $this->router->route->setName($this->request['params']['name']);
159
-                if (isset($this->request['params']['method'])) $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']];
162
+                if (isset($this->request['params']['name'])) {
163
+                    $this->router->route->setName($this->request['params']['name']);
164
+                }
165
+                if (isset($this->request['params']['method'])) {
166
+                    $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']];
167
+                }
160 168
             }
161 169
         }
162 170
         $this->router->route->setDetail($this->request);
@@ -167,9 +175,12 @@  discard block
 block discarded – undo
167 175
      */
168 176
     public function validMethod()
169 177
     {
170
-        if(is_callable($this->request['params']))return true;
171
-        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
172
-            return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false;
178
+        if(is_callable($this->request['params'])) {
179
+            return true;
180
+        }
181
+        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
182
+                    return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false;
183
+        }
173 184
         $method = (isset($this->router->route->getDetail()['params']['method'])) ? $this->router->route->getDetail()['params']['method'] : ['GET'];
174 185
         return (in_array($this->router->route->getMethod(), $method)) ? true : false;
175 186
     }
@@ -198,13 +209,16 @@  discard block
 block discarded – undo
198 209
     {
199 210
         if (strpos($this->router->route->getCallback(), '@') !== false) {
200 211
             $routes = explode('@', $this->router->route->getCallback());
201
-            if (!isset($routes[1])) $routes[1] = 'index';
212
+            if (!isset($routes[1])) {
213
+                $routes[1] = 'index';
214
+            }
202 215
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
203 216
             $class = (class_exists($routes[0]))
204 217
                 ? $routes[0]
205 218
                 : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0];
206
-            if (!class_exists($class))
207
-                throw new \Exception('Class "' . $class . '." is not found');
219
+            if (!class_exists($class)) {
220
+                            throw new \Exception('Class "' . $class . '." is not found');
221
+            }
208 222
             if (method_exists($class, $routes[1])) {
209 223
                 $this->router->route->setTarget([
210 224
                     'dispatcher' => $this->dispatcher['matchController'],
@@ -231,9 +245,9 @@  discard block
 block discarded – undo
231 245
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
232 246
             $viewDir = $this->router->collection->getRoutes('view_dir_' . $index);
233 247
             $target = null;
234
-            if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && is_file($viewDir . $path))
235
-                $target = $viewDir . $path;
236
-            else {
248
+            if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && is_file($viewDir . $path)) {
249
+                            $target = $viewDir . $path;
250
+            } else {
237 251
                 foreach ($this->router->getConfig()['templateExtension'] as $ext) {
238 252
                     if (is_file($viewDir . $path . $ext)) {
239 253
                         $target = $viewDir . $path . $ext;
@@ -242,8 +256,9 @@  discard block
 block discarded – undo
242 256
                     }
243 257
                 }
244 258
             }
245
-            if(is_null($target))
246
-                throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
259
+            if(is_null($target)) {
260
+                            throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
261
+            }
247 262
             $this->router->route->setTarget([
248 263
                 'dispatcher' => $this->dispatcher['matchTemplate'],
249 264
                 'template'   => $target,
Please login to merge, or discard this patch.
src/Routing/Middleware.php 1 patch
Braces   +28 added lines, -16 removed lines patch added patch discarded remove patch
@@ -29,11 +29,14 @@  discard block
 block discarded – undo
29 29
      */
30 30
     public function globalMiddleware()
31 31
     {
32
-        if (isset($this->router->collection->middleware['global_middleware']))
33
-            foreach ($this->router->collection->middleware['global_middleware'] as $mid) {
32
+        if (isset($this->router->collection->middleware['global_middleware'])) {
33
+                    foreach ($this->router->collection->middleware['global_middleware'] as $mid) {
34 34
                 if (class_exists($mid)) {
35
-                    $mid_global = call_user_func($this->router->getConfig()['di'],$mid);
36
-                    if (method_exists($mid_global, 'handle')) $this->callHandler($mid_global);
35
+                    $mid_global = call_user_func($this->router->getConfig()['di'],$mid);
36
+        }
37
+                    if (method_exists($mid_global, 'handle')) {
38
+                        $this->callHandler($mid_global);
39
+                    }
37 40
                 }
38 41
             }
39 42
     }
@@ -43,11 +46,14 @@  discard block
 block discarded – undo
43 46
      */
44 47
     public function blockMiddleware()
45 48
     {
46
-        if (isset($this->router->collection->middleware['block_middleware']))
47
-            if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) {
48
-                $class = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')];
49
+        if (isset($this->router->collection->middleware['block_middleware'])) {
50
+                    if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) {
51
+                $class = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')];
52
+        }
49 53
                 $mid_block = call_user_func($this->router->getConfig()['di'],$class);
50
-                if (method_exists($mid_block, 'handle')) $this->callHandler($mid_block);
54
+                if (method_exists($mid_block, 'handle')) {
55
+                    $this->callHandler($mid_block);
56
+                }
51 57
             }
52 58
     }
53 59
 
@@ -61,7 +67,9 @@  discard block
 block discarded – undo
61 67
             if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) {
62 68
                 $class = $this->router->collection->middleware['class_middleware'][$ctrl];
63 69
                 $mid_class = call_user_func($this->router->getConfig()['di'],$class);
64
-                if (method_exists($mid_class, 'handle')) $this->callHandler($mid_class);
70
+                if (method_exists($mid_class, 'handle')) {
71
+                    $this->callHandler($mid_class);
72
+                }
65 73
             }
66 74
         }
67 75
     }
@@ -71,11 +79,14 @@  discard block
 block discarded – undo
71 79
      */
72 80
     public function routeMiddleware()
73 81
     {
74
-        if (isset($this->router->collection->middleware['route_middleware']))
75
-            if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) {
76
-                $class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']];
82
+        if (isset($this->router->collection->middleware['route_middleware'])) {
83
+                    if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) {
84
+                $class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']];
85
+        }
77 86
                 $mid_route = call_user_func($this->router->getConfig()['di'],$class);
78
-                if (method_exists($mid_route, 'handle')) $this->callHandler($mid_route);
87
+                if (method_exists($mid_route, 'handle')) {
88
+                    $this->callHandler($mid_route);
89
+                }
79 90
             }
80 91
     }
81 92
 
@@ -86,9 +97,10 @@  discard block
 block discarded – undo
86 97
     private function callHandler($instance){
87 98
         $reflectionMethod = new ReflectionMethod($instance, 'handle');
88 99
         $dependencies = [];
89
-        foreach ($reflectionMethod->getParameters() as $arg)
90
-            if (!is_null($arg->getClass()))
91
-                $dependencies[] = $this->getClass($arg->getClass()->name);
100
+        foreach ($reflectionMethod->getParameters() as $arg) {
101
+                    if (!is_null($arg->getClass()))
102
+                $dependencies[] = $this->getClass($arg->getClass()->name);
103
+        }
92 104
         $dependencies = array_merge($dependencies,[$this->router->route]);
93 105
         return $reflectionMethod->invokeArgs($instance, $dependencies);
94 106
     }
Please login to merge, or discard this patch.