$middleware */ public function __construct( private readonly array $middleware, private readonly MiddlewareResolverInterface $resolver, private readonly RequestHandlerInterface $finalHandler, ) { } public function handle(ServerRequestInterface $request): ResponseInterface { if ($this->cursor < \count($this->middleware)) { $entry = $this->middleware[$this->cursor]; ++$this->cursor; $middleware = $this->resolver->resolve($entry); return $middleware->process($request, $this); } // Stack exhausted: delegate to the terminal handler (router + controller). return $this->finalHandler->handle($request); } }