-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #41161 [HttpClient] Add
DecoratorTrait
to ease writing simp…
…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
Showing
3 changed files
with
60 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |