[HttpClient] Add DecoratorTrait
to ease writing simple decorators
This commit is contained in:
parent
90ab9edd47
commit
46821708b0
@ -13,7 +13,6 @@ namespace Symfony\Component\HttpClient;
|
|||||||
|
|
||||||
use Symfony\Component\HttpClient\Response\AsyncResponse;
|
use Symfony\Component\HttpClient\Response\AsyncResponse;
|
||||||
use Symfony\Component\HttpClient\Response\ResponseStream;
|
use Symfony\Component\HttpClient\Response\ResponseStream;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
||||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
|
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
|
||||||
|
|
||||||
@ -24,12 +23,7 @@ use Symfony\Contracts\HttpClient\ResponseStreamInterface;
|
|||||||
*/
|
*/
|
||||||
trait AsyncDecoratorTrait
|
trait AsyncDecoratorTrait
|
||||||
{
|
{
|
||||||
private $client;
|
use DecoratorTrait;
|
||||||
|
|
||||||
public function __construct(HttpClientInterface $client = null)
|
|
||||||
{
|
|
||||||
$this->client = $client ?? HttpClient::create();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
@ -51,15 +45,4 @@ trait AsyncDecoratorTrait
|
|||||||
|
|
||||||
return new ResponseStream(AsyncResponse::stream($responses, $timeout, static::class));
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ CHANGELOG
|
|||||||
---
|
---
|
||||||
|
|
||||||
* Implement `HttpClientInterface::withOptions()` from `symfony/contracts` v2.4
|
* Implement `HttpClientInterface::withOptions()` from `symfony/contracts` v2.4
|
||||||
|
* Add `DecoratorTrait` to ease writing simple decorators
|
||||||
|
|
||||||
5.2.0
|
5.2.0
|
||||||
-----
|
-----
|
||||||
|
58
src/Symfony/Component/HttpClient/DecoratorTrait.php
Normal file
58
src/Symfony/Component/HttpClient/DecoratorTrait.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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 <p@tchwork.com>
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user