Add middleware priorities.

This commit is contained in:
kj 2022-06-07 23:05:49 -04:00
parent eb27acf68e
commit a641248453

View File

@ -92,16 +92,18 @@ class Router {
header('Location: '.static::basePath().substr($path,1));
}
/*
/**
* Añade un middleware a la última ruta usada.
* Solo se puede usar un middleware a la vez.
*
* @param mixed $callback
* @param int $prioriry
*
* @return Router
* Devuelve un enlace estático.
*/
public static function middleware($callback) : Router{
public static function middleware($callback, int $priority = null) : Router {
if (!isset(static::$last))
return new static();
@ -112,12 +114,23 @@ class Router {
$callback = 'Middlewares\\'.$callback;
}
if (isset($priority) && $priority <= 0)
$priority = 1;
if (is_null($priority) || $priority >= count(static::$$method[$index]['callback']))
static::$$method[$index]['callback'][] = $callback;
else {
static::$$method[$index]['callback'] = array_merge(
array_slice(static::$$method[$index]['callback'], 0, $priority),
[$callback],
array_slice(static::$$method[$index]['callback'], $priority)
);
}
return new static();
}
/*
/**
* @return Neuron
* Devuelve un objeto que contiene los atributos:
* post - Donde se encuentran los valores enviados por $_POST.