|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Mvc; |
|
4
|
|
|
|
|
5
|
|
|
use Win\DesignPattern\Singleton; |
|
6
|
|
|
use Win\Helper\Url; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Rota de URL |
|
10
|
|
|
* |
|
11
|
|
|
* Redireciona a requisição para um outro Controller. |
|
12
|
|
|
* Veja as rotas em "config/routes.php" |
|
13
|
|
|
*/ |
|
14
|
|
|
class Router { |
|
15
|
|
|
|
|
16
|
|
|
use Singleton; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string[] */ |
|
19
|
|
|
private $routes = []; |
|
20
|
|
|
public static $file = '/app/config/routes.php'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Se for atribuído algum valor indica que é uma rota personalizada |
|
24
|
|
|
* @var mixed[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $customUrl = [null, null]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Retorna a nova URL |
|
30
|
|
|
* @return mixed[] |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getCustomUrl() { |
|
33
|
|
|
return static::$customUrl; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Retorna TRUE se a URL foi personalizada |
|
38
|
|
|
* @return boolean |
|
39
|
|
|
*/ |
|
40
|
|
|
public function hasCustomUrl() { |
|
41
|
|
|
return (!is_null(static::$customUrl[0])); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Inicia o processo de URL personalizada |
|
46
|
|
|
* retornando TRUE se alguma rota foi encontrada |
|
47
|
|
|
* @return boolean |
|
48
|
|
|
*/ |
|
49
|
|
|
public function run() { |
|
50
|
|
|
$this->load(); |
|
51
|
|
|
if (count($this->routes)) { |
|
52
|
|
|
static::$customUrl = static::createCustomUrl(); |
|
|
|
|
|
|
53
|
|
|
return $this->hasCustomUrl(); |
|
54
|
|
|
} |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** Carrega o arquivo que contem as rotas */ |
|
59
|
|
|
private function load() { |
|
60
|
|
|
if (file_exists(BASE_PATH . static::$file)) { |
|
61
|
|
|
$this->routes = include BASE_PATH . static::$file; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Percorre todas as rotas e retorna a nova URL |
|
67
|
|
|
* @return string[] Nova URL [0 => controller, 1 => action] |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function createCustomUrl() { |
|
70
|
|
|
$search = ['', '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9', '$10']; |
|
71
|
|
|
$matches = []; |
|
72
|
|
|
foreach ($this->routes as $url => $route): |
|
73
|
|
|
$exists = preg_match('@' . Url::instance()->format($url) . '$@', Url::instance()->getUrl(), $matches) == 1; |
|
74
|
|
|
if ($exists): |
|
75
|
|
|
$route = str_replace($search, $matches, $route) . '/'; |
|
76
|
|
|
return explode('/', $route); |
|
77
|
|
|
endif; |
|
78
|
|
|
endforeach; |
|
79
|
|
|
return [null, null]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Cria um Controller de acordo com a nova URL |
|
84
|
|
|
* @return Controller|DefaultController |
|
85
|
|
|
*/ |
|
86
|
|
|
public function createController() { |
|
87
|
|
|
return ControllerFactory::create(static::$customUrl[0], static::$customUrl[1]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|