From 030e1079dc8fe32223eb4dac33d68a78cbb9df5d Mon Sep 17 00:00:00 2001 From: KJ Date: Tue, 22 Aug 2023 01:48:04 -0400 Subject: [PATCH] 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: middleware('firstmiddleware'); Router::get('/other-route', 'otherfunction'); // Recover first router and add a middleware Router::get('/some-route')->middleware('secondmiddleware'); ?> --- src/Libs/Router.php | 124 ++++++++++++++++++++++++++++---------------- 1 file changed, 78 insertions(+), 46 deletions(-) diff --git a/src/Libs/Router.php b/src/Libs/Router.php index e682fdf..5022ca5 100644 --- a/src/Libs/Router.php +++ b/src/Libs/Router.php @@ -1,4 +1,4 @@ - $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: