Skip to content

Commit

Permalink
feature #41161 [HttpClient] Add DecoratorTrait to ease writing simp…
Browse files Browse the repository at this point in the history
…le decorators (nicolas-grekas)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[HttpClient] Add `DecoratorTrait` to ease writing simple decorators

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Commits
-------

46821708b0 [HttpClient] Add `DecoratorTrait` to ease writing simple decorators
  • Loading branch information
nicolas-grekas committed May 10, 2021
2 parents 530c937 + b2cad06 commit 5573d1d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
19 changes: 1 addition & 18 deletions AsyncDecoratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

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;

Expand All @@ -24,12 +23,7 @@
*/
trait AsyncDecoratorTrait
{
private $client;

public function __construct(HttpClientInterface $client = null)
{
$this->client = $client ?? HttpClient::create();
}
use DecoratorTrait;

/**
* {@inheritdoc}
Expand All @@ -51,15 +45,4 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa

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;
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Implement `HttpClientInterface::withOptions()` from `symfony/contracts` v2.4
* Add `DecoratorTrait` to ease writing simple decorators

5.2.0
-----
Expand Down
58 changes: 58 additions & 0 deletions DecoratorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
*/
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;
}
}

0 comments on commit 5573d1d

Please sign in to comment.