1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Services; |
4
|
|
|
|
5
|
|
|
use Win\Common\InjectableTrait; |
6
|
|
|
use Win\Common\Utils\Input; |
7
|
|
|
use Win\HttpException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Rota de URL |
11
|
|
|
* |
12
|
|
|
* Define o [Controller, action] a ser executado baseado na URL |
13
|
|
|
* @see "config/routes.php" |
14
|
|
|
*/ |
15
|
|
|
class Router |
16
|
|
|
{ |
17
|
|
|
use InjectableTrait; |
18
|
|
|
const HOME = ['index', 'index']; |
19
|
|
|
const SUFFIX = '/'; |
20
|
|
|
|
21
|
|
|
/** @var string HTTPS/HTTP */ |
22
|
|
|
public $protocol; |
23
|
|
|
|
24
|
|
|
/** @var string URL Base */ |
25
|
|
|
public $baseUrl; |
26
|
|
|
|
27
|
|
|
/** @var string URL Relativa */ |
28
|
|
|
public $relativeUrl; |
29
|
|
|
|
30
|
|
|
/** @var string URL Completa/Absoluta */ |
31
|
|
|
public $url; |
32
|
|
|
|
33
|
|
|
/** @var string[] fragmentos da URL */ |
34
|
|
|
public $segments; |
35
|
|
|
|
36
|
|
|
/** @var string Nome do Controller */ |
37
|
|
|
public $page; |
38
|
|
|
|
39
|
|
|
/** @var string Nome do action */ |
40
|
|
|
public $action; |
41
|
|
|
|
42
|
|
|
/** @var string[][] url => [Controller, action] */ |
43
|
|
|
public $routes = []; |
44
|
|
|
|
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
$this->protocol = Input::protocol(); |
48
|
|
|
$this->baseUrl = $this->getBaseUrl(); |
49
|
|
|
$this->relativeUrl = $this->getRelativeUrl(); |
50
|
|
|
$this->segments = $this->getSegments(); |
51
|
|
|
$this->url = $this->baseUrl . $this->relativeUrl; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Percorre todas as rotas e retorna o destino final |
56
|
|
|
* @return array Destino |
57
|
|
|
* @example return [Controller, action, ...$args] |
58
|
|
|
*/ |
59
|
|
|
public function getDestination() |
60
|
|
|
{ |
61
|
|
|
$url = $this->format($this->relativeUrl); |
62
|
|
|
$args = []; |
63
|
|
|
|
64
|
|
|
foreach ($this->routes as $request => $destination) { |
65
|
|
|
$pattern = '@^' . $this->format($request) . '$@'; |
66
|
|
|
$match = preg_match($pattern, $url, $args); |
67
|
|
|
if ($match) { |
68
|
|
|
return [...$destination, ...array_splice($args, 1)]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
throw new HttpException('Route não encontrada, verifique: "config/routes.php"', 404); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Retorna TRUE se está na página inicial |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function isHomePage() |
80
|
|
|
{ |
81
|
|
|
return $this->segments == static::HOME; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retorna no formato de URL |
86
|
|
|
* @param string $url |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function format($url) |
90
|
|
|
{ |
91
|
|
|
return rtrim($url, static::SUFFIX) . static::SUFFIX; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Redireciona para a URL escolhida |
96
|
|
|
* @param string $url URL relativa ou absoluta |
97
|
|
|
* @codeCoverageIgnore |
98
|
|
|
*/ |
99
|
|
|
public function redirect($url = '') |
100
|
|
|
{ |
101
|
|
|
if (false === strpos($url, '://')) { |
102
|
|
|
$url = $this->baseUrl . $url; |
103
|
|
|
} |
104
|
|
|
header('location:' . $url); |
105
|
|
|
die(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Volta para o método index da pagina atual |
110
|
|
|
* @codeCoverageIgnore |
111
|
|
|
*/ |
112
|
|
|
public function redirectToIndex() |
113
|
|
|
{ |
114
|
|
|
$this->redirect($this->segments[0]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Atualiza a mesma página |
119
|
|
|
* @param string $url |
120
|
|
|
* @codeCoverageIgnore |
121
|
|
|
*/ |
122
|
|
|
public function refresh() |
123
|
|
|
{ |
124
|
|
|
$this->redirect($this->url); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Retorna a URL base |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
private function getBaseUrl() |
132
|
|
|
{ |
133
|
|
|
$host = Input::server('HTTP_HOST'); |
134
|
|
|
if ($host) { |
135
|
|
|
$script = Input::server('SCRIPT_NAME'); |
136
|
|
|
$baseUrl = preg_replace('@/+$@', '', dirname($script)); |
137
|
|
|
return $this->protocol . '://' . $host . $baseUrl . '/'; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Define o final da URL |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
private function getRelativeUrl() |
146
|
|
|
{ |
147
|
|
|
$host = Input::server('HTTP_HOST'); |
148
|
|
|
if ($host) { |
149
|
|
|
$requestUri = explode('?', Input::server('REQUEST_URI')); |
150
|
|
|
$context = explode($host, $this->baseUrl); |
151
|
|
|
$uri = (explode(end($context), $requestUri[0], 2)); |
152
|
|
|
return end($uri); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Define os fragmentos da URL |
158
|
|
|
* @return string[] |
159
|
|
|
*/ |
160
|
|
|
private function getSegments() |
161
|
|
|
{ |
162
|
|
|
return array_filter(explode('/', $this->relativeUrl)) + static::HOME; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|