Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pew-pew-team/http-factory
Factory for request decoding and response encoding
https://github.com/pew-pew-team/http-factory
accept bundle content-type decoder encoder factory http request response symfony symfony-bundle
Last synced: about 1 month ago
JSON representation
Factory for request decoding and response encoding
- Host: GitHub
- URL: https://github.com/pew-pew-team/http-factory
- Owner: pew-pew-team
- License: mit
- Created: 2024-03-17T12:34:50.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-03-17T13:05:58.000Z (8 months ago)
- Last Synced: 2024-09-30T16:04:48.600Z (about 2 months ago)
- Topics: accept, bundle, content-type, decoder, encoder, factory, http, request, response, symfony, symfony-bundle
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTP Factory
A set of drivers for encoding HTTP responses and decoding HTTP requests.
## Installation
PewPew HTTP Factory is available as Composer repository and can be installed
using the following command in a root of your project:```bash
$ composer require pew-pew/http-factory
```More detailed installation [instructions are here](https://getcomposer.org/doc/01-basic-usage.md).
## Usage
### Decoder
```php
// Symfony Request
$request = new \Symfony\Component\HttpFoundation\Request();// Requests Factory
$requests = new \PewPew\HttpFactory\RequestDecoderFactory([
new \PewPew\HttpFactory\Driver\JsonDriver(),
]);$payload = $requests
->createDecoder($request) // Detect passed "content-type" header and
// create decoder if available.
?->decode($request->getContent(true)); // Decode request body.
```### Encoder
```php
// Symfony Request
$request = new \Symfony\Component\HttpFoundation\Request();// Responses Factory
$responses = new \PewPew\HttpFactory\ResponseEncoderFactory([
new \PewPew\HttpFactory\Driver\JsonDriver(),
]);$response = $responses
->createEncoder($request) // Detect passed "accept" header and create
// encoder if available.
?->encode(['some' => 'any'], 200); // Encode payload and create response.
```### Symfony Integration
Add the bundle to your `bundles.php` file:
```php
// bundles.php
return [
// ...
PewPew\HttpFactory\HttpFactoryBundle::class => ['all' => true],
];
```Use `ResponseEncoderFactoryInterface` and `RequestDecoderFactoryInterface`
in your services:```php
use PewPew\HttpFactory\ResponseEncoderFactoryInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;final readonly class ExampleController
{
public function __construct(
private ResponseEncoderFactoryInterface $responses,
) {}
public function someAction(Request $request): Response
{
$encoder = $this->responses->createEncoder($request);
if ($encoder === null) {
throw new \Symfony\Component\HttpFoundation\Exception\BadRequestException(
'Unsupported "accept" request header',
);
}
return $encoder->encode([
'status' => 'ok'
], Response::HTTP_OK);
}
}
```