1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DevPontes\Route; |
4
|
|
|
|
5
|
|
|
use Throwable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Description of Route |
9
|
|
|
* |
10
|
|
|
* @author Moises Pontes |
11
|
|
|
* @package DevPontes\Route |
12
|
|
|
*/ |
13
|
|
|
class Route |
14
|
|
|
{ |
15
|
|
|
/** @var Throwable */ |
16
|
|
|
private $fail; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $param; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
private $routes; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $method; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $controller; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $namespace; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
private $url; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Route constructor. |
38
|
|
|
* |
39
|
|
|
* @param array $routes |
40
|
|
|
* @param array $error |
41
|
|
|
*/ |
42
|
|
|
public function __construct(array $routes) |
43
|
|
|
{ |
44
|
|
|
$url = filter_input(INPUT_GET, 'url', FILTER_SANITIZE_SPECIAL_CHARS); |
45
|
|
|
|
46
|
|
|
$this->setUrl($url); |
47
|
|
|
$this->setRoutes($routes); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return Throwable|null |
52
|
|
|
*/ |
53
|
|
|
public function fail(): ?Throwable |
54
|
|
|
{ |
55
|
|
|
return $this->fail; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set namespace app |
60
|
|
|
* |
61
|
|
|
* @param string $namespace |
62
|
|
|
* @return Route |
63
|
|
|
*/ |
64
|
|
|
public function namespace(string $namespace): Route |
65
|
|
|
{ |
66
|
|
|
$this->namespace = $namespace; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Separa a url em partes |
72
|
|
|
* |
73
|
|
|
* @param string|null $url |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
private function setUrl(?string $url): void |
77
|
|
|
{ |
78
|
|
|
$url = "/" . $url; |
79
|
|
|
$this->url = explode('/', $url); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Configura as rotas |
84
|
|
|
* |
85
|
|
|
* @param array $routes |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
private function setRoutes(array $routes): void |
89
|
|
|
{ |
90
|
|
|
$this->routes = []; |
91
|
|
|
|
92
|
|
|
foreach ($routes as $route) { |
93
|
|
|
$dispatch = explode('@', $route[1]); |
94
|
|
|
|
95
|
|
|
$set = [ |
96
|
|
|
"url" => $route[0], |
97
|
|
|
"controller" => $dispatch[0], |
98
|
|
|
"method" => $dispatch[1], |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
array_push($this->routes, $set); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $routeArr |
107
|
|
|
* @param string $route |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
private function setParam(array $routeArr, string &$route): void |
111
|
|
|
{ |
112
|
|
|
foreach ($routeArr as $k => $v) { |
113
|
|
|
if (preg_match('/^\{.*\}$/', $v) && (count($this->url) == count($routeArr))) { |
114
|
|
|
$routeArr[$k] = $this->url[$k]; |
115
|
|
|
$this->param = $this->url[$k]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$route = implode('/', $routeArr); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Execulta a controller |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function run(): void |
128
|
|
|
{ |
129
|
|
|
$url = implode('/', $this->url); |
130
|
|
|
|
131
|
|
|
foreach ($this->routes as $route) { |
132
|
|
|
$routeArray = explode('/', $route['url']); |
133
|
|
|
$this->setParam($routeArray, $route['url']); |
134
|
|
|
|
135
|
|
|
if ($route['url'] == $url) { |
136
|
|
|
$this->method = $route['method']; |
137
|
|
|
$this->controller = $route['controller']; |
138
|
|
|
break; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
try { |
143
|
|
|
$controller = $this->namespace . "\\" . $this->controller; |
144
|
|
|
call_user_func([new $controller(), $this->method], $this->param); |
145
|
|
|
} catch (Throwable $th) { |
146
|
|
|
$this->fail = $th; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|