-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathFirstMiddleware.php
More file actions
33 lines (27 loc) · 964 Bytes
/
FirstMiddleware.php
File metadata and controls
33 lines (27 loc) · 964 Bytes
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
<?php
declare(strict_types=1);
namespace PhpMcp\Server\Tests\Fixtures\Middlewares;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use React\Promise\PromiseInterface;
class FirstMiddleware
{
public function __invoke(ServerRequestInterface $request, callable $next)
{
$result = $next($request);
return match (true) {
$result instanceof PromiseInterface => $result->then(fn($response) => $this->handle($response)),
$result instanceof ResponseInterface => $this->handle($result),
default => $result
};
}
private function handle($response)
{
if ($response instanceof ResponseInterface) {
$existing = $response->getHeaderLine('X-Middleware-Order');
$new = $existing ? $existing . ',first' : 'first';
return $response->withHeader('X-Middleware-Order', $new);
}
return $response;
}
}