@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Router; |
4 | 4 | |
5 | -trait Helper{ |
|
5 | +trait Helper { |
|
6 | 6 | use CheckTrait, ControllerTrait; |
7 | 7 | |
8 | 8 | private $currentRoute = null; |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | protected function getProtocol(): string |
26 | 26 | { |
27 | 27 | $protocol = ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? 'ajax' : 'get'; |
28 | - $protocol = (array_key_existS('HTTP_REQUESTED_METHOD',$_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol; |
|
28 | + $protocol = (array_key_existS('HTTP_REQUESTED_METHOD', $_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol; |
|
29 | 29 | |
30 | 30 | return $protocol; |
31 | 31 | } |
@@ -43,18 +43,18 @@ discard block |
||
43 | 43 | protected function import(string $path) |
44 | 44 | { |
45 | 45 | foreach (scandir($path) as $routeFile) { |
46 | - if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){ |
|
47 | - require_once($path. DIRECTORY_SEPARATOR .$routeFile); |
|
46 | + if (pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php') { |
|
47 | + require_once($path.DIRECTORY_SEPARATOR.$routeFile); |
|
48 | 48 | } |
49 | 49 | } |
50 | 50 | } |
51 | 51 | |
52 | - protected function ControllerForm($controller, string $method, array $values){ |
|
52 | + protected function ControllerForm($controller, string $method, array $values) { |
|
53 | 53 | $this->checkRole(); |
54 | 54 | $method = ($method !== 'method') ? $method : $this->getData()['POST']['role']; |
55 | - $data = (array_key_exists('data',$values)) ? json_decode($values['data'], true) : null; |
|
55 | + $data = (array_key_exists('data', $values)) ? json_decode($values['data'], true) : null; |
|
56 | 56 | |
57 | - call_user_func_array([$controller,$method], $data); |
|
57 | + call_user_func_array([$controller, $method], $data); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | protected function Controller(string $controll): void |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | $data = $this->getData(); |
63 | 63 | |
64 | 64 | foreach ($data['GET'] as $name => $value) { |
65 | - $controll = str_replace('{'.$name.'}',$value,$controll); |
|
65 | + $controll = str_replace('{'.$name.'}', $value, $controll); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | $this->checkControllsettable($controll); |
@@ -71,30 +71,30 @@ discard block |
||
71 | 71 | |
72 | 72 | $this->checkControllmethod($controll); |
73 | 73 | |
74 | - $controller = ROUTER_CONFIG['controller.namespace'].'\\'. ucfirst(explode(':',$controll)[0]); |
|
74 | + $controller = ROUTER_CONFIG['controller.namespace'].'\\'.ucfirst(explode(':', $controll)[0]); |
|
75 | 75 | $controller = new $controller(); |
76 | - $method = explode(':',$controll)[1]; |
|
76 | + $method = explode(':', $controll)[1]; |
|
77 | 77 | |
78 | - if( ($this->getProtocol() == 'form') ){ |
|
78 | + if (($this->getProtocol() == 'form')) { |
|
79 | 79 | $this->ControllerForm($controller, $method, $data['POST']); |
80 | 80 | }else { |
81 | - $data = (array_key_exists('data',$data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET']; |
|
82 | - call_user_func_array([$controller,$method], $data); |
|
81 | + $data = (array_key_exists('data', $data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET']; |
|
82 | + call_user_func_array([$controller, $method], $data); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | } |
86 | 86 | |
87 | - protected function explodeRoutes(bool $bar, string $url ,bool $bar_, string $url_): array |
|
87 | + protected function explodeRoutes(bool $bar, string $url, bool $bar_, string $url_): array |
|
88 | 88 | { |
89 | - $url = $bar ? substr($url, 0, -1) : $url ; |
|
90 | - $url = explode('/',$url); |
|
89 | + $url = $bar ? substr($url, 0, -1) : $url; |
|
90 | + $url = explode('/', $url); |
|
91 | 91 | |
92 | - $url_ = $bar_ ? substr($url_, 0, -1) : $url_ ; |
|
93 | - $url_ = explode('/',$url_); |
|
92 | + $url_ = $bar_ ? substr($url_, 0, -1) : $url_; |
|
93 | + $url_ = explode('/', $url_); |
|
94 | 94 | |
95 | - foreach($url as $ur => $u){ |
|
96 | - if(substr($u,0,2) === '{?'){ |
|
97 | - if(!array_key_exists($ur,$url_)){ |
|
95 | + foreach ($url as $ur => $u) { |
|
96 | + if (substr($u, 0, 2) === '{?') { |
|
97 | + if (!array_key_exists($ur, $url_)) { |
|
98 | 98 | $url_[$ur] = ''; |
99 | 99 | }; |
100 | 100 | } |
@@ -105,28 +105,28 @@ discard block |
||
105 | 105 | |
106 | 106 | protected function toHiking(array $route) |
107 | 107 | { |
108 | - $this->callOnRoute($route,'beforeAll'); |
|
109 | - $this->callOnRoute($route,'before'); |
|
108 | + $this->callOnRoute($route, 'beforeAll'); |
|
109 | + $this->callOnRoute($route, 'before'); |
|
110 | 110 | |
111 | - if(is_string($route['role'])){ |
|
111 | + if (is_string($route['role'])) { |
|
112 | 112 | $this->Controller($route['role']); |
113 | - $this->callOnRoute($route,'after'); |
|
114 | - $this->callOnRoute($route,'afterAll'); |
|
113 | + $this->callOnRoute($route, 'after'); |
|
114 | + $this->callOnRoute($route, 'afterAll'); |
|
115 | 115 | return true; |
116 | 116 | } |
117 | 117 | |
118 | - call_user_func_array($route['role'],$this->getData()['GET']); |
|
118 | + call_user_func_array($route['role'], $this->getData()['GET']); |
|
119 | 119 | |
120 | - $this->callOnRoute($route,'after'); |
|
121 | - $this->callOnRoute($route,'afterAll'); |
|
120 | + $this->callOnRoute($route, 'after'); |
|
121 | + $this->callOnRoute($route, 'afterAll'); |
|
122 | 122 | } |
123 | 123 | |
124 | - protected function callOnRoute(array $route,string $state) |
|
124 | + protected function callOnRoute(array $route, string $state) |
|
125 | 125 | { |
126 | - if($route[$state] !== null){ |
|
127 | - if(is_string($route[$state])){ |
|
126 | + if ($route[$state] !== null) { |
|
127 | + if (is_string($route[$state])) { |
|
128 | 128 | $this->Controller($route[$state]); |
129 | - }else{ |
|
129 | + }else { |
|
130 | 130 | $route[$state](); |
131 | 131 | } |
132 | 132 | } |
@@ -2,11 +2,11 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Router; |
4 | 4 | |
5 | -trait ExtraJobsTrait{ |
|
5 | +trait ExtraJobsTrait { |
|
6 | 6 | |
7 | 7 | public function before($walking): Router |
8 | 8 | { |
9 | - return $this->setOnRoute($walking,'before'); |
|
9 | + return $this->setOnRoute($walking, 'before'); |
|
10 | 10 | } |
11 | 11 | |
12 | 12 | public static function beforeAll($walking, $except = null): Router |
@@ -14,12 +14,12 @@ discard block |
||
14 | 14 | $excepts = is_array($except) ? $except : [$except]; |
15 | 15 | self::getInstance()->beforeExcepts = $excepts; |
16 | 16 | self::getInstance()->beforeAll = $walking; |
17 | - return self::getInstance()->setOnRoutes($walking,'beforeAll',$excepts); |
|
17 | + return self::getInstance()->setOnRoutes($walking, 'beforeAll', $excepts); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | public function after($walking): Router |
21 | 21 | { |
22 | - return $this->setOnRoute($walking,'after'); |
|
22 | + return $this->setOnRoute($walking, 'after'); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | public static function afterAll($walking, $except = null): Router |
@@ -27,17 +27,17 @@ discard block |
||
27 | 27 | $excepts = is_array($except) ? $except : [$except]; |
28 | 28 | self::getInstance()->afterExcepts = $excepts; |
29 | 29 | self::getInstance()->afterAll = $walking; |
30 | - return self::getInstance()->setOnRoutes($walking,'afterAll',$excepts); |
|
30 | + return self::getInstance()->setOnRoutes($walking, 'afterAll', $excepts); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | private function setOnRoute($walking, string $state): Router |
34 | 34 | { |
35 | - if($this->lastReturn !== null){ |
|
35 | + if ($this->lastReturn !== null) { |
|
36 | 36 | $currentGroup = end($this->routers)['group']; |
37 | 37 | |
38 | 38 | foreach ($this->routers as $key => $value) { |
39 | 39 | |
40 | - if($value['group'] === $currentGroup){ |
|
40 | + if ($value['group'] === $currentGroup) { |
|
41 | 41 | $this->routers[$key][$state] = $walking; |
42 | 42 | } |
43 | 43 | |
@@ -51,8 +51,8 @@ discard block |
||
51 | 51 | |
52 | 52 | private function setOnRoutes($walking, string $state, array $excepts): Router |
53 | 53 | { |
54 | - foreach($this->routers as $r => $route){ |
|
55 | - if(!in_array($this->routers,$excepts)){ |
|
54 | + foreach ($this->routers as $r => $route) { |
|
55 | + if (!in_array($this->routers, $excepts)) { |
|
56 | 56 | $this->routers[$r][$state] = $walking; |
57 | 57 | } |
58 | 58 | } |
@@ -4,18 +4,18 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -trait CheckWhere{ |
|
7 | +trait CheckWhere { |
|
8 | 8 | |
9 | 9 | protected function checkWhereParam($data) |
10 | 10 | { |
11 | - if(count($data) === 0){ |
|
11 | + if (count($data) === 0) { |
|
12 | 12 | throw new Exception('It is necessary to define a condition to be tested.'); |
13 | 13 | } |
14 | 14 | } |
15 | 15 | |
16 | 16 | protected function checkWhereParams($params) |
17 | 17 | { |
18 | - if(count($params) === 0){ |
|
18 | + if (count($params) === 0) { |
|
19 | 19 | throw new Exception('The route in question has no parameters to be tested.'); |
20 | 20 | } |
21 | 21 | } |
@@ -24,24 +24,24 @@ discard block |
||
24 | 24 | { |
25 | 25 | $pass = true; |
26 | 26 | |
27 | - if(!is_array($route['where'])){ |
|
27 | + if (!is_array($route['where'])) { |
|
28 | 28 | return $pass; |
29 | 29 | } |
30 | 30 | |
31 | - $routeURI = explode('/',$route['url']); |
|
31 | + $routeURI = explode('/', $route['url']); |
|
32 | 32 | $params = []; |
33 | - foreach($routeURI as $p => $part){ |
|
34 | - if(!$this->isWhered($part,$request[$p])){ |
|
33 | + foreach ($routeURI as $p => $part) { |
|
34 | + if (!$this->isWhered($part, $request[$p])) { |
|
35 | 35 | continue; |
36 | 36 | } |
37 | 37 | |
38 | - $param = substr(str_replace('?','',$part),1,-1); |
|
38 | + $param = substr(str_replace('?', '', $part), 1, -1); |
|
39 | 39 | |
40 | - if(array_key_exists($param,$route['where'])){ |
|
40 | + if (array_key_exists($param, $route['where'])) { |
|
41 | 41 | |
42 | 42 | $params[$param] = $route['where'][$param]; |
43 | 43 | |
44 | - if(!preg_match("/^{$params[$param]}$/",$request[$p])){ |
|
44 | + if (!preg_match("/^{$params[$param]}$/", $request[$p])) { |
|
45 | 45 | $pass = false; |
46 | 46 | } |
47 | 47 | } |
@@ -52,17 +52,17 @@ discard block |
||
52 | 52 | |
53 | 53 | private function isWhered(string $part, string $value): bool |
54 | 54 | { |
55 | - return $this->isParameter($part) && !$this->checkParameterOptional($part,$value); |
|
55 | + return $this->isParameter($part) && !$this->checkParameterOptional($part, $value); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | private function checkParameterOptional(string $part, string $value): bool |
59 | 59 | { |
60 | - return substr($part,0,2) === '{?' && empty($value); |
|
60 | + return substr($part, 0, 2) === '{?' && empty($value); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | protected function isParameter(string $part): bool |
64 | 64 | { |
65 | - return (substr($part,0,1) === '{' && substr($part,-1) === '}'); |
|
65 | + return (substr($part, 0, 1) === '{' && substr($part, -1) === '}'); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | protected function callWhereAdd($data) |
@@ -72,16 +72,16 @@ discard block |
||
72 | 72 | $data = (count($data) > 1) ? [$data[0] => $data[1]] : $data[0]; |
73 | 73 | |
74 | 74 | $route = end($this->routers); |
75 | - $routeURI = explode('/',$route['url']); |
|
75 | + $routeURI = explode('/', $route['url']); |
|
76 | 76 | $params = []; |
77 | - foreach($routeURI as $part){ |
|
78 | - if(!$this->isParameter($part)){ |
|
77 | + foreach ($routeURI as $part) { |
|
78 | + if (!$this->isParameter($part)) { |
|
79 | 79 | continue; |
80 | 80 | } |
81 | 81 | |
82 | - $param = substr(str_replace('?','',$part),1,-1); |
|
82 | + $param = substr(str_replace('?', '', $part), 1, -1); |
|
83 | 83 | |
84 | - if(array_key_exists($param,$data)){ |
|
84 | + if (array_key_exists($param, $data)) { |
|
85 | 85 | $params[$param] = $data[$param]; |
86 | 86 | } |
87 | 87 | |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | |
90 | 90 | $this->checkWhereParams($params); |
91 | 91 | |
92 | - $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$params) : $params; |
|
92 | + $route['where'] = (is_array($route['where'])) ? array_merge($route['where'], $params) : $params; |
|
93 | 93 | |
94 | 94 | $this->routers[count($this->routers)-1] = $route; |
95 | 95 | } |