├── .github └── workflows │ └── pull_request.yml ├── CHANGELOG.md ├── CODEOWNERS ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── composer.json └── src ├── MiddlewareClient.php └── MiddlewareInterface.php /.github/workflows/pull_request.yml: -------------------------------------------------------------------------------- 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: ['8.0', '8.1', '8.2', '8.3', '8.4'] 11 | name: PHP ${{ matrix.php-versions }} 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Setup PHP 16 | uses: shivammathur/setup-php@v2 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 26 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 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 | ## [1.2.0] - 2023-12-20 8 | ### Removed 9 | - Support for PHP versions < 8.0 10 | 11 | ### Changed 12 | - Dependency on `psr/http-message` now is `^1.0 || ^2.0` 13 | 14 | ## [1.1.0] - 2020-11-26 15 | ### Added 16 | - Support for PHP 8 17 | 18 | ### Changed 19 | - Updated README with link to Packagist 20 | 21 | ## [1.0.0] - 2020-02-25 22 | Initial set up. 23 | 24 | ### Added 25 | - Initial version of code, including unit tests 26 | - README 27 | - CHANGELOG 28 | - CONTRIBUTING.md 29 | - Composer configuration 30 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Each line is a file pattern followed by one or more owners. 2 | # These owners will be the default owners for everything in 3 | # the repo. 4 | 5 | * @coolblue/php-http-client-middleware 6 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 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. 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Coolblue 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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 | ```bash 17 | composer require coolblue/http-client-middleware 18 | ``` 19 | 20 | ## Usage 21 | Middleware needs to comply to the interface `\Coolblue\Http\Client\MiddlewareInterface`: 22 | 23 | ```php 24 | 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 | } 36 | -------------------------------------------------------------------------------- /src/MiddlewareInterface.php: -------------------------------------------------------------------------------- 1 |