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