add mising types, patch method and a new way to set middleware

- Removed resumed way to set controllers and middlewares:
  Now, the method of routers and middlewares need to be
  callables.
- Add patch method in order to support PATCH http method.
- Add a way to get add more middlewares after. For Example:

  <?php
    use Libs\Router;

    Router::get('/some-route', 'somefunction')
      ->middleware('firstmiddleware');
    Router::get('/other-route', 'otherfunction');

    // Recover first router and add a middleware
    Router::get('/some-route')->middleware('secondmiddleware');
  ?>
This commit is contained in:
KJ 2023-08-22 01:48:04 -04:00
parent af673a68b8
commit 030e1079dc
1 changed files with 78 additions and 46 deletions

View File

@ -1,4 +1,4 @@
<?php
<?php
/**
* Router - DuckBrain
*
@ -14,9 +14,10 @@
namespace Libs;
class Router {
private static $get = [];
private static $post = [];
private static $put = [];
private static $get = [];
private static $post = [];
private static $put = [];
private static $patch = [];
private static $delete = [];
private static $last;
public static $notFoundCallback = 'Libs\Router::defaultNotFound';
@ -43,7 +44,7 @@ class Router {
* @param string $path
* Ruta con pseudovariables.
*
* @param mixed $callback
* @param callable $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return array
@ -51,7 +52,7 @@ class Router {
* path - Contiene la ruta con las pseudovariables reeplazadas por expresiones regulares.
* callback - Contiene el callback en formato Namespace\Clase::Método.
*/
private static function parse(string $path, $callback): array {
private static function parse(string $path, callable $callback): array {
preg_match_all('/{(\w+)}/s', $path, $matches, PREG_PATTERN_ORDER);
$paramNames = $matches[1];
@ -61,10 +62,6 @@ class Router {
['([^\/]+)'],
$path);
if (!is_callable($callback)) {
$callback = 'Controllers\\'.$callback;
}
return [
'path' => $path,
'callback' => [$callback],
@ -107,24 +104,19 @@ class Router {
* Añade un middleware a la última ruta usada.
* Solo se puede usar un middleware a la vez.
*
* @param mixed $callback
* @param callable $callback
* @param int $prioriry
*
* @return static
* Devuelve un enlace estático.
* Devuelve la instancia actual.
*/
public static function middleware($callback, int $priority = null): static {
public static function middleware(callable $callback, int $priority = null): static {
if (!isset(static::$last))
return new static();
$method = static::$last[0];
$index = static::$last[1];
if (!is_callable($callback)) {
$callback = 'Middlewares\\'.$callback;
}
if (isset($priority) && $priority <= 0)
$priority = 1;
@ -171,22 +163,53 @@ class Router {
return (object) '';
}
/**
* Configura calquier método para todas las rutas.
*
* @param string $method
* Método http.
* @param string $path
* Ruta con pseudovariables.
* @param callable|null $callback
*
* @return
* Devuelve la instancia actual.
*/
public static function configure(string $method, string $path, ?callable $callback = null): static {
if (is_null($callback)) {
$path = preg_quote($path, '/');
$path = preg_replace(
['/\\\{\w+\\\}/s'],
['([^\/]+)'],
$path);
foreach(static::$$method as $index => $router)
if ($router['path'] == $path) {
static::$last = [$method, $index];
break;
}
return new static();
}
static::$$method[] = static::parse($path, $callback);
static::$last = [$method, count(static::$$method)-1];
return new static();
}
/**
* Define los routers para el método GET.
*
* @param string $path
* Ruta con pseudovariables.
*
* @param mixed $callback
* @param callable $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return static
* Devuelve un enlace estático.
* Devuelve la instancia actual.
*/
public static function get(string $path, $callback): static {
static::$get[] = static::parse($path, $callback);
static::$last = ['get', count(static::$get)-1];
return new static();
public static function get(string $path, callable $callback = null): static {
return static::configure('get', $path, $callback);
}
/**
@ -194,17 +217,14 @@ class Router {
*
* @param string $path
* Ruta con pseudovariables.
*
* @param mixed $callback
* @param callable $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return static
* Devuelve un enlace estático.
* Devuelve la instancia actual.
*/
public static function post(string $path, $callback): static {
static::$post[] = static::parse($path, $callback);
static::$last = ['post', count(static::$post)-1];
return new static();
public static function post(string $path, callable $callback = null): static {
return static::configure('post', $path, $callback);
}
/**
@ -212,18 +232,30 @@ class Router {
*
* @param string $path
* Ruta con pseudovariables.
*
* @param mixed $callback
* @param callable $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return static
* Devuelve un enlace estático
* Devuelve la instancia actual
*/
public static function put(string $path, $callback): static {
static::$put[] = static::parse($path, $callback);
static::$last = ['put', count(static::$put)-1];
return new static();
public static function put(string $path, callable $callback = null): static {
return static::configure('put', $path, $callback);
}
/**
* Define los routers para el método PATCH.
*
* @param string $path
* Ruta con pseudovariables.
* @param callable $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return static
* Devuelve la instancia actual
*/
public static function patch(string $path, callable $callback = null): static {
return static::configure('patch', $path, $callback);
}
/**
@ -231,17 +263,14 @@ class Router {
*
* @param string $path
* Ruta con pseudovariables
*
* @param callable $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return static
* Devuelve un enlace estático
* Devuelve la instancia actual
*/
public static function delete(string $path, $callback): static {
static::$delete[] = static::parse($path, $callback);
static::$last = ['delete', count(static::$delete)-1];
return new static();
public static function delete(string $path, callable $callback = null): static {
return static::configure('delete', $path, $callback);
}
/**
@ -284,7 +313,10 @@ class Router {
case 'PUT':
$routers = static::$put;
break;
case 'DELETE':
case 'PATCH':
$routers = static::$patch;
break;
case 'DELETE':
$routers = static::$delete;
break;
default: