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 2 patches
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
24 24
     /**
25 25
      * @var array
26 26
      */
27
-    private $matcher = ['matchClosure','matchController','matchTemplate'];
27
+    private $matcher = ['matchClosure', 'matchController', 'matchTemplate'];
28 28
 
29 29
     /**
30 30
      * @var array
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
     /**
47 47
      * @param string $matcher
48 48
      */
49
-    public function addMatcher($matcher){
49
+    public function addMatcher($matcher) {
50 50
         $this->matcher[] = $matcher;
51 51
     }
52 52
 
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
     /**
62 62
      * @param array $dispatcher
63 63
      */
64
-    public function setDispatcher($dispatcher = []){
64
+    public function setDispatcher($dispatcher = []) {
65 65
         $this->dispatcher = $dispatcher;
66 66
     }
67 67
 
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
      * @return mixed|void
72 72
      * @internal param array $dispatcher
73 73
      */
74
-    public function addDispatcher($method,$class){
74
+    public function addDispatcher($method, $class) {
75 75
         $this->dispatcher[$method] = $class;
76 76
     }
77 77
 
@@ -133,13 +133,13 @@  discard block
 block discarded – undo
133 133
      */
134 134
     private function generateTarget()
135 135
     {
136
-        if($this->validMethod()) {
137
-            foreach($this->matcher as $match) if(call_user_func([$this,$match])) break;
136
+        if ($this->validMethod()) {
137
+            foreach ($this->matcher as $match) if (call_user_func([$this, $match])) break;
138 138
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
139
-            $this->router->route->addTarget('block',$this->router->collection->getRoutes('block_'.$index));
140
-            $this->router->route->addTarget('view_dir',$this->router->collection->getRoutes('view_dir_'.$index));
139
+            $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index));
140
+            $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index));
141 141
             $this->router->response->setStatusCode(202);
142
-        }else
142
+        } else
143 143
             $this->router->response->setStatusCode(405);
144 144
         return $this->router->route->hasTarget();
145 145
     }
@@ -147,9 +147,9 @@  discard block
 block discarded – undo
147 147
     /**
148 148
      *
149 149
      */
150
-    private function setRoute(){
150
+    private function setRoute() {
151 151
         if (isset($this->request['params'])) {
152
-            if(is_callable($this->request['params']))
152
+            if (is_callable($this->request['params']))
153 153
                 $this->router->route->setCallback($this->request['params']);
154 154
             else {
155 155
                 (is_array($this->request['params']) && isset($this->request['params']['use']))
@@ -167,7 +167,7 @@  discard block
 block discarded – undo
167 167
      */
168 168
     public function validMethod()
169 169
     {
170
-        if(is_callable($this->request['params']))return true;
170
+        if (is_callable($this->request['params']))return true;
171 171
         if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
172 172
             return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false;
173 173
         $method = (isset($this->router->route->getDetail()['params']['method'])) ? $this->router->route->getDetail()['params']['method'] : ['GET'];
@@ -202,7 +202,7 @@  discard block
 block discarded – undo
202 202
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
203 203
             $class = (class_exists($routes[0]))
204 204
                 ? $routes[0]
205
-                : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0];
205
+                : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0];
206 206
             if (!class_exists($class))
207 207
                 throw new \Exception('Class "' . $class . '." is not found');
208 208
             if (method_exists($class, $routes[1])) {
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
      */
226 226
     public function matchTemplate()
227 227
     {
228
-        if(is_string($this->router->route->getCallback())) {
228
+        if (is_string($this->router->route->getCallback())) {
229 229
             $path = trim($this->router->route->getCallback(), '/');
230 230
             $extension = substr(strrchr($path, "."), 1);
231 231
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
@@ -242,7 +242,7 @@  discard block
 block discarded – undo
242 242
                     }
243 243
                 }
244 244
             }
245
-            if(is_null($target))
245
+            if (is_null($target))
246 246
                 throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
247 247
             $this->router->route->setTarget([
248 248
                 'dispatcher' => $this->dispatcher['matchTemplate'],
Please login to merge, or discard this 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/Matcher/UriMatcher.php 1 patch
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -20,7 +20,7 @@  discard block
 block discarded – undo
20 20
     /**
21 21
      * @var array
22 22
      */
23
-    private $matcher = ['matchController','matchTemplate'];
23
+    private $matcher = ['matchController', 'matchTemplate'];
24 24
 
25 25
     /**
26 26
      * @var array
@@ -41,7 +41,7 @@  discard block
 block discarded – undo
41 41
     /**
42 42
      * @param string $matcher
43 43
      */
44
-    public function addMatcher($matcher){
44
+    public function addMatcher($matcher) {
45 45
         $this->matcher[] = $matcher;
46 46
     }
47 47
 
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
      * @param $class
67 67
      * @return mixed|void
68 68
      */
69
-    public function addDispatcher($method,$class){
69
+    public function addDispatcher($method, $class) {
70 70
         $this->dispatcher[$method] = $class;
71 71
     }
72 72
 
@@ -75,8 +75,8 @@  discard block
 block discarded – undo
75 75
      */
76 76
     public function match()
77 77
     {
78
-        foreach($this->matcher as $matcher){
79
-            if(call_user_func([$this,$matcher])) {
78
+        foreach ($this->matcher as $matcher) {
79
+            if (call_user_func([$this, $matcher])) {
80 80
                 $this->router->response->setStatusCode(202);
81 81
                 return true;
82 82
             }
@@ -91,12 +91,12 @@  discard block
 block discarded – undo
91 91
     {
92 92
         foreach ($this->router->getConfig()['templateExtension'] as $extension) {
93 93
             for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) {
94
-                $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '',$this->router->route->getUrl()));
94
+                $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '', $this->router->route->getUrl()));
95 95
                 $end = array_pop($url);
96
-                $url = implode('/', array_map('ucwords', $url)).'/'.$end;
96
+                $url = implode('/', array_map('ucwords', $url)) . '/' . $end;
97 97
                 if (is_file(($template = rtrim($this->router->collection->getRoutes('view_dir_' . $i), '/') . $url . $extension))) {
98
-                    $block = $this->router->collection->getRoutes('block_'.$i);
99
-                    $viewDir = $this->router->collection->getRoutes('view_dir_'.$i);
98
+                    $block = $this->router->collection->getRoutes('block_' . $i);
99
+                    $viewDir = $this->router->collection->getRoutes('view_dir_' . $i);
100 100
                     $this->router->route->setTarget([
101 101
                         'dispatcher' => $this->dispatcher['matchTemplate'],
102 102
                         'block' => $block,
@@ -119,15 +119,15 @@  discard block
 block discarded – undo
119 119
     {
120 120
         $routes = array_slice(explode('/', $this->router->route->getUrl()), 1);
121 121
         $i = 0;
122
-        do{
123
-            $route =  ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes;
122
+        do {
123
+            $route = ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes;
124 124
             if (isset($route[0])) {
125
-                $class =  (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller'))
126
-                    ? $this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller'
125
+                $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller'))
126
+                    ? $this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller'
127 127
                     : ucfirst($route[0]) . 'Controller';
128 128
                 if (isset($route[1]) && method_exists($class, $route[1])) {
129
-                    $block = $this->router->collection->getRoutes('block_'.$i);
130
-                    $viewDir = $this->router->collection->getRoutes('view_dir_'.$i);
129
+                    $block = $this->router->collection->getRoutes('block_' . $i);
130
+                    $viewDir = $this->router->collection->getRoutes('view_dir_' . $i);
131 131
                     $this->router->route->setTarget([
132 132
                         'dispatcher' => $this->dispatcher['matchController'],
133 133
                         'block' => $block,
@@ -141,7 +141,7 @@  discard block
 block discarded – undo
141 141
                 }
142 142
             }
143 143
             ++$i;
144
-        }while($i < $this->router->collection->countRoutes);
144
+        }while ($i < $this->router->collection->countRoutes);
145 145
         return false;
146 146
     }
147 147
 
Please login to merge, or discard this patch.
src/Routing/Matcher/MatcherInterface.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -41,5 +41,5 @@
 block discarded – undo
41 41
      * @param $class
42 42
      * @return mixed
43 43
      */
44
-    public function addDispatcher($method,$class);
44
+    public function addDispatcher($method, $class);
45 45
 }
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.