Add middleware priorities.

This commit is contained in:
kj 2022-06-07 23:05:49 -04:00
parent eb27acf68e
commit a641248453
1 changed files with 17 additions and 4 deletions

View File

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