From 1e349ba17c353da93113527e9d19bb3f592f03d9 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Sat, 28 Mar 2026 05:12:56 +0000 Subject: [PATCH] Refactoring magic method definition --- src/Application/Application.php | 39 ++++-------------- src/Router/Router.php | 71 +++++++++++++++------------------ 2 files changed, 39 insertions(+), 71 deletions(-) diff --git a/src/Application/Application.php b/src/Application/Application.php index e0bba97d..153f76e8 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -7,7 +7,6 @@ use Bow\Application\Exception\ApplicationException; use Bow\Configuration\Loader; use Bow\Container\Capsule; -use Bow\Container\Compass; use Bow\Contracts\ResponseInterface; use Bow\Http\Exception\BadRequestException; use Bow\Http\Exception\HttpException; @@ -86,6 +85,8 @@ public function __construct(Request $request, Response $response) $this->request = $request; $this->response = $response; + $this->request->capture(); + $this->router = Router::configure($request->get('_method')); $this->capsule = Capsule::getInstance(); @@ -93,8 +94,6 @@ public function __construct(Request $request, Response $response) $this->capsule->instance('request', $request); $this->capsule->instance('router', $this->router); $this->capsule->instance('app', $this); - - $this->request->capture(); } /** @@ -149,13 +148,6 @@ public function run(): bool $method = $this->request->method(); - // We verify the existence of a special method DELETE, PUT - if ($method == 'POST') { - if ($this->router->hasSpecialMethod()) { - $method = $this->router->getSpecialMethod(); - } - } - // We verify the existence of the method of the request in // the routing collection $routes = $this->router->getRoutes(); @@ -186,23 +178,16 @@ public function run(): bool // Error management if ($resolved) { - $this->sendResponse($response); + $this->send($response); return true; } // We apply the 404 error code $this->response->status(404); - $error_code = $this->router->getErrorCodes(); - - if (!array_key_exists(404, $error_code)) { - throw new RouterException( - sprintf('Route "%s" not found', $this->request->path()) - ); - } - - $response = Compass::getInstance()->execute($this->router->getErrorCodes(), []); - $this->sendResponse($response, 404); + throw new RouterException( + sprintf('Route "%s" not found', $this->request->path()) + ); return false; } @@ -214,7 +199,7 @@ public function run(): bool * @param int $code * @return void */ - private function sendResponse(mixed $response, int $code = 200): void + private function send(mixed $response, int $code = 200): void { if ($response instanceof ResponseInterface) { $response->sendContent(); @@ -423,16 +408,6 @@ public function __invoke(...$params): mixed return $this->capsule->bind($params[0], $params[1]); } - /** - * Send the application response - * - * @return void - */ - public function send(): void - { - $this->run(); - } - /** * Delegate method calls to the router * diff --git a/src/Router/Router.php b/src/Router/Router.php index 698e597a..70a97d9f 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -21,7 +21,7 @@ class Router * * @var array */ - protected array $error_code = []; + protected array $error_codes = []; /** * Define the global middleware @@ -37,11 +37,6 @@ class Router */ protected string $prefix = ''; - /** - * @var ?string - */ - protected ?string $special_method = null; - /** * Define the domain constraint for routes * @@ -247,7 +242,7 @@ public function route(array $definition): void unset($cb['controller']); } - $route = $this->pushHttpVerb($method, $path, $cb); + $route = $this->pushMany($method, $path, $cb); if (isset($definition['middleware'])) { $route->middleware($definition['middleware']); @@ -261,28 +256,24 @@ public function route(array $definition): void } /** - * Add other HTTP verbs [PUT, DELETE, UPDATE, HEAD, PATCH] + * Add other HTTP verbs [PUT, DELETE, OPTIONS, HEAD, PATCH] * * @param string|array $methods * @param string $path * @param callable|array|string $cb * @return Route */ - private function pushHttpVerb(string|array $methods, string $path, callable|string|array $cb): Route + private function pushMany(string|array $methods, string $path, callable|string|array $cb): Route { $methods = (array) $methods; - if (!$this->magic_method) { - return $this->routeLoader($methods, $path, $cb); - } - foreach ($methods as $key => $method) { - if ($this->magic_method === $method) { - $methods[$key] = $this->magic_method; + if (in_array($this->magic_method, ['PUT', 'DELETE', 'PATCH']) && in_array($method, ['PUT', 'DELETE', 'PATCH'])) { + $methods[$key] = 'POST'; } } - return $this->routeLoader($methods, $path, $cb); + return $this->push($methods, $path, $cb); } /** @@ -293,7 +284,7 @@ private function pushHttpVerb(string|array $methods, string $path, callable|stri * @param callable|string|array $cb * @return Route */ - private function routeLoader(string|array $methods, string $path, callable|string|array $cb): Route + private function push(string|array $methods, string $path, callable|string|array $cb): Route { $methods = (array) $methods; @@ -361,7 +352,7 @@ public function any(string $path, callable|string|array $cb): Route { $methods = array_map('strtoupper', ['options', 'patch', 'post', 'delete', 'put', 'get']); - return $this->pushHttpVerb($methods, $path, $cb); + return $this->pushMany($methods, $path, $cb); } /** @@ -373,7 +364,7 @@ public function any(string $path, callable|string|array $cb): Route */ public function get(string $path, callable|string|array $cb): Route { - return $this->routeLoader('GET', $path, $cb); + return $this->push('GET', $path, $cb); } /** @@ -385,17 +376,7 @@ public function get(string $path, callable|string|array $cb): Route */ public function post(string $path, callable|string|array $cb): Route { - if (!$this->magic_method) { - return $this->routeLoader('POST', $path, $cb); - } - - $method = strtoupper($this->magic_method); - - if (in_array($method, ['DELETE', 'PUT'])) { - $this->special_method = $method; - } - - return $this->pushHttpVerb($method, $path, $cb); + return $this->push('POST', $path, $cb); } /** @@ -407,7 +388,11 @@ public function post(string $path, callable|string|array $cb): Route */ public function delete(string $path, callable|string|array $cb): Route { - return $this->pushHttpVerb('DELETE', $path, $cb); + if ($this->magic_method && strtoupper($this->magic_method) === 'DELETE') { + return $this->post($path, $cb); + } + + return $this->push('DELETE', $path, $cb); } /** @@ -419,7 +404,11 @@ public function delete(string $path, callable|string|array $cb): Route */ public function put(string $path, callable|string|array $cb): Route { - return $this->pushHttpVerb('PUT', $path, $cb); + if ($this->magic_method && strtoupper($this->magic_method) === 'PUT') { + return $this->post($path, $cb); + } + + return $this->push('PUT', $path, $cb); } /** @@ -431,7 +420,11 @@ public function put(string $path, callable|string|array $cb): Route */ public function patch(string $path, callable|string|array $cb): Route { - return $this->pushHttpVerb('PATCH', $path, $cb); + if ($this->magic_method && strtoupper($this->magic_method) === 'PATCH') { + return $this->post($path, $cb); + } + + return $this->push('PATCH', $path, $cb); } /** @@ -443,7 +436,7 @@ public function patch(string $path, callable|string|array $cb): Route */ public function options(string $path, callable|string|array $cb): Route { - return $this->pushHttpVerb('OPTIONS', $path, $cb); + return $this->push('OPTIONS', $path, $cb); } /** @@ -456,7 +449,7 @@ public function options(string $path, callable|string|array $cb): Route */ public function code(int $code, callable|array|string $cb): Router { - $this->error_code[$code] = $cb; + $this->error_codes[$code] = $cb; return $this; } @@ -468,7 +461,7 @@ public function code(int $code, callable|array|string $cb): Router */ public function getErrorCodes(): array { - return $this->error_code; + return $this->error_codes; } /** @@ -483,7 +476,7 @@ public function match(array $methods, string $path, callable|string|array $cb): { $methods = array_map('strtoupper', $methods); - return $this->pushHttpVerb($methods, $path, $cb); + return $this->pushMany($methods, $path, $cb); } /** @@ -503,7 +496,7 @@ public function getRoutes(): array */ public function getSpecialMethod(): string { - return $this->special_method; + return $this->magic_method; } /** @@ -513,7 +506,7 @@ public function getSpecialMethod(): string */ public function hasSpecialMethod(): bool { - return !is_null($this->special_method); + return !is_null($this->magic_method); } /**