Passed
Push — master ( 814b1f...e0eeef )
by Henri
01:17
created
src/Helper.php 2 patches
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
     }
44 44
 
45 45
     protected function ControllerForm($controller, string $method, array $values){
46
-		$this->checkRole();
46
+        $this->checkRole();
47 47
         $method = ($method !== 'method') ? $method : $this->getData()['POST']['role'];
48 48
         $data = (array_key_exists('data',$values)) ? json_decode($values['data'], true) : null;
49 49
 
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
         }
158 158
         
159 159
         $this->currentRoute = null;
160
-	    throw new Exception('Page not found.',404);
160
+        throw new Exception('Page not found.',404);
161 161
     }
162 162
 
163 163
 }
Please login to merge, or discard this patch.
Spacing   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait Helper{
7
+trait Helper {
8 8
     use CheckTrait, ControllerTrait;
9 9
     
10 10
     private $currentRoute = null;
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
27 27
     protected function getProtocol(): string
28 28
     {
29 29
         $protocol = ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? 'ajax' : 'get';
30
-        $protocol = (array_key_existS('HTTP_REQUESTED_METHOD',$_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol;
30
+        $protocol = (array_key_existS('HTTP_REQUESTED_METHOD', $_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol;
31 31
             
32 32
         return $protocol;
33 33
     }
@@ -42,12 +42,12 @@  discard block
 block discarded – undo
42 42
         ];
43 43
     }
44 44
 
45
-    protected function ControllerForm($controller, string $method, array $values){
45
+    protected function ControllerForm($controller, string $method, array $values) {
46 46
 		$this->checkRole();
47 47
         $method = ($method !== 'method') ? $method : $this->getData()['POST']['role'];
48
-        $data = (array_key_exists('data',$values)) ? json_decode($values['data'], true) : null;
48
+        $data = (array_key_exists('data', $values)) ? json_decode($values['data'], true) : null;
49 49
 
50
-        call_user_func_array([$controller,$method],  $data);
50
+        call_user_func_array([$controller, $method], $data);
51 51
     }
52 52
 
53 53
     protected function Controller(string $controll): void
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
         $data = $this->getData();
56 56
 
57 57
         foreach ($data['GET'] as $name => $value) {
58
-            $controll = str_replace('{'.$name.'}',$value,$controll);
58
+            $controll = str_replace('{'.$name.'}', $value, $controll);
59 59
         }
60 60
 
61 61
         $this->checkControllsettable($controll);
@@ -64,30 +64,30 @@  discard block
 block discarded – undo
64 64
 
65 65
         $this->checkControllmethod($controll);
66 66
 
67
-        $controller = ucfirst(explode(':',$controll)[0]);
67
+        $controller = ucfirst(explode(':', $controll)[0]);
68 68
         $controller = new $controller();
69
-        $method = explode(':',$controll)[1];
69
+        $method = explode(':', $controll)[1];
70 70
 
71
-        if( ($this->getProtocol() == 'form') ){
71
+        if (($this->getProtocol() == 'form')) {
72 72
             $this->ControllerForm($controller, $method, $data['POST']);
73 73
         }else {
74
-            $data = (array_key_exists('data',$data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET'];
75
-            call_user_func_array([$controller,$method],  $data);
74
+            $data = (array_key_exists('data', $data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET'];
75
+            call_user_func_array([$controller, $method], $data);
76 76
         }
77 77
        
78 78
     }    
79 79
 
80
-    protected function explodeRoutes(bool $bar, string $url ,bool $bar_, string $url_): array
80
+    protected function explodeRoutes(bool $bar, string $url, bool $bar_, string $url_): array
81 81
     {   
82
-        $url = $bar ? substr($url, 0, -1) : $url ;
83
-        $url = explode('/',$url);
82
+        $url = $bar ? substr($url, 0, -1) : $url;
83
+        $url = explode('/', $url);
84 84
 
85
-        $url_ = $bar_ ? substr($url_, 0, -1) : $url_ ;
86
-        $url_ = explode('/',$url_);
85
+        $url_ = $bar_ ? substr($url_, 0, -1) : $url_;
86
+        $url_ = explode('/', $url_);
87 87
 
88
-        foreach($url as $ur => $u){
89
-            if(substr($u,0,2) === '{?'){
90
-                if(!array_key_exists($ur,$url_)){
88
+        foreach ($url as $ur => $u) {
89
+            if (substr($u, 0, 2) === '{?') {
90
+                if (!array_key_exists($ur, $url_)) {
91 91
                     $url_[$ur] = '';
92 92
                 };
93 93
             }
@@ -98,28 +98,28 @@  discard block
 block discarded – undo
98 98
 
99 99
     protected function toHiking(array $route)
100 100
     {
101
-        $this->callOnRoute($route,'beforeAll');
102
-        $this->callOnRoute($route,'before');
101
+        $this->callOnRoute($route, 'beforeAll');
102
+        $this->callOnRoute($route, 'before');
103 103
 
104
-        if(is_string($route['role'])){
104
+        if (is_string($route['role'])) {
105 105
             $this->Controller($route['role']);
106
-            $this->callOnRoute($route,'after');
107
-            $this->callOnRoute($route,'afterAll');
106
+            $this->callOnRoute($route, 'after');
107
+            $this->callOnRoute($route, 'afterAll');
108 108
             return true;
109 109
         }
110 110
 
111
-        call_user_func_array($route['role'],$this->getData()['GET']);
111
+        call_user_func_array($route['role'], $this->getData()['GET']);
112 112
 
113
-        $this->callOnRoute($route,'after');
114
-        $this->callOnRoute($route,'afterAll');
113
+        $this->callOnRoute($route, 'after');
114
+        $this->callOnRoute($route, 'afterAll');
115 115
     }
116 116
 
117
-    protected function callOnRoute(array $route,string $state)
117
+    protected function callOnRoute(array $route, string $state)
118 118
     {
119
-        if($route[$state] !== null){
120
-            if(is_string($route[$state])){
119
+        if ($route[$state] !== null) {
120
+            if (is_string($route[$state])) {
121 121
                 $this->Controller($route[$state]);
122
-            }else{
122
+            }else {
123 123
                 $route[$state]();
124 124
             }
125 125
         }
@@ -129,12 +129,12 @@  discard block
 block discarded – undo
129 129
     {
130 130
         $currentProtocol = $this->getProtocol();
131 131
 
132
-        foreach(array_reverse($this->routers) as $r => $route){
132
+        foreach (array_reverse($this->routers) as $r => $route) {
133 133
 
134 134
             $this->currentRoute = $route;
135 135
             $this->currentRoute['name'] = $r;
136 136
 
137
-            if(!$this->checkProtocol($route['protocol'], $currentProtocol)){
137
+            if (!$this->checkProtocol($route['protocol'], $currentProtocol)) {
138 138
                 continue;
139 139
             }
140 140
 
@@ -144,11 +144,11 @@  discard block
 block discarded – undo
144 144
 
145 145
 
146 146
             $routs = $this->explodeRoutes(
147
-                (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url'],
148
-                (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']
147
+                (substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url'],
148
+                (substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']
149 149
             );
150 150
 
151
-            if(!$this->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])){
151
+            if (!$this->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])) {
152 152
                 continue;
153 153
             }
154 154
 
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
         }
158 158
         
159 159
         $this->currentRoute = null;
160
-	    throw new Exception('Page not found.',404);
160
+	    throw new Exception('Page not found.', 404);
161 161
     }
162 162
 
163 163
 }
Please login to merge, or discard this patch.
src/Router.php 2 patches
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -43,20 +43,20 @@  discard block
 block discarded – undo
43 43
     {
44 44
         $this->lastReturn = null;
45 45
         
46
-		$url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url;
46
+        $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url;
47 47
 
48
-    	foreach($this->routers as $key => $value){
49
-    		if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
48
+        foreach($this->routers as $key => $value){
49
+            if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
50 50
                 throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured.");
51 51
             }
52 52
         }
53 53
         
54 54
         $this->checkTypeRole($walking);
55 55
 
56
-		$route = [
57
-			'url' => $this->prefix.$url,
58
-			'role' => $walking,
59
-			'protocol' => $protocol,
56
+        $route = [
57
+            'url' => $this->prefix.$url,
58
+            'role' => $walking,
59
+            'protocol' => $protocol,
60 60
             'filters' => null,
61 61
             'where' => null,
62 62
             'before' => null,
@@ -64,9 +64,9 @@  discard block
 block discarded – undo
64 64
             'after' => null,
65 65
             'afterAll' => $this->afterAll,
66 66
             'group' => self::getInstance()->group
67
-		];
67
+        ];
68 68
 
69
-		$this->routers[] = $route;		
69
+        $this->routers[] = $route;		
70 70
         
71 71
         return self::getInstance();
72 72
     }
@@ -148,7 +148,7 @@  discard block
 block discarded – undo
148 148
             return $instance;
149 149
         }
150 150
 
151
-		return $instance->loadFromArrays();
151
+        return $instance->loadFromArrays();
152 152
     }
153 153
 
154 154
     public static function dispatch(?string $routeName = null): bool
Please login to merge, or discard this patch.
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-class Router{
7
+class Router {
8 8
     use Helper, CheckTrait, CheckWhere, DefinitionsTrait, ExtraJobsTrait;
9 9
 
10 10
     private static $instance = null;
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
 
27 27
     public static function create(): Router
28 28
     {
29
-        if(!self::getInstance()->instanced){
29
+        if (!self::getInstance()->instanced) {
30 30
             self::getInstance()->instanced = true;
31 31
         }
32 32
         
@@ -39,14 +39,14 @@  discard block
 block discarded – undo
39 39
         return self::$instance;
40 40
     }
41 41
 
42
-    public function set($url ,$walking , string $protocol): Router
42
+    public function set($url, $walking, string $protocol): Router
43 43
     {
44 44
         $this->lastReturn = null;
45 45
         
46
-		$url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url;
46
+		$url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url;
47 47
 
48
-    	foreach($this->routers as $key => $value){
49
-    		if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
48
+    	foreach ($this->routers as $key => $value) {
49
+    		if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) {
50 50
                 throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured.");
51 51
             }
52 52
         }
@@ -71,9 +71,9 @@  discard block
 block discarded – undo
71 71
         return self::getInstance();
72 72
     }
73 73
 
74
-    public static function group(string $prefix,$callback): Router
74
+    public static function group(string $prefix, $callback): Router
75 75
     {
76
-        self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix;
76
+        self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix;
77 77
         self::getInstance()->group = sha1(date('d/m/Y h:m:i'));
78 78
         $callback();
79 79
         self::getInstance()->group = null;
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
 
85 85
     public static function where(): Router
86 86
     {
87
-        if(self::getInstance()->lastReturn){
87
+        if (self::getInstance()->lastReturn) {
88 88
             throw new Exception("It is not possible to define parameter tests for groups of routes.");
89 89
         }
90 90
 
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 
96 96
     public static function name(string $name): Router
97 97
     {
98
-        if(self::getInstance()->lastReturn){
98
+        if (self::getInstance()->lastReturn) {
99 99
             throw new Exception("There is no reason to call a {$name} route group.");
100 100
         }
101 101
 
@@ -103,11 +103,11 @@  discard block
 block discarded – undo
103 103
 
104 104
         self::getInstance()->hasRouteName($name);
105 105
 
106
-        if(in_array($name,self::getInstance()->beforeExcepts)){
106
+        if (in_array($name, self::getInstance()->beforeExcepts)) {
107 107
             $currentRoute['beforeAll'] = null;
108 108
         }
109 109
 
110
-        if(in_array($name,self::getInstance()->afterExcepts)){
110
+        if (in_array($name, self::getInstance()->afterExcepts)) {
111 111
             $currentRoute['afterAll'] = null;
112 112
         }
113 113
 
@@ -120,15 +120,15 @@  discard block
 block discarded – undo
120 120
 
121 121
     private function loadFromName(?string $routName = null): bool
122 122
     {
123
-        if(!is_null($routName)){
123
+        if (!is_null($routName)) {
124 124
             $currentProtocol = $this->getProtocol();
125 125
 
126 126
             $this->checkName($routName);
127 127
     
128 128
             $route = $this->routers[$routName];
129 129
     
130
-            if(!$this->checkProtocol($route['protocol'], $currentProtocol)){
131
-                throw new Exception('Page not found.',404);
130
+            if (!$this->checkProtocol($route['protocol'], $currentProtocol)) {
131
+                throw new Exception('Page not found.', 404);
132 132
             }
133 133
 
134 134
             $this->currentRoute = $route;
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
     {
145 145
         $instance = self::create()->getInstance();
146 146
 
147
-        if($instance->loadFromName($routeName)){
147
+        if ($instance->loadFromName($routeName)) {
148 148
             return $instance;
149 149
         }
150 150
 
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
     {
156 156
         $instance = self::create()->getInstance();
157 157
 
158
-        if(!$instance->loaded){
158
+        if (!$instance->loaded) {
159 159
             self::load($routeName);
160 160
         }
161 161
 
@@ -167,13 +167,13 @@  discard block
 block discarded – undo
167 167
 
168 168
     public function filter($filters): Router
169 169
     {
170
-        if($this->lastReturn !== null){
170
+        if ($this->lastReturn !== null) {
171 171
             $currentGroup = end($this->routers)['group'];
172 172
 
173 173
             foreach ($this->routers as $key => $value) {
174 174
 
175
-                if($value['group'] === $currentGroup){
176
-                    $currentRoute = $this->addFilter($this->routers[$key],$filters);
175
+                if ($value['group'] === $currentGroup) {
176
+                    $currentRoute = $this->addFilter($this->routers[$key], $filters);
177 177
                     $this->routers[$key] = $currentRoute;
178 178
                 }
179 179
 
@@ -182,24 +182,24 @@  discard block
 block discarded – undo
182 182
             return $this;
183 183
         }
184 184
         
185
-        $this->routers[count($this->routers)-1] = $this->addFilter(end($this->routers),$filters);
185
+        $this->routers[count($this->routers)-1] = $this->addFilter(end($this->routers), $filters);
186 186
         return $this;
187 187
     }
188 188
 
189 189
     public static function addFilter(array $route, $filter): array
190 190
     {
191
-        if(is_null($route['filters'])){
191
+        if (is_null($route['filters'])) {
192 192
             $route['filters'] = $filter;
193 193
             return $route;
194 194
         }
195 195
 
196 196
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
197 197
 
198
-        if(is_array($route['filters'])){
198
+        if (is_array($route['filters'])) {
199 199
             foreach ($route['filters'] as $key => $value) {
200 200
                 $filters[] = $value;
201 201
             }
202
-        }else{
202
+        }else {
203 203
             $filters[] = $route['filters'];
204 204
         }
205 205
 
Please login to merge, or discard this patch.