php-sdk/src/Server/Transport/Http/MiddlewareRequestHandler.php
Christopher Hertel eac6f01d58
Improve middleware pipeline consistency (#262)
- Make MiddlewareRequestHandler final and use index counter instead of
  array_shift to avoid mutating internal state
- Make OAuthProxyMiddleware factory discovery consistently lazy via
  getter methods, matching httpClient and requestFactory pattern

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 10:25:23 +01:00

48 lines
1.2 KiB
PHP

<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Server\Transport\Http;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* A request handler that processes a middleware pipeline before dispatching
* the request to the core transport handler.
*
* @author Volodymyr Panivko <sveneld300@gmail.com>
*
* @internal
*/
final class MiddlewareRequestHandler implements RequestHandlerInterface
{
private int $index = 0;
/**
* @param list<MiddlewareInterface> $middleware
*/
public function __construct(
private readonly array $middleware,
private readonly \Closure $application,
) {
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
if (!isset($this->middleware[$this->index])) {
return ($this->application)($request);
}
return $this->middleware[$this->index++]->process($request, $this);
}
}