Skip to content

Commit 2b96257

Browse files
authored
Merge pull request #1 from timoschinkel/initial
Import code and set up repository
2 parents cf304eb + 7f43507 commit 2b96257

File tree

12 files changed

+297
-1
lines changed

12 files changed

+297
-1
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/tests export-ignore
2+
/.gitattributes export-ignore
3+
/.gitignore export-ignore
4+
/phpcs.xml export-ignore
5+
/phpunit.xml.dist export-ignore

.github/workflows/pull_request.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Inspections
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
php-versions: ['7.2', '7.3', '7.4']
11+
name: PHP ${{ matrix.php-versions }}
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Setup PHP
16+
uses: shivammathur/setup-php@v1
17+
with:
18+
php-version: ${{ matrix.php-versions }}
19+
coverage: xdebug
20+
- name: Install dependencies
21+
run: composer install --prefer-dist --no-progress --no-suggest
22+
- name: Unit tests
23+
run: ./vendor/bin/phpunit
24+
- name: Code style
25+
run: ./vendor/bin/phpcs

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## Unreleased
8+
Initial set up.
9+
10+
### Added
11+
- Initial version of code, including unit tests
12+
- README
13+
- CHANGELOG
14+
- CONTRIBUTING.md
15+
- Composer configuration

CONTRIBUTING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Contributing
2+
3+
When contributing to this repository, please first discuss the change you wish to make via issue,
4+
email, or any other method with the owners of this repository before making a change.
5+
6+
This repository is offered as open source and is maintained by volunteers. Please treat the contributors the way you
7+
would like to be treated yourself.
8+
9+
## Pull Request Process
10+
11+
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
12+
build.
13+
2. Ensure the code follows coding style and is tested. If a CI pipeline is set up ensure the Pull Request passes the CI
14+
pipeline.The coding standard we use is [PSR-12](https://www.php-fig.org/psr/psr-12/).
15+
3. Update README.md and other applicable documentation within the repository to reflect the changes in the Pull Request.
16+
4. Update the CHANGELOG.md with a new version number and details of changes to the interface, this includes new environment
17+
variables, exposed ports, useful file locations and container parameters. The versioning scheme we use is
18+
[SemVer](http://semver.org/).
19+
5. You may merge the Pull Request in once you have the sign-off of by a code owner, or if you
20+
do not have permission to do that, you may request a code owner to merge it for you.

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
1-
# php-http-client-middleware
1+
# HTTP Client middleware
2+
3+
HTTP Client middleware is a middleware solution for [PSR-18 (HTTP Client)](http://www.php-fig.org/psr/psr-18).
4+
5+
This package contains both a middleware interface as well as a ready to use HTTP Client that is capable of handling middleware.
6+
7+
**NB** This package does not contain any middleware implementations.
8+
9+
## Rationale
10+
The interface offered by PSR-18 (HTTP Client) does not offer any (configuration) options offered by known HTTP abstractions - for example Guzzle and Symfony HttpClient. This makes implementations of this interface exchangeable, but does require you to write these configurations yourself. This can mean additional code that is repeated in multiple locations. Something that might be undesirable.
11+
12+
This might be solved by using a solution similar to the middleware defined in [PSR-15 (HTTP Server Request Handlers)](https://www.php-fig.org/psr/psr-15). Using middleware will allow centralization of functionality without the necessity of extending or wrapping a client. It also enables you to perform actions both _before_ performing the actual request and _after_ the actual request.
13+
14+
## Installation
15+
16+
Coming soon.
17+
18+
## Usage
19+
Middleware needs to comply to the interface `\Coolblue\Http\Client\MiddlewareInterface`:
20+
21+
```php
22+
<?php
23+
24+
declare(strict_types=1);
25+
26+
namespace Coolblue\Http\Client;
27+
28+
use Psr\Http\Client\ClientInterface;
29+
use Psr\Http\Message\RequestInterface;
30+
use Psr\Http\Message\ResponseInterface;
31+
32+
interface MiddlewareInterface
33+
{
34+
public function process(RequestInterface $request, ClientInterface $client): ResponseInterface;
35+
}
36+
37+
```
38+
39+
To create a middleware enabled client:
40+
41+
```php
42+
43+
$client = new Client(); // an instance of \Psr\Http\Client\ClientInterface
44+
$middlewareOne = new Middleware(); // an instance of \Coolblue\Client\Http\MiddlewareInterface
45+
$middlewareTwo = new Middleware(); // an instance of \Coolblue\Client\Http\MiddlewareInterface
46+
47+
$middlewareClient = new \Coolblue\Http\Client\MiddlewareClient(
48+
$client,
49+
$middlewareOne,
50+
$middlewareTwo
51+
);
52+
```

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "coolblue/http-client-middleware",
3+
"description": "A middleware solution for PSR-18 compliant http clients.",
4+
"type": "library",
5+
"license": "MIT",
6+
"require": {
7+
"php": "^7.2",
8+
"psr/http-client": "^1.0",
9+
"psr/http-message": "^1.0"
10+
},
11+
"require-dev": {
12+
"phpunit/phpunit": "^8.0",
13+
"squizlabs/php_codesniffer": "^3.5"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Coolblue\\Http\\Client\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Coolblue\\Tests\\Http\\Client\\": "tests/"
23+
}
24+
},
25+
"config": {
26+
"sort-packages": true
27+
}
28+
}

phpcs.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PHP_CodeSniffer">
3+
<arg name="basepath" value="."/>
4+
<arg name="colors"/>
5+
<arg value="s"/>
6+
7+
<rule ref="PSR12" />
8+
9+
<file>src/</file>
10+
<file>tests/</file>
11+
</ruleset>

phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
colors="true"
6+
failOnRisky="true"
7+
failOnWarning="true"
8+
verbose="true"
9+
>
10+
<testsuites>
11+
<testsuite name="Http Client Middleware Test Suite">
12+
<directory>./tests</directory>
13+
</testsuite>
14+
</testsuites>
15+
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">src</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

src/MiddlewareClient.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coolblue\Http\Client;
6+
7+
use Psr\Http\Client\ClientInterface;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
final class MiddlewareClient implements ClientInterface
12+
{
13+
/** @var ClientInterface */
14+
private $client;
15+
16+
/** @var MiddlewareInterface[] */
17+
private $middleware;
18+
19+
public function __construct(ClientInterface $client, MiddlewareInterface ...$middleware)
20+
{
21+
$this->client = $client;
22+
$this->middleware = $middleware;
23+
}
24+
25+
public function sendRequest(RequestInterface $request): ResponseInterface
26+
{
27+
$middleware = reset($this->middleware);
28+
if ($middleware instanceof MiddlewareInterface) {
29+
$new = new self($this->client, ...array_slice($this->middleware, 1));
30+
return $middleware->process($request, $new);
31+
}
32+
33+
return $this->client->sendRequest($request);
34+
}
35+
}

0 commit comments

Comments
 (0)