|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Router; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
trait Helper{ |
|
8
|
|
|
use CheckTrait, ControllerTrait; |
|
9
|
|
|
|
|
10
|
|
|
private $currentRoute = null; |
|
11
|
|
|
|
|
12
|
|
|
public static function current(): ?array |
|
13
|
|
|
{ |
|
14
|
|
|
return self::getInstance()->currentRoute; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public static function currentRouteName(): ?string |
|
18
|
|
|
{ |
|
19
|
|
|
return self::getInstance()->currentRoute['name']; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public static function currentRouteAction() |
|
23
|
|
|
{ |
|
24
|
|
|
return self::getInstance()->currentRoute['role']; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function getProtocol(): string |
|
28
|
|
|
{ |
|
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; |
|
31
|
|
|
|
|
32
|
|
|
return $protocol; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function getData(): ?array |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
'POST' => $_POST, |
|
39
|
|
|
'GET' => $_GET, |
|
40
|
|
|
'FILES' => $_FILES, |
|
41
|
|
|
'PROTOCOL' => $this->getProtocol() |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function ControllerForm($controller, string $method, array $values){ |
|
46
|
|
|
$this->checkRole(); |
|
47
|
|
|
$method = ($method !== 'method') ? $method : $this->getData()['POST']['role']; |
|
48
|
|
|
$data = (array_key_exists('data',$values)) ? json_decode($values['data'], true) : null; |
|
49
|
|
|
|
|
50
|
|
|
call_user_func_array([$controller,$method], $data); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function Controller(string $controll): void |
|
54
|
|
|
{ |
|
55
|
|
|
$data = $this->getData(); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($data['GET'] as $name => $value) { |
|
58
|
|
|
$controll = str_replace('{'.$name.'}',$value,$controll); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->checkControllsettable($controll); |
|
62
|
|
|
|
|
63
|
|
|
$this->checkControllexist($controll); |
|
64
|
|
|
|
|
65
|
|
|
$this->checkControllmethod($controll); |
|
66
|
|
|
|
|
67
|
|
|
$controller = ucfirst(explode(':',$controll)[0]); |
|
68
|
|
|
$controller = new $controller(); |
|
69
|
|
|
$method = explode(':',$controll)[1]; |
|
70
|
|
|
|
|
71
|
|
|
if( ($this->getProtocol() == 'form') ){ |
|
72
|
|
|
$this->ControllerForm($controller, $method, $data['POST']); |
|
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); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function explodeRoutes(bool $bar, string $url ,bool $bar_, string $url_): array |
|
81
|
|
|
{ |
|
82
|
|
|
$url = $bar ? substr($url, 0, -1) : $url ; |
|
83
|
|
|
$url = explode('/',$url); |
|
84
|
|
|
|
|
85
|
|
|
$url_ = $bar_ ? substr($url_, 0, -1) : $url_ ; |
|
86
|
|
|
$url_ = explode('/',$url_); |
|
87
|
|
|
|
|
88
|
|
|
foreach($url as $ur => $u){ |
|
89
|
|
|
if(substr($u,0,2) === '{?'){ |
|
90
|
|
|
if(!array_key_exists($ur,$url_)){ |
|
91
|
|
|
$url_[$ur] = ''; |
|
92
|
|
|
}; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return ['routeLoop' => $url, 'routeRequest' => $url_]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function toHiking(array $route) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->callOnRoute($route,'beforeAll'); |
|
102
|
|
|
$this->callOnRoute($route,'before'); |
|
103
|
|
|
|
|
104
|
|
|
if(is_string($route['role'])){ |
|
105
|
|
|
$this->Controller($route['role']); |
|
106
|
|
|
$this->callOnRoute($route,'after'); |
|
107
|
|
|
$this->callOnRoute($route,'afterAll'); |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
call_user_func_array($route['role'],$this->getData()['GET']); |
|
112
|
|
|
|
|
113
|
|
|
$this->callOnRoute($route,'after'); |
|
114
|
|
|
$this->callOnRoute($route,'afterAll'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function callOnRoute(array $route,string $state) |
|
118
|
|
|
{ |
|
119
|
|
|
if($route[$state] !== null){ |
|
120
|
|
|
if(is_string($route[$state])){ |
|
121
|
|
|
$this->Controller($route[$state]); |
|
122
|
|
|
}else{ |
|
123
|
|
|
$route[$state](); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function loadFromArrays() |
|
129
|
|
|
{ |
|
130
|
|
|
$currentProtocol = $this->getProtocol(); |
|
131
|
|
|
|
|
132
|
|
|
foreach(array_reverse($this->routers) as $r => $route){ |
|
133
|
|
|
|
|
134
|
|
|
$this->currentRoute = $route; |
|
135
|
|
|
$this->currentRoute['name'] = $r; |
|
136
|
|
|
|
|
137
|
|
|
if(!$this->checkProtocol($route['protocol'], $currentProtocol)){ |
|
138
|
|
|
continue; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->hasProtocol($route, $currentProtocol); |
|
142
|
|
|
|
|
143
|
|
|
$_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : ''; |
|
144
|
|
|
|
|
145
|
|
|
|
|
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'] |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
if(!$this->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])){ |
|
152
|
|
|
continue; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$this->loaded = true; |
|
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->currentRoute = null; |
|
160
|
|
|
throw new Exception('Page not found.',404); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|