-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathMiddlewareRequestHandler.php
More file actions
48 lines (41 loc) · 1.24 KB
/
MiddlewareRequestHandler.php
File metadata and controls
48 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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);
}
}