Compare commits

..

No commits in common. "eb27acf68ecb521cd8da6a8c8c8ff77e8b6060b0" and "4003a88f66b3d6399bebc9b88640ceaf5a5129b8" have entirely different histories.

2 changed files with 11 additions and 37 deletions

View File

@ -1,27 +0,0 @@
<?php
/**
* Middleware - DuckBrain
*
* Librería base para middlewares.
*
* @author KJ
* @website https://kj2.me
* @licence MIT
*/
namespace Libs;
class Middleware {
/**
* Llama al siguiente callback.
*
* @param Neuron $req
*
* @return mixed
*/
public static function next(Neuron $req) {
$next = array_pop($req->next);
return call_user_func_array($next, [$req]);
}
}

View File

@ -21,7 +21,7 @@ class Router {
private static $last; private static $last;
public static $notFoundCallback = 'Libs\Router::defaultNotFound'; public static $notFoundCallback = 'Libs\Router::defaultNotFound';
public static function defaultNotFound () { private static function defaultNotFound () {
header("HTTP/1.0 404 Not Found"); header("HTTP/1.0 404 Not Found");
echo '<h2 style="text-align: center;margin: 25px 0px;">Error 404 - Página no encontrada</h2>'; echo '<h2 style="text-align: center;margin: 25px 0px;">Error 404 - Página no encontrada</h2>';
} }
@ -58,7 +58,7 @@ class Router {
return [ return [
'path' => $path, 'path' => $path,
'callback' => [$callback], 'callback' => $callback,
'paramNames' => $paramNames 'paramNames' => $paramNames
]; ];
} }
@ -112,7 +112,7 @@ class Router {
$callback = 'Middlewares\\'.$callback; $callback = 'Middlewares\\'.$callback;
} }
static::$$method[$index]['callback'][] = $callback; static::$$method[$index]['middleware'] = $callback;
return new static(); return new static();
} }
@ -267,7 +267,7 @@ class Router {
break; break;
} }
$req = static::getReq(); $args = static::getReq();
foreach ($routers as $router) { // revisa todos los routers para ver si coinciden con la ruta actual foreach ($routers as $router) { // revisa todos los routers para ver si coinciden con la ruta actual
if (preg_match_all('/^'.$router['path'].'\/?$/si',$path, $matches, PREG_PATTERN_ORDER)) { if (preg_match_all('/^'.$router['path'].'\/?$/si',$path, $matches, PREG_PATTERN_ORDER)) {
@ -277,14 +277,15 @@ class Router {
if (isset($matches[1])) { if (isset($matches[1])) {
foreach ($matches as $index => $match) { foreach ($matches as $index => $match) {
$paramName = $router['paramNames'][$index-1]; $paramName = $router['paramNames'][$index-1];
$req->params->$paramName = urldecode($match[0]); $args->params->$paramName = urldecode($match[0]);
} }
} }
// Llamar al último callback configurado // Comprobando si hay middleware
$next = array_pop($router['callback']); if (isset($router['middleware']))
$req->next = $router['callback']; $data = call_user_func_array($router['middleware'], [$router['callback'], $args]);
$data = call_user_func_array($next, [$req]); else
$data = call_user_func_array($router['callback'], [$args]);
if (isset($data)) { if (isset($data)) {
header('Content-Type: application/json'); header('Content-Type: application/json');
@ -296,7 +297,7 @@ class Router {
} }
// Si no hay router que coincida llamamos a $notFoundCallBack // Si no hay router que coincida llamamos a $notFoundCallBack
call_user_func_array(static::$notFoundCallback, [$req]); call_user_func_array(static::$notFoundCallback, [$args]);
} }
} }
?> ?>