1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Server\Transport\Http;
15:
16: use Psr\Http\Message\ResponseInterface;
17: use Psr\Http\Message\ServerRequestInterface;
18: use Psr\Http\Server\MiddlewareInterface;
19: use Psr\Http\Server\RequestHandlerInterface;
20:
21: /**
22: * Composes PSR-15 middleware outermost-first in front of an inner request handler.
23: */
24: final readonly class MiddlewarePipeline implements RequestHandlerInterface
25: {
26: /**
27: * @var list<MiddlewareInterface>
28: */
29: private array $middleware;
30:
31: public function __construct(
32: private RequestHandlerInterface $handler,
33: MiddlewareInterface ...$middleware,
34: ) {
35: $this->middleware = array_values($middleware);
36: }
37:
38: #[\Override]
39: public function handle(ServerRequestInterface $request): ResponseInterface
40: {
41: if ([] === $this->middleware) {
42: return $this->handler->handle($request);
43: }
44:
45: return $this->middleware[0]->process(
46: $request,
47: new self($this->handler, ...\array_slice($this->middleware, 1)),
48: );
49: }
50: }
51: