https://github.com/bemit/middleware-utils
Middleware Utils with PHP-DI, universal for PSR-7/17, 15 Middlewares
https://github.com/bemit/middleware-utils
Last synced: 24 days ago
JSON representation
Middleware Utils with PHP-DI, universal for PSR-7/17, 15 Middlewares
- Host: GitHub
- URL: https://github.com/bemit/middleware-utils
- Owner: bemit
- License: mit
- Created: 2020-01-05T18:09:43.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-05T20:43:28.000Z (over 5 years ago)
- Last Synced: 2025-02-15T00:32:59.523Z (3 months ago)
- Language: PHP
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Middleware Utils
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
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 response
- `respondJson(ResponseInterface $response, $data): ResponseInterface` - uses the response and adds header and data
- `create400(): ResponseInterface` - Bad Request
- `create401(): ResponseInterface` - Unauthorized
- `create402(): ResponseInterface` - Payment Required
- `create403(): ResponseInterface` - Forbidden
- `create404(): ResponseInterface` - Not Found
- `create405(): ResponseInterface` - Method Not Allowed
- `create409(): ResponseInterface` - Conflict
- `create410(): ResponseInterface` - Gone
- `create413(): ResponseInterface` - Payload Too Large
- `create415(): ResponseInterface` - Unsupported Media Type
- `create440(): ResponseInterface` - Login Time-out
- `create500(): ResponseInterface` - Internal Server Error
- `create501(): ResponseInterface` - Not Implemented
- `create502(): ResponseInterface` - Bad Gateway
- `create503(): ResponseInterface` - Service Unavailable## ApiError
Unified error body:
```php
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
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
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](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](https://mlbr.xyz)