Skip to content

Commit

Permalink
QoL: fix middleware issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gigili committed May 4, 2024
1 parent 1a1bbb9 commit c41ba28
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions src/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -581,4 +589,4 @@ public function append(array $routes) : self {
$this->routes = array_merge_recursive($routes, $this->routes);
return $this;
}
}
}

0 comments on commit c41ba28

Please sign in to comment.