Completed
Push — master ( f9a3f2...2eb481 )
by Sinnarasa
02:45
created
src/Routing/Matcher/ArrayMatcher.php 1 patch
Braces   +34 added lines, -21 removed lines patch added patch discarded remove patch
@@ -125,8 +125,9 @@  discard block
 block discarded – undo
125 125
     {
126 126
         if (substr($this->request['route'], -1) == '*') {
127 127
             $pos = strpos($this->request['route'], '*');
128
-            if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params']))
129
-                return true;
128
+            if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) {
129
+                            return true;
130
+            }
130 131
         }
131 132
         if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) {
132 133
             array_shift($this->request['parameters']);
@@ -140,10 +141,11 @@  discard block
 block discarded – undo
140 141
      */
141 142
     private function generateTarget()
142 143
     {
143
-        if($this->validMethod())
144
-            foreach($this->resolver as $resolver)
144
+        if($this->validMethod()) {
145
+                    foreach($this->resolver as $resolver)
145 146
                 if (is_array($target = call_user_func_array([$this,$resolver],[$this->router->route->getCallback()]))) {
146
-                    $this->setTarget($target);
147
+                    $this->setTarget($target);
148
+        }
147 149
                     $this->router->response->setStatusCode(202);
148 150
                     return true;
149 151
                 }
@@ -167,14 +169,18 @@  discard block
 block discarded – undo
167 169
      */
168 170
     private function setCallback(){
169 171
         if (isset($this->request['params'])) {
170
-            if(is_callable($this->request['params']))
171
-                $this->router->route->setCallback($this->request['params']);
172
-            else {
172
+            if(is_callable($this->request['params'])) {
173
+                            $this->router->route->setCallback($this->request['params']);
174
+            } else {
173 175
                 (is_array($this->request['params']) && isset($this->request['params']['use']))
174 176
                     ? $this->router->route->setCallback($this->request['params']['use'])
175 177
                     : $this->router->route->setCallback($this->request['params']);
176
-                if (isset($this->request['params']['name'])) $this->router->route->setName($this->request['params']['name']);
177
-                if (isset($this->request['params']['method'])) $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']];
178
+                if (isset($this->request['params']['name'])) {
179
+                    $this->router->route->setName($this->request['params']['name']);
180
+                }
181
+                if (isset($this->request['params']['method'])) {
182
+                    $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']];
183
+                }
178 184
             }
179 185
         }
180 186
     }
@@ -184,9 +190,12 @@  discard block
 block discarded – undo
184 190
      */
185 191
     public function validMethod()
186 192
     {
187
-        if(is_callable($this->request['params']))return true;
188
-        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
189
-            return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false;
193
+        if(is_callable($this->request['params'])) {
194
+            return true;
195
+        }
196
+        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
197
+                    return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false;
198
+        }
190 199
         $method = (isset($this->request['params']['method'])) ? $this->request['params']['method'] : ['GET'];
191 200
         return (in_array($this->router->route->getMethod(), $method)) ? true : false;
192 201
     }
@@ -250,13 +259,16 @@  discard block
 block discarded – undo
250 259
     {
251 260
         if (is_string($callback) && strpos($callback, '@') !== false) {
252 261
             $routes = explode('@', $callback);
253
-            if (!isset($routes[1])) $routes[1] = 'index';
262
+            if (!isset($routes[1])) {
263
+                $routes[1] = 'index';
264
+            }
254 265
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
255 266
             $class = (class_exists($routes[0]))
256 267
                 ? $routes[0]
257 268
                 : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0];
258
-            if (!class_exists($class))
259
-                throw new \Exception('Class "' . $class . '." is not found');
269
+            if (!class_exists($class)) {
270
+                            throw new \Exception('Class "' . $class . '." is not found');
271
+            }
260 272
             if (method_exists($class, $routes[1])) {
261 273
                 return [
262 274
                     'dispatcher' => $this->dispatcher['matchController'],
@@ -283,9 +295,9 @@  discard block
 block discarded – undo
283 295
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
284 296
             $viewDir = $this->router->collection->getRoutes('view_dir_' . $index);
285 297
             $target = null;
286
-            if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path)))
287
-                $target = $fullPath;
288
-            else {
298
+            if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) {
299
+                            $target = $fullPath;
300
+            } else {
289 301
                 foreach ($this->router->getConfig()['templateExtension'] as $ext) {
290 302
                     if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) {
291 303
                         $target = $fullPath;
@@ -294,8 +306,9 @@  discard block
 block discarded – undo
294 306
                     }
295 307
                 }
296 308
             }
297
-            if(is_null($target))
298
-                throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
309
+            if(is_null($target)) {
310
+                            throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
311
+            }
299 312
             return [
300 313
                 'dispatcher' => $this->dispatcher['matchTemplate'],
301 314
                 'template'   => $target,
Please login to merge, or discard this patch.
src/Routing/Router.php 1 patch
Braces   +16 added lines, -10 removed lines patch added patch discarded remove patch
@@ -79,8 +79,9 @@  discard block
 block discarded – undo
79 79
      * @param object|array $matcher
80 80
      */
81 81
     public function setMatcher($matcher){
82
-        if(is_object($matcher))
83
-            $matcher = [$matcher];
82
+        if(is_object($matcher)) {
83
+                    $matcher = [$matcher];
84
+        }
84 85
         $this->matcher = $matcher;
85 86
     }
86 87
 
@@ -97,7 +98,9 @@  discard block
 block discarded – undo
97 98
     public function run()
98 99
     {
99 100
         $this->setUrl();
100
-        if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath();
101
+        if ($this->config['generateRoutesPath']) {
102
+            $this->collection->generateRoutesPath();
103
+        }
101 104
         if ($this->match()) {
102 105
             $this->handle();
103 106
             $this->callTarget();
@@ -110,8 +113,9 @@  discard block
 block discarded – undo
110 113
      */
111 114
     public function setUrl($url = null)
112 115
     {
113
-        if (is_null($url))
114
-            $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
116
+        if (is_null($url)) {
117
+                    $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
118
+        }
115 119
         $this->route->setUrl('/' . trim(explode('?', $url)[0], '/'));
116 120
     }
117 121
 
@@ -121,8 +125,9 @@  discard block
 block discarded – undo
121 125
     public function match()
122 126
     {
123 127
         foreach ($this->matcher as $key => $matcher) {
124
-            if (call_user_func([$this->matcher[$key], 'match']))
125
-                return true;
128
+            if (call_user_func([$this->matcher[$key], 'match'])) {
129
+                            return true;
130
+            }
126 131
         }
127 132
         return false;
128 133
     }
@@ -169,9 +174,10 @@  discard block
 block discarded – undo
169 174
         if (isset($this->route->getDetail()['response_templates']) && isset($this->route->getDetail()['response_templates'][$code = $this->response->getStatusCode()])) {
170 175
             $this->route->setCallback($this->route->getDetail()['response_templates'][$code]);
171 176
             foreach($this->matcher as $key => $instance) {
172
-                foreach (call_user_func([$instance, 'getResolver']) as $match)
173
-                    if (is_array($target = call_user_func_array([$instance, $match], [$this->route->getCallback()]))){
174
-                        call_user_func_array([$instance, 'setTarget'],[$target]);
177
+                foreach (call_user_func([$instance, 'getResolver']) as $match) {
178
+                                    if (is_array($target = call_user_func_array([$instance, $match], [$this->route->getCallback()]))){
179
+                        call_user_func_array([$instance, 'setTarget'],[$target]);
180
+                }
175 181
                         $this->callTarget();
176 182
                         break;
177 183
                     }
Please login to merge, or discard this patch.