1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* KumbiaPHP web & app Framework |
4
|
|
|
* |
5
|
|
|
* LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the new BSD license that is bundled |
8
|
|
|
* with this package in the file LICENSE. |
9
|
|
|
* |
10
|
|
|
* @category Kumbia |
11
|
|
|
* @package Router |
12
|
|
|
* |
13
|
|
|
* @copyright Copyright (c) 2005 - 2020 KumbiaPHP Team (http://www.kumbiaphp.com) |
14
|
|
|
* @license https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE New BSD License |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Clase que Actua como router del Front-Controller |
19
|
|
|
* |
20
|
|
|
* Manejo de redirecciones de peticiones |
21
|
|
|
* Contiene información referente a la url de |
22
|
|
|
* la petición ( modudo, controlador, acción, parametros, etc ) |
23
|
|
|
* |
24
|
|
|
* @category Kumbia |
25
|
|
|
* @package Router |
26
|
|
|
*/ |
27
|
|
|
class Router |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Array estático con las variables del router |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected static $vars = [ |
36
|
|
|
// 'method' => '', //Método usado GET, POST, ... |
37
|
|
|
// 'route' => '', //Ruta pasada URL |
38
|
|
|
// 'module' => '', //Nombre del módulo actual |
39
|
|
|
// 'controller' => 'index', //Nombre del controlador actual |
40
|
|
|
// 'action' => 'index', //Nombre de la acción actual, por defecto index |
41
|
|
|
// 'parameters' => [], //Lista los parámetros adicionales de la URL |
42
|
|
|
// 'controller_path' => 'index' |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Array estático con las variables del router por defecto |
47
|
|
|
* TODO: Convertir a constante |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected static $default = [ |
52
|
|
|
'module' => '', //Nombre del módulo actual |
53
|
|
|
'controller' => 'index', //Nombre del controlador actual, por defecto index |
54
|
|
|
'action' => 'index', //Nombre de la acción actual, por defecto index |
55
|
|
|
'parameters' => [], //Lista los parámetros adicionales de la URL |
56
|
|
|
'controller_path' => 'index' |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* This is the name of router class |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected static $router = 'KumbiaRouter'; |
64
|
|
|
//Es el router por defecto |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Indica si esta pendiente la ejecución de una ruta por parte del dispatcher |
68
|
|
|
* |
69
|
|
|
* @var boolean |
70
|
|
|
*/ |
71
|
|
|
protected static $routed = false; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Procesamiento basico del router |
75
|
|
|
* @param string $url |
76
|
|
|
* |
77
|
|
|
* @throws KumbiaException |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public static function init($url) |
81
|
|
|
{ |
82
|
|
|
// Se miran los parámetros por seguridad |
83
|
|
|
if (stripos($url, '/../') !== false) { |
84
|
|
|
throw new KumbiaException("Posible intento de hack en URL: '$url'"); |
85
|
|
|
} |
86
|
|
|
// Si hay intento de hack TODO: añadir la ip y referer en el log |
87
|
|
|
self::$default['route'] = $url; |
88
|
|
|
//Método usado |
89
|
|
|
self::$default['method'] = $_SERVER['REQUEST_METHOD']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Ejecuta una url |
94
|
|
|
* |
95
|
|
|
* @param string $url |
96
|
|
|
* |
97
|
|
|
* @throws KumbiaException |
98
|
|
|
* @return Controller |
99
|
|
|
*/ |
100
|
|
|
public static function execute($url) |
101
|
|
|
{ |
102
|
|
|
self::init($url); |
103
|
|
|
//alias |
104
|
|
|
$router = self::$router; |
105
|
|
|
$conf = Config::get('config.application.routes'); |
106
|
|
|
//Si config.ini tiene routes activados, mira si esta routed |
107
|
|
|
if ($conf) { |
108
|
|
|
/*Esta activado el router*/ |
109
|
|
|
/* This if for back compatibility*/ |
110
|
|
|
if ($conf === '1') { |
111
|
|
|
$url = $router::ifRouted($url); |
112
|
|
|
} else { |
113
|
|
|
/*Es otra clase de router*/ |
114
|
|
|
$router = self::$router = $conf; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Descompone la url |
119
|
|
|
self::$vars = $router::rewrite($url) + self::$default; |
120
|
|
|
|
121
|
|
|
// Despacha la ruta actual |
122
|
|
|
return self::dispatch($router::getController(self::$vars)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Realiza el dispatch de la ruta actual |
127
|
|
|
* |
128
|
|
|
* @param Controller $cont Controlador a usar |
129
|
|
|
* |
130
|
|
|
* @throws KumbiaException |
131
|
|
|
* @return Controller |
132
|
|
|
*/ |
133
|
|
|
private static function dispatch($cont) |
134
|
|
|
{ |
135
|
|
|
// Se ejecutan los filtros initialize y before |
136
|
|
|
if ($cont->k_callback(true) === false) { |
137
|
|
|
return $cont; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (method_exists($cont, $cont->action_name)) { |
141
|
|
|
if (strcasecmp($cont->action_name, 'k_callback') === 0 ) { |
142
|
|
|
throw new KumbiaException('Esta intentando ejecutar un método reservado de KumbiaPHP'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($cont->limit_params) { // with variadic php5.6 delete it |
146
|
|
|
$reflectionMethod = new ReflectionMethod($cont, $cont->action_name); |
147
|
|
|
$num_params = count($cont->parameters); |
148
|
|
|
|
149
|
|
|
if ($num_params < $reflectionMethod->getNumberOfRequiredParameters() || |
150
|
|
|
$num_params > $reflectionMethod->getNumberOfParameters()) { |
151
|
|
|
|
152
|
|
|
throw new KumbiaException('', 'num_params'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
call_user_func_array([$cont, $cont->action_name], $cont->parameters); |
159
|
|
|
|
160
|
|
|
//Corre los filtros after y finalize |
161
|
|
|
$cont->k_callback(); |
162
|
|
|
|
163
|
|
|
//Si esta routed internamente volver a ejecutar |
164
|
|
|
self::isRouted(); |
165
|
|
|
|
166
|
|
|
return $cont; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Redirecciona la ejecución internamente |
171
|
|
|
* |
172
|
|
|
* @throws KumbiaException |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
protected static function isRouted() |
176
|
|
|
{ |
177
|
|
|
if (self::$routed) { |
178
|
|
|
self::$routed = false; |
179
|
|
|
$router = self::$router; |
180
|
|
|
// Despacha la ruta actual |
181
|
|
|
self::dispatch($router::getController(self::$vars)); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Envia el valor de un atributo o el array con todos los atributos y sus valores del router |
187
|
|
|
* Mirar el atributo vars del router |
188
|
|
|
* ej. |
189
|
|
|
* <code>Router::get()</code> |
190
|
|
|
* |
191
|
|
|
* ej. |
192
|
|
|
* <code>Router::get('controller')</code> |
193
|
|
|
* |
194
|
|
|
* @param string $var (opcional) un atributo: route, module, controller, action, parameters o routed |
195
|
|
|
* |
196
|
|
|
* @return array|string con el valor del atributo |
197
|
|
|
*/ |
198
|
|
|
public static function get($var = '') |
199
|
|
|
{ |
200
|
|
|
return ($var) ? self::$vars[$var] : self::$vars; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Redirecciona la ejecución internamente o externamente con un routes propio |
205
|
|
|
* |
206
|
|
|
* @param array $params array de $vars (móddulo, controller, action, params, ...) |
207
|
|
|
* @param boolean $intern si la redirección es interna |
208
|
|
|
* |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
public static function to(array $params, $intern = false) |
212
|
|
|
{ |
213
|
|
|
if ($intern) { |
214
|
|
|
self::$routed = true; |
215
|
|
|
} |
216
|
|
|
self::$vars = $params + self::$default; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|