Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Mar 17, 2024
0 parents commit 3c51e6c
Show file tree
Hide file tree
Showing 29 changed files with 1,071 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml,neon}]
indent_size = 2
15 changes: 15 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
* text=auto eol=lf
*.pp eol=lf linguist-language=EBNF
*.pp2 eol=lf linguist-language=EBNF

.editorconfig export-ignore
.php-cs-fixer.php export-ignore
.gitattributes export-ignore
.gitignore export-ignore

phpunit.xml export-ignore
psalm.xml export-ignore
rector.php export-ignore

/.github export-ignore
/tests export-ignore
45 changes: 45 additions & 0 deletions .github/workflows/codestyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: codestyle

on:
push:
pull_request:

jobs:
psalm:
name: Code Style
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
php: [ '8.3' ]
os: [ ubuntu-latest ]
steps:
- name: Set Git To Use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- name: Validate Composer
run: composer validate
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Restore Composer Cache
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
- name: Install Dependencies
uses: nick-invision/retry@v2
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --prefer-dist --no-interaction --no-progress
- name: Check Code Style
run: composer phpcs:check
49 changes: 49 additions & 0 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: security

on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'

jobs:
security:
name: Security
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
php: [ '8.3' ]
os: [ ubuntu-latest ]
steps:
- name: Set Git To Use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- name: Validate Composer
run: composer validate
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Restore Composer Cache
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
- name: Install Dependencies
uses: nick-invision/retry@v2
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --prefer-dist --no-interaction --no-progress
- name: Composer Audit
run: composer audit
- name: Security Advisories
run: composer require --dev roave/security-advisories:dev-latest
45 changes: 45 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: static-analysis

on:
push:
pull_request:

jobs:
psalm:
name: Psalm
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
php: [ '8.3' ]
os: [ ubuntu-latest ]
steps:
- name: Set Git To Use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- name: Validate Composer
run: composer validate
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Restore Composer Cache
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
- name: Install Dependencies
uses: nick-invision/retry@v2
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --prefer-dist --no-interaction --no-progress
- name: Static Analysis
run: composer linter:check
52 changes: 52 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: tests

on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'

jobs:
tests:
name: Tests (${{matrix.php}}, ${{ matrix.os }}, ${{ matrix.stability }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
php: [ '8.3' ]
os: [ ubuntu-latest, macos-latest, windows-latest ]
stability: [ prefer-lowest, prefer-stable ]
steps:
- name: Set Git To Use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: pecl
ini-values: "memory_limit=-1"
- name: Validate Composer
run: composer validate
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Restore Composer Cache
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
- name: Install Dependencies
uses: nick-invision/retry@v2
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --${{ matrix.stability }} --ignore-platform-reqs --prefer-dist --no-interaction --no-progress
- name: Execute Unit Tests
run: composer test:unit
- name: Execute Functional Tests
run: composer test:functional
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# IDE
/.idea/

# Composer
/composer.lock
/vendor/

# Testing
test.php
16 changes: 16 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$files = PhpCsFixer\Finder::create()
->in([__DIR__ . '/src']);

return (new PhpCsFixer\Config())
->setRules([
'@PER-CS2.0' => true,
'@PER-CS2.0:risky' => true,
'strict_param' => true,
'array_syntax' => [
'syntax' => 'short',
],
])
->setCacheFile(__DIR__ . '/vendor/.cache.php-cs-fixer')
->setFinder($files);
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Nesmeyanov Kirill

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<p align="center">
<a href="https://github.com/pew-pew-team"><img src="https://raw.githubusercontent.com/pew-pew-team/.github/master/assets/logo.svg" width="128" height="128" /></a>
</p>

<p align="center">
<a href="https://packagist.org/packages/pew-pew/http-factory"><img src="https://poser.pugx.org/pew-pew/http-factory/require/php?style=for-the-badge" alt="PHP 8.3+"></a>
<a href="https://packagist.org/packages/pew-pew/http-factory"><img src="https://poser.pugx.org/pew-pew/http-factory/version?style=for-the-badge" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/pew-pew/http-factory"><img src="https://poser.pugx.org/pew-pew/http-factory/v/unstable?style=for-the-badge" alt="Latest Unstable Version"></a>
<a href="https://raw.githubusercontent.com/pew-pew-team/http-factory/blob/master/LICENSE"><img src="https://poser.pugx.org/pew-pew/http-factory/license?style=for-the-badge" alt="License MIT"></a>
</p>
<p align="center">
<a href="https://github.com/pew-pew-team/http-factory/actions"><img src="https://github.com/pew-pew-team/http-factory/workflows/tests/badge.svg"></a>
<a href="https://github.com/pew-pew-team/http-factory/actions"><img src="https://github.com/pew-pew-team/http-factory/workflows/codestyle/badge.svg"></a>
<a href="https://github.com/pew-pew-team/http-factory/actions"><img src="https://github.com/pew-pew-team/http-factory/workflows/security/badge.svg"></a>
<a href="https://github.com/pew-pew-team/http-factory/actions"><img src="https://github.com/pew-pew-team/http-factory/workflows/static-analysis/badge.svg"></a>
</p>

# 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);
}
}
```
Loading

0 comments on commit 3c51e6c

Please sign in to comment.