codin / relay
Tiny PSR-15 Http Server Middleware Processor
Installs: 5 988
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.3
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friends-of-phpspec/phpspec-code-coverage: @stable
- friendsofphp/php-cs-fixer: @stable
- phpspec/phpspec: @stable
- phpstan/phpstan: @stable
This package is auto-updated.
Last update: 2024-11-22 08:37:37 UTC
README
Example
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; // Define some Middlewares $response = new class() implements MiddlewareInterface { public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $psr17Factory = new Nyholm\Psr7\Factory\Psr17Factory(); $responseBody = $psr17Factory->createStream(''); return $psr17Factory->createResponse(200)->withBody($responseBody); } }; $cors = new class() implements MiddlewareInterface { public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { return $handler->handle($request)->withHeader('Access-Control-Allow-Origin', '*'); } }; $exceptions = new class() implements MiddlewareInterface { public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { try { return $handler->handle($request); } catch (Throwable $exception) { $this->logger->error('Server Error', compact('exception')); $psr17Factory = new Nyholm\Psr7\Factory\Psr17Factory(); $responseBody = $psr17Factory->createStream('Server Error'); return $psr17Factory->createResponse(500)->withBody($responseBody); } } }; // Create a request $psr17Factory = new Nyholm\Psr7\Factory\Psr17Factory(); $request = $psr17Factory->createRequest('GET', 'http://tnyholm.se'); // Run middlewares on a request to create a response $relay = new Codin\Relay\Relay([$cors, $exceptions, $response]); $response = $relay->handle($request);