From c41ba28357adad34398a09adb62af9f301d152a8 Mon Sep 17 00:00:00 2001 From: Igor Ilic Date: Sat, 4 May 2024 11:58:37 +0200 Subject: [PATCH] QoL: fix middleware issues --- src/Routes.php | 82 +++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/src/Routes.php b/src/Routes.php index 15a1b1d..f7a5c0b 100644 --- a/src/Routes.php +++ b/src/Routes.php @@ -371,46 +371,54 @@ private function get_path() : string { * * @throws CallbackNotFound When the specified middleware method is not found */ - private function execute_middleware(array $data) : void { - $namedArguments = match ( is_null($this->currentRoute) ) { - false => $this->get_route_arguments($this->currentRoute, $this->get_path()), - default => [] - }; - - foreach ( $data as $key => $function ) { - $arguments = []; - $tmpArguments = []; - - if ( is_integer($key) && is_array($function) ) { - $class = $function[0]; - $method = $function[1]; - array_shift($function); - array_shift($function); - $tmpArguments = $function; - $function = [ new $class, $method ]; - } + private function execute_middleware(array $data): void + { + $namedArguments = match (is_null($this->currentRoute)) { + false => $this->get_route_arguments($this->currentRoute, $this->get_path()), + default => [] + }; + + foreach ( $data as $key => $function ) { + $arguments = []; + $tmpArguments = []; + + if ( is_integer($key) && is_array($function) ) { + $class = $function[0]; + $method = $function[1]; + array_shift($function); + array_shift($function); + $tmpArguments = $function; + $function = [new $class, $method]; + } - if ( is_string($key) ) { - $tmpArguments = [ $function ]; - $function = $key; - } + if ( is_string($key) ) { + $tmpArguments = [$function]; + $function = $key; + } - $parameters = $this->get_all_arguments($function); - $requestClassIndex = array_search(Request::class, array_values($parameters)); + $parameters = $this->get_all_arguments($function) ?? []; + $requestClassIndex = array_search(Request::class, array_values($parameters)); + $responseClassIndex = array_search(Response::class, array_values($parameters)); - $paramNames = array_keys($parameters); - for ( $index = 0; $index < count($parameters); $index++ ) { - if ( $index === $requestClassIndex ) { - $arguments[$index] = $this->request; - continue; - } - $arguments[$index] = $tmpArguments[$index] ?? $namedArguments[$paramNames[$index]] ?? NULL; - } + $paramNames = array_keys($parameters); + for ( $index = 0; $index < count($parameters); $index++ ) { + if ( $index === $requestClassIndex ) { + $arguments[$index] = $this->request; + continue; + } - if ( !is_callable($function) ) throw new CallbackNotFound("Middleware method $function not found", 404); - call_user_func($function, ...$arguments); - } - } + if ( $index === $responseClassIndex ) { + $arguments[$index] = $this->response; + continue; + } + + $arguments[$index] = $tmpArguments[$index] ?? $namedArguments[$paramNames[$index]] ?? NULL; + } + + if ( !is_callable($function) ) throw new CallbackNotFound("Middleware method $function not found", 404); + call_user_func($function, ...$arguments); + } + } /** * Private method used to fetch the arguments of the route's callback methods @@ -581,4 +589,4 @@ public function append(array $routes) : self { $this->routes = array_merge_recursive($routes, $this->routes); return $this; } - } + } \ No newline at end of file