next = $handler; return $this->handle($request); } /** * Handle the request and return a response. * * It calls all configured middleware and handles their response */ public function handle(ServerRequestInterface $request): ResponseInterface { $middleware = array_shift($this->stack); if (!$middleware) { if ($this->next) { return $this->next->handle($request); } throw new LogicException('Middleware queue is empty'); } $middleware = $this->resolveMiddleware($middleware); if (!$middleware instanceof MiddlewareInterface) { throw new InvalidArgumentException('Middleware is no instance of ' . MiddlewareInterface::class); } return $middleware->process($request, $this); } public function setContainer(Application $container): void { $this->container = $container; } }