|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Stub adapters for PSR7 and PSR15. |
| 4 | + * |
| 5 | + * @package TheWebSolver\Codegarage\Test |
| 6 | + * |
| 7 | + * @phpcs:disable Universal.Namespaces.DisallowCurlyBraceSyntax.Forbidden |
| 8 | + * @phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 9 | + * @phpcs:disable Universal.Namespaces.OneDeclarationPerFile.MultipleFound |
| 10 | + * @phpcs:disable Universal.Namespaces.DisallowDeclarationWithoutName.Forbidden |
| 11 | + */ |
| 12 | + |
| 13 | +declare( strict_types = 1 ); |
| 14 | + |
| 15 | +namespace TheWebSolver\Codegarage\Lib\Psr7Adapter { |
| 16 | + interface ServerRequestInterface { |
| 17 | + public function withAttribute( string $name, mixed $value ): static; |
| 18 | + public function getAttribute( string $name, mixed $default = null ): mixed; |
| 19 | + } |
| 20 | + |
| 21 | + interface ResponseInterface { |
| 22 | + public function withStatus( int $code ): static; |
| 23 | + public function getStatusCode(): int; |
| 24 | + } |
| 25 | + |
| 26 | + class Request implements ServerRequestInterface { |
| 27 | + /** @var array<string,mixed> */ |
| 28 | + private array $attribute; |
| 29 | + |
| 30 | + public function withAttribute( string $name, mixed $value ): static { |
| 31 | + $new = clone $this; |
| 32 | + $new->attribute[ $name ] = $value; |
| 33 | + |
| 34 | + return $new; |
| 35 | + } |
| 36 | + |
| 37 | + public function getAttribute( string $name, mixed $default = null ): mixed { |
| 38 | + return $this->attribute[ $name ] ?? $default; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + class Response implements ResponseInterface { |
| 43 | + private int $statusCode; |
| 44 | + |
| 45 | + public function withStatus( int $code ): static { |
| 46 | + $new = clone $this; |
| 47 | + $new->statusCode = $code; |
| 48 | + |
| 49 | + return $new; |
| 50 | + } |
| 51 | + |
| 52 | + public function getStatusCode(): int { |
| 53 | + return $this->statusCode; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +namespace TheWebSolver\Codegarage\Lib\Psr15Adapter { |
| 59 | + use TheWebSolver\Codegarage\Lib\PipelineBridge; |
| 60 | + use TheWebSolver\Codegarage\Lib\Psr7Adapter\{ ServerRequestInterface, ResponseInterface }; |
| 61 | + |
| 62 | + interface RequestHandlerInterface { |
| 63 | + public function handle( ServerRequestInterface $request ): ResponseInterface; |
| 64 | + } |
| 65 | + |
| 66 | + interface MiddlewareInterface { |
| 67 | + public function process( |
| 68 | + ServerRequestInterface $request, |
| 69 | + RequestHandlerInterface $handler |
| 70 | + ): ResponseInterface; |
| 71 | + } |
| 72 | + |
| 73 | + class Middleware implements MiddlewareInterface { |
| 74 | + public function process( |
| 75 | + ServerRequestInterface $request, |
| 76 | + RequestHandlerInterface $handler |
| 77 | + ): ResponseInterface { |
| 78 | + $response = $request |
| 79 | + ->getAttribute( name: PipelineBridge::MIDDLEWARE_RESPONSE ) |
| 80 | + ->withStatus( code: 200 ); |
| 81 | + |
| 82 | + return $response; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +namespace { |
| 88 | + use TheWebSolver\Codegarage\Lib\Psr7Adapter\{ ServerRequestInterface, ResponseInterface }; |
| 89 | + use TheWebSolver\Codegarage\Lib\Psr15Adapter\{ MiddlewareInterface, RequestHandlerInterface }; |
| 90 | + |
| 91 | + class MiddlewareAdapter implements MiddlewareInterface { |
| 92 | + public function __construct( private readonly \Closure $middleware ) {} |
| 93 | + |
| 94 | + public function process( |
| 95 | + ServerRequestInterface $request, |
| 96 | + RequestHandlerInterface $handler |
| 97 | + ): ResponseInterface { |
| 98 | + return ( $this->middleware )( $request, $handler ); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments