|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Router; |
|
4
|
|
|
|
|
5
|
|
|
trait Helper{ |
|
6
|
|
|
use CheckTrait, ControllerTrait; |
|
7
|
|
|
|
|
8
|
|
|
private $currentRoute = null; |
|
9
|
|
|
|
|
10
|
|
|
public static function current(): ?array |
|
11
|
|
|
{ |
|
12
|
|
|
return self::getInstance()->currentRoute; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public static function currentRouteName(): ?string |
|
16
|
|
|
{ |
|
17
|
|
|
return self::getInstance()->currentRoute['name']; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public static function currentRouteAction() |
|
21
|
|
|
{ |
|
22
|
|
|
return self::getInstance()->currentRoute['role']; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected function getProtocol(): string |
|
26
|
|
|
{ |
|
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; |
|
29
|
|
|
|
|
30
|
|
|
return $protocol; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function getData(): ?array |
|
34
|
|
|
{ |
|
35
|
|
|
return [ |
|
36
|
|
|
'POST' => $_POST, |
|
37
|
|
|
'GET' => $_GET, |
|
38
|
|
|
'FILES' => $_FILES, |
|
39
|
|
|
'PROTOCOL' => $this->getProtocol() |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function import(string $path) |
|
44
|
|
|
{ |
|
45
|
|
|
foreach (scandir($path) as $routeFile) { |
|
46
|
|
|
if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){ |
|
47
|
|
|
require_once($path. DIRECTORY_SEPARATOR .$routeFile); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function ControllerForm($controller, string $method, array $values){ |
|
53
|
|
|
$this->checkRole(); |
|
54
|
|
|
$method = ($method !== 'method') ? $method : $this->getData()['POST']['role']; |
|
55
|
|
|
$data = (array_key_exists('data',$values)) ? json_decode($values['data'], true) : null; |
|
56
|
|
|
|
|
57
|
|
|
call_user_func_array([$controller,$method], $data); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function Controller(string $controll): void |
|
61
|
|
|
{ |
|
62
|
|
|
$data = $this->getData(); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($data['GET'] as $name => $value) { |
|
65
|
|
|
$controll = str_replace('{'.$name.'}',$value,$controll); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->checkControllsettable($controll); |
|
69
|
|
|
|
|
70
|
|
|
$this->checkControllexist($controll); |
|
71
|
|
|
|
|
72
|
|
|
$this->checkControllmethod($controll); |
|
73
|
|
|
|
|
74
|
|
|
$controller = ROUTER_CONFIG['controller.namespace'].'\\'. ucfirst(explode(':',$controll)[0]); |
|
75
|
|
|
$controller = new $controller(); |
|
76
|
|
|
$method = explode(':',$controll)[1]; |
|
77
|
|
|
|
|
78
|
|
|
if( ($this->getProtocol() == 'form') ){ |
|
79
|
|
|
$this->ControllerForm($controller, $method, $data['POST']); |
|
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); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function explodeRoutes(bool $bar, string $url ,bool $bar_, string $url_): array |
|
88
|
|
|
{ |
|
89
|
|
|
$url = $bar ? substr($url, 0, -1) : $url ; |
|
90
|
|
|
$url = explode('/',$url); |
|
91
|
|
|
|
|
92
|
|
|
$url_ = $bar_ ? substr($url_, 0, -1) : $url_ ; |
|
93
|
|
|
$url_ = explode('/',$url_); |
|
94
|
|
|
|
|
95
|
|
|
foreach($url as $ur => $u){ |
|
96
|
|
|
if(substr($u,0,2) === '{?'){ |
|
97
|
|
|
if(!array_key_exists($ur,$url_)){ |
|
98
|
|
|
$url_[$ur] = ''; |
|
99
|
|
|
}; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return ['routeLoop' => $url, 'routeRequest' => $url_]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function toHiking($walking) |
|
107
|
|
|
{ |
|
108
|
|
|
if(is_string($walking)){ |
|
109
|
|
|
$this->Controller($walking); |
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
call_user_func_array($walking,$this->getData()['GET']); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|