$middlewares * @param callable(Request): Response $handler * @return callable(Request): Response */ function compose($middlewares, $handler) { $next = $handler; foreach (array_reverse($middlewares) as $middleware) { $next = function ($request) use ($middleware, $next) { return $middleware->send($request, $next); }; } return $next; }