forked from dyorg/Lean
/
Launch.php
144 lines (104 loc) · 2.65 KB
/
Launch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Lean;
class Launch
{
private $action_method_to_launch;
private $path_class_to_launch;
static public function instance()
{
return new self;
}
public function run()
{
$this->process();
}
private function dispache($module, $controller, $method)
{
if(empty($module)) {
$module = Route::get_route_module_default();
}
if(empty($controller)) {
$controller = Route::get_route_controller_default();
}
if(empty($method)) {
$method = 'index';
}
$class = Config::get_application_path() . "\\" . Config::get_module_prefixe() . $module . "\\controllers\\" . $controller . "Controller";
eval($class.'::singleton()->'.$method.'();');
}
private function get_info()
{
/*
* verifica url com index.php
*/
preg_match('/(.*)\/index.php(.*)/', $_SERVER["PHP_SELF"], $matches);
$PATH_ROOT = $matches[1];
$PATH_INFO = $matches[2];
/*
* verifica url sem index.php
*/
if(!preg_match('/index.php/', $_SERVER["REQUEST_URI"]))
{
$PATH_INFO = str_replace($PATH_ROOT, '', $_SERVER["REQUEST_URI"]);
if(preg_match('/(.*)[?]/', $PATH_INFO, $matches))
{
$PATH_INFO = $matches[1];
}
}
/*
* define url root
*/
define(strtoupper(Config::get_root_path()), $PATH_ROOT.'/');
$PATH_INFO = trim($PATH_INFO, '/');
return $PATH_INFO;
}
private function process()
{
$info = self::get_info();
/*
* rotas
*/
if($routes = Route::get_routes())
{
if(array_key_exists($info, $routes))
{
$route = $routes[$info];
if(is_object($route))
{
$route();
}
elseif(is_array($route))
{
$module = isset($route['module']) ? $route['module'] : NULL;
$controller = isset($route['controller']) ? $route['controller'] : NULL;
$method = isset($route['method']) ? $route['method'] : NULL;
$this->dispache($module, $controller, $method);
}
return true;
}
}
/*
* rota padrão caso info seja vazio
*/
if(empty($info))
{
$route_default = Route::get_route_default();
if(!empty($route_default)) $info = $route_default;
}
/*
* explode info
*/
if(isset($info))
{
$url_string = explode('/', strtolower(trim($info, '/')));
if($url_string[0] == '') $url_string = null;
}
/*
* valida e dispara método
*/
$module = isset($url_string[0]) ? $url_string[0] : null;
$controller = isset($url_string[1]) ? (str_replace(' ', '', ucwords(str_replace('-', ' ', $url_string[1])))) : null;
$method = isset($url_string[2]) ? str_replace('-', '_', $url_string[2]) : null;
$this->dispache($module, $controller, $method);
}
}