orbiter / middleware-utils
Middleware Utils with PHP-DI, universal for PSR-7/17, 15 Middlewares
Requires
- php: >=7.3
- ext-json: *
- php-di/php-di: 6.*
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
This package is auto-updated.
Last update: 2024-12-06 07:33:51 UTC
README
Utilities and middlewares for PSR-7/17, PSR-15 middleware systems for usage with PHP-DI.
HasResponseFactory
Adds a response factory to a middleware with various factory methods, relies on DI-injection of Psr\Http\Message\ResponseFactoryInterface
and Psr\Http\Message\StreamFactoryInterface
.
<?php use Orbiter\MiddlewareUtils\HasResponseFactory; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; class SampleMiddleware implements MiddlewareInterface { use HasResponseFactory; public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { if($request->hasHeader('error') === 'some-strange-bug') { // Simple Status Response return $this->create500(); // Same like `create500` return $this->createResponse(500, 'Internal Server Error'); } if($request->hasHeader('error') === 'client-error') { // Creating: 400 Bad Request with JSON Body return $this->respondJson($this->create400(), ['message' => 'client-error-msg']); } return $handler->handle($request); } }
createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
- create any empty responserespondJson(ResponseInterface $response, $data): ResponseInterface
- uses the response and adds header and datacreate400(): ResponseInterface
- Bad Requestcreate401(): ResponseInterface
- Unauthorizedcreate402(): ResponseInterface
- Payment Requiredcreate403(): ResponseInterface
- Forbiddencreate404(): ResponseInterface
- Not Foundcreate405(): ResponseInterface
- Method Not Allowedcreate409(): ResponseInterface
- Conflictcreate410(): ResponseInterface
- Gonecreate413(): ResponseInterface
- Payload Too Largecreate415(): ResponseInterface
- Unsupported Media Typecreate440(): ResponseInterface
- Login Time-outcreate500(): ResponseInterface
- Internal Server Errorcreate501(): ResponseInterface
- Not Implementedcreate502(): ResponseInterface
- Bad Gatewaycreate503(): ResponseInterface
- Service Unavailable
ApiError
Unified error body:
<?php use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Orbiter\MiddlewareUtils\HasResponseFactory; use Orbiter\MiddlewareUtils\ApiError; class SampleMiddleware implements MiddlewareInterface { use HasResponseFactory; public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { if($request->hasHeader('error') === 'user-not-found') { // Creating: 400 Bad Request with ApiError as JSON Body return $this->respondJson($this->create404(), new ApiError('User Not Found')); } return $handler->handle($request); } }
CorsMiddleware
Dead-Simple CORS middleware, support multiple origins.
Create the middleware with any DI-factory, add to any middleware pipe.
<?php use Orbiter\MiddlewareUtils\CorsMiddleware; $pipe = new RespondPipe(); /** * @var DI\FactoryInterface $factory */ $pipe->with( $factory->make(CorsMiddleware::class, [ 'origins_allowed' => [ 'http://localhost:3000', 'https://admin.example.org', ], 'headers_allowed' => [ 'Content-Type', 'Accept', 'AUTHORIZATION', 'X-Requested-With', 'X_AUTH_TOKEN', 'X_AUTH_SIGNATURE', 'X_API_OPTION', 'remember-me', ], 'headers_expose' => [ 'Content-Range', ], 'max_age' => 2, ]) );
CORSMiddleware Zend-Expressive
<?php use Zend\Stratigility\MiddlewarePipe; use DI\FactoryInterface; use Orbiter\MiddlewareUtils\CorsMiddleware; class PipelineFactory { public function __invoke(FactoryInterface $factory) { $pipeline = new MiddlewarePipe(); // create CORS Middleware with PHP-DI $pipeline->pipe($factory->make(CorsMiddleware::class, [ 'origins_allowed' => ['http://localhost:3000'], 'headers_allowed' => [ 'Content-Type', 'Accept', 'AUTHORIZATION', 'X-Requested-With', 'X_AUTH_TOKEN', 'X_AUTH_SIGNATURE', 'X_API_OPTION', 'remember-me', ], 'headers_expose' => [ 'Content-Range', ], 'max_age' => 2, ])); $pipeline->pipe(OtherMiddleware::class); // ... return $pipeline; } }
License
This project is free software distributed under the MIT License.
See: LICENSE.
Contributors
By committing your code to the code repository you agree to release the code under the MIT License attached to the repository.
Maintained by Michael Becker