From 46821708b025b5762c09f009f443bda7c5abe898 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 10 May 2021 16:29:37 +0200 Subject: [PATCH] [HttpClient] Add `DecoratorTrait` to ease writing simple decorators --- .../HttpClient/AsyncDecoratorTrait.php | 19 +----- src/Symfony/Component/HttpClient/CHANGELOG.md | 1 + .../Component/HttpClient/DecoratorTrait.php | 58 +++++++++++++++++++ 3 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 src/Symfony/Component/HttpClient/DecoratorTrait.php diff --git a/src/Symfony/Component/HttpClient/AsyncDecoratorTrait.php b/src/Symfony/Component/HttpClient/AsyncDecoratorTrait.php index 2e6267300d..aff402d83c 100644 --- a/src/Symfony/Component/HttpClient/AsyncDecoratorTrait.php +++ b/src/Symfony/Component/HttpClient/AsyncDecoratorTrait.php @@ -13,7 +13,6 @@ namespace Symfony\Component\HttpClient; use Symfony\Component\HttpClient\Response\AsyncResponse; use Symfony\Component\HttpClient\Response\ResponseStream; -use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; use Symfony\Contracts\HttpClient\ResponseStreamInterface; @@ -24,12 +23,7 @@ use Symfony\Contracts\HttpClient\ResponseStreamInterface; */ trait AsyncDecoratorTrait { - private $client; - - public function __construct(HttpClientInterface $client = null) - { - $this->client = $client ?? HttpClient::create(); - } + use DecoratorTrait; /** * {@inheritdoc} @@ -51,15 +45,4 @@ trait AsyncDecoratorTrait return new ResponseStream(AsyncResponse::stream($responses, $timeout, static::class)); } - - /** - * {@inheritdoc} - */ - public function withOptions(array $options): self - { - $clone = clone $this; - $clone->client = $this->client->withOptions($options); - - return $clone; - } } diff --git a/src/Symfony/Component/HttpClient/CHANGELOG.md b/src/Symfony/Component/HttpClient/CHANGELOG.md index 3b97488c93..c0e6fc88a4 100644 --- a/src/Symfony/Component/HttpClient/CHANGELOG.md +++ b/src/Symfony/Component/HttpClient/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Implement `HttpClientInterface::withOptions()` from `symfony/contracts` v2.4 + * Add `DecoratorTrait` to ease writing simple decorators 5.2.0 ----- diff --git a/src/Symfony/Component/HttpClient/DecoratorTrait.php b/src/Symfony/Component/HttpClient/DecoratorTrait.php new file mode 100644 index 0000000000..cc5a2feb4d --- /dev/null +++ b/src/Symfony/Component/HttpClient/DecoratorTrait.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpClient; + +use Symfony\Contracts\HttpClient\HttpClientInterface; +use Symfony\Contracts\HttpClient\ResponseInterface; +use Symfony\Contracts\HttpClient\ResponseStreamInterface; + +/** + * Eases with writing decorators. + * + * @author Nicolas Grekas + */ +trait DecoratorTrait +{ + private $client; + + public function __construct(HttpClientInterface $client = null) + { + $this->client = $client ?? HttpClient::create(); + } + + /** + * {@inheritdoc} + */ + public function request(string $method, string $url, array $options = []): ResponseInterface + { + return $this->client->request($method, $url, $options); + } + + /** + * {@inheritdoc} + */ + public function stream($responses, float $timeout = null): ResponseStreamInterface + { + return $this->client->stream($responses, $timeout); + } + + /** + * {@inheritdoc} + */ + public function withOptions(array $options): self + { + $clone = clone $this; + $clone->client = $this->client->withOptions($options); + + return $clone; + } +}