Completed
Push — master ( 2eb481...e34dd2 )
by Sinnarasa
02:44
created
src/Routing/Matcher/ArrayMatcher.php 1 patch
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
     /**
26 26
      * @var array
27 27
      */
28
-    private $resolver = ['isClosureAndTemplate','isControllerAndTemplate','isTemplate'];
28
+    private $resolver = ['isClosureAndTemplate', 'isControllerAndTemplate', 'isTemplate'];
29 29
 
30 30
     /**
31 31
      * @var array
@@ -34,8 +34,8 @@  discard block
 block discarded – undo
34 34
         'isClosure' => 'JetFire\Routing\Dispatcher\ClosureDispatcher',
35 35
         'isController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher',
36 36
         'isTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher',
37
-        'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'],
38
-        'isClosureAndTemplate' => ['JetFire\Routing\Dispatcher\ClosureDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'],
37
+        'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'],
38
+        'isClosureAndTemplate' => ['JetFire\Routing\Dispatcher\ClosureDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'],
39 39
     ];
40 40
 
41 41
     /**
@@ -49,14 +49,14 @@  discard block
 block discarded – undo
49 49
     /**
50 50
      * @param array $resolver
51 51
      */
52
-    public function setResolver($resolver = []){
52
+    public function setResolver($resolver = []) {
53 53
         $this->resolver = $resolver;
54 54
     }
55 55
 
56 56
     /**
57 57
      * @param string $resolver
58 58
      */
59
-    public function addResolver($resolver){
59
+    public function addResolver($resolver) {
60 60
         $this->resolver[] = $resolver;
61 61
     }
62 62
 
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
     /**
72 72
      * @param array $dispatcher
73 73
      */
74
-    public function setDispatcher($dispatcher = []){
74
+    public function setDispatcher($dispatcher = []) {
75 75
         $this->dispatcher = $dispatcher;
76 76
     }
77 77
 
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
      * @return mixed|void
82 82
      * @internal param array $dispatcher
83 83
      */
84
-    public function addDispatcher($method,$class){
84
+    public function addDispatcher($method, $class) {
85 85
         $this->dispatcher[$method] = $class;
86 86
     }
87 87
 
@@ -142,9 +142,9 @@  discard block
 block discarded – undo
142 142
      */
143 143
     private function generateTarget()
144 144
     {
145
-        if($this->validMethod())
146
-            foreach($this->resolver as $resolver)
147
-                if (is_array($target = call_user_func_array([$this,$resolver],[$this->router->route->getCallback()]))) {
145
+        if ($this->validMethod())
146
+            foreach ($this->resolver as $resolver)
147
+                if (is_array($target = call_user_func_array([$this, $resolver], [$this->router->route->getCallback()]))) {
148 148
                     $this->setTarget($target);
149 149
                     $this->router->response->setStatusCode(202);
150 150
                     return true;
@@ -156,20 +156,20 @@  discard block
 block discarded – undo
156 156
     /**
157 157
      * @param array $target
158 158
      */
159
-    public function setTarget($target = []){
159
+    public function setTarget($target = []) {
160 160
         $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
161 161
         $this->router->route->setDetail($this->request);
162 162
         $this->router->route->setTarget($target);
163
-        $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_'.$index));
164
-        $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_'.$index));
163
+        $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index));
164
+        $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index));
165 165
     }
166 166
 
167 167
     /**
168 168
      *
169 169
      */
170
-    private function setCallback(){
170
+    private function setCallback() {
171 171
         if (isset($this->request['params'])) {
172
-            if(is_callable($this->request['params']))
172
+            if (is_callable($this->request['params']))
173 173
                 $this->router->route->setCallback($this->request['params']);
174 174
             else {
175 175
                 (is_array($this->request['params']) && isset($this->request['params']['use']))
@@ -186,7 +186,7 @@  discard block
 block discarded – undo
186 186
      */
187 187
     public function validMethod()
188 188
     {
189
-        if(is_callable($this->request['params']))return true;
189
+        if (is_callable($this->request['params']))return true;
190 190
         if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
191 191
             return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false;
192 192
         $method = (isset($this->request['params']['method'])) ? $this->request['params']['method'] : ['GET'];
@@ -198,10 +198,10 @@  discard block
 block discarded – undo
198 198
      * @return array|bool
199 199
      * @throws \Exception
200 200
      */
201
-    public function isClosureAndTemplate($callback){
202
-        if(is_array($cls = $this->isClosure($callback))) {
201
+    public function isClosureAndTemplate($callback) {
202
+        if (is_array($cls = $this->isClosure($callback))) {
203 203
             if (is_array($this->request['params']) && isset($this->request['params']['template']) && is_array($tpl = $this->isTemplate($this->request['params']['template']))) {
204
-                return array_merge(array_merge($cls, $tpl),[
204
+                return array_merge(array_merge($cls, $tpl), [
205 205
                     'dispatcher' => $this->dispatcher['isClosureAndTemplate']
206 206
                 ]);
207 207
             }
@@ -215,10 +215,10 @@  discard block
 block discarded – undo
215 215
      * @return array|bool
216 216
      * @throws \Exception
217 217
      */
218
-    public function isControllerAndTemplate($callback){
219
-        if(is_array($ctrl = $this->isController($callback))) {
218
+    public function isControllerAndTemplate($callback) {
219
+        if (is_array($ctrl = $this->isController($callback))) {
220 220
             if (is_array($this->request['params']) && isset($this->request['params']['template']) && is_array($tpl = $this->isTemplate($this->request['params']['template']))) {
221
-                return array_merge(array_merge($ctrl, $tpl),[
221
+                return array_merge(array_merge($ctrl, $tpl), [
222 222
                     'dispatcher' => $this->dispatcher['isControllerAndTemplate']
223 223
                 ]);
224 224
             }
@@ -256,7 +256,7 @@  discard block
 block discarded – undo
256 256
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
257 257
             $class = (class_exists($routes[0]))
258 258
                 ? $routes[0]
259
-                : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0];
259
+                : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0];
260 260
             if (!class_exists($class))
261 261
                 throw new \Exception('Class "' . $class . '." is not found');
262 262
             if (method_exists($class, $routes[1])) {
@@ -279,7 +279,7 @@  discard block
 block discarded – undo
279 279
      */
280 280
     public function isTemplate($callback)
281 281
     {
282
-        if(is_string($callback)) {
282
+        if (is_string($callback)) {
283 283
             $path = trim($callback, '/');
284 284
             $extension = substr(strrchr($path, "."), 1);
285 285
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
@@ -296,7 +296,7 @@  discard block
 block discarded – undo
296 296
                     }
297 297
                 }
298 298
             }
299
-            if(is_null($target))
299
+            if (is_null($target))
300 300
                 throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
301 301
             return [
302 302
                 'dispatcher' => $this->dispatcher['isTemplate'],
Please login to merge, or discard this patch.
src/Routing/Router.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -49,13 +49,13 @@  discard block
 block discarded – undo
49 49
      * @param ResponseInterface $response
50 50
      * @param Route $route
51 51
      */
52
-    public function __construct(RouteCollection $collection,ResponseInterface $response = null,Route $route = null)
52
+    public function __construct(RouteCollection $collection, ResponseInterface $response = null, Route $route = null)
53 53
     {
54 54
         $this->collection = $collection;
55
-        $this->response = is_null($response)? new Response() : $response;
55
+        $this->response = is_null($response) ? new Response() : $response;
56 56
         $this->response->setStatusCode(404);
57
-        $this->route = is_null($route)? new Route() : $route;
58
-        $this->config['di'] = function($class){
57
+        $this->route = is_null($route) ? new Route() : $route;
58
+        $this->config['di'] = function($class) {
59 59
             return new $class;
60 60
         };
61 61
     }
@@ -79,8 +79,8 @@  discard block
 block discarded – undo
79 79
     /**
80 80
      * @param object|array $matcher
81 81
      */
82
-    public function setMatcher($matcher){
83
-        if(is_object($matcher))
82
+    public function setMatcher($matcher) {
83
+        if (is_object($matcher))
84 84
             $matcher = [$matcher];
85 85
         $this->matcher = $matcher;
86 86
     }
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
     /**
89 89
      * @param string $matcher
90 90
      */
91
-    public function addMatcher($matcher){
91
+    public function addMatcher($matcher) {
92 92
         $this->matcher[] = $matcher;
93 93
     }
94 94
 
@@ -133,9 +133,9 @@  discard block
 block discarded – undo
133 133
      */
134 134
     public function callTarget()
135 135
     {
136
-        $target = is_array($this->route->getTarget('dispatcher'))?$this->route->getTarget('dispatcher'):[$this->route->getTarget('dispatcher')];
137
-        if(!empty($target)) {
138
-            foreach($target as $call) {
136
+        $target = is_array($this->route->getTarget('dispatcher')) ? $this->route->getTarget('dispatcher') : [$this->route->getTarget('dispatcher')];
137
+        if (!empty($target)) {
138
+            foreach ($target as $call) {
139 139
                 $this->dispatcher = new $call($this->route, $this->response);
140 140
                 call_user_func([$this->dispatcher, 'call']);
141 141
             }
@@ -170,11 +170,11 @@  discard block
 block discarded – undo
170 170
         if (isset($this->route->getDetail()['response_templates']) && isset($this->route->getDetail()['response_templates'][$code = $this->response->getStatusCode()])) {
171 171
             $this->route->setCallback($this->route->getDetail()['response_templates'][$code]);
172 172
             $matcher = null;
173
-            foreach($this->matcher as $instance) if($instance instanceof ArrayMatcher) $matcher = $instance;
174
-            if(is_null($matcher))$matcher = new ArrayMatcher($this);
173
+            foreach ($this->matcher as $instance) if ($instance instanceof ArrayMatcher) $matcher = $instance;
174
+            if (is_null($matcher))$matcher = new ArrayMatcher($this);
175 175
             foreach (call_user_func([$matcher, 'getResolver']) as $match)
176
-                if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))){
177
-                    call_user_func_array([$matcher, 'setTarget'],[$target]);
176
+                if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))) {
177
+                    call_user_func_array([$matcher, 'setTarget'], [$target]);
178 178
                     $this->callTarget();
179 179
                     break;
180 180
                 }
Please login to merge, or discard this patch.