refactor!: add Synapsis for dependency resolution and injection
- Remove Middleware class and custom callback handling logic. - Implement Synapsis as a dependency injection container for automatic resolution. - Refactor Router to use Synapsis for process route callbacks and not found handler. - Update Request to remove middleware-specific properties and use Router::$currentParams for path parameters.
This commit is contained in:
@@ -21,6 +21,7 @@ class Router
|
||||
private static $patch = [];
|
||||
private static $delete = [];
|
||||
private static $last;
|
||||
public static $currentParams = [];
|
||||
public static $notFoundCallback = 'Libs\Router::defaultNotFound';
|
||||
|
||||
/**
|
||||
@@ -325,55 +326,18 @@ class Router
|
||||
if (preg_match_all('/^' . $router['path'] . '\/?$/si', $path, $matches, PREG_PATTERN_ORDER)) {
|
||||
unset($matches[0]);
|
||||
|
||||
// Objtener un reflection del callback
|
||||
$lastCallback = $router['callback'][0];
|
||||
if ($lastCallback instanceof \Closure) { // si es función anónima
|
||||
$reflectionCallback = new \ReflectionFunction($lastCallback);
|
||||
} else {
|
||||
if (is_string($lastCallback)) {
|
||||
$lastCallback = preg_split('/::/', $lastCallback);
|
||||
}
|
||||
|
||||
// Revisamos su es un método o solo una función
|
||||
if (count($lastCallback) == 2) {
|
||||
$reflectionCallback = new \ReflectionMethod($lastCallback[0], $lastCallback[1]);
|
||||
} else {
|
||||
$reflectionCallback = new \ReflectionFunction($lastCallback[0]);
|
||||
}
|
||||
}
|
||||
|
||||
// Obtener los parámetros
|
||||
$arguments = $reflectionCallback->getParameters();
|
||||
if (isset($arguments[0])) {
|
||||
// Obtenemos la clase del primer parámetro
|
||||
$argumentClass = strval($arguments[0]->getType());
|
||||
|
||||
// Verificamos si la clase está o no tipada
|
||||
if (empty($argumentClass)) {
|
||||
$request = new Request();
|
||||
} else {
|
||||
$request = new $argumentClass();
|
||||
|
||||
// Verificamos que sea instancia de Request (requerimiento)
|
||||
if (!($request instanceof Request)) {
|
||||
throw new \Exception('Bad argument type on router callback.');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$request = new Request();
|
||||
}
|
||||
|
||||
// Comprobando y guardando los parámetros variables de la ruta
|
||||
if (isset($matches[1])) {
|
||||
foreach ($matches as $index => $match) {
|
||||
$paramName = $router['paramNames'][$index - 1];
|
||||
$request->params->$paramName = urldecode($match[0]);
|
||||
$paramName = $router['paramNames'][$index - 1];
|
||||
static::$currentParams[$paramName] = urldecode($match[0]);
|
||||
}
|
||||
}
|
||||
|
||||
// Llama a la validación y luego procesa la cola de callbacks
|
||||
$request->next = $router['callback'];
|
||||
$data = $request->handle();
|
||||
// Procesa la cola de callbacks
|
||||
foreach (array_reverse($router['callback']) as $callback) {
|
||||
$data = Synapsis::resolve($callback);
|
||||
}
|
||||
|
||||
// Por defecto imprime como JSON si se retorna algo
|
||||
if (isset($data)) {
|
||||
@@ -386,6 +350,6 @@ class Router
|
||||
}
|
||||
|
||||
// Si no hay router que coincida llamamos a $notFoundCallBack
|
||||
call_user_func_array(static::$notFoundCallback, [new Request()]);
|
||||
Synapsis::resolve(static::$notFoundCallback);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user