8000 feature #41161 [HttpClient] Add `DecoratorTrait` to ease writing simp… · symfony/symfony@8ef49cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ef49cf

Browse files
feature #41161 [HttpClient] Add DecoratorTrait to ease writing simple 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 ------- 4682170 [HttpClient] Add `DecoratorTrait` to ease writing simple decorators
2 parents 252ee39 + 4682170 commit 8ef49cf

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

src/Symfony/Component/HttpClient/AsyncDecoratorTrait.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\HttpClient\Response\AsyncResponse;
1515
use Symfony\Component\HttpClient\Response\ResponseStream;
16-
use Symfony\Contracts\HttpClient\HttpClientInterface;
1716
use Symfony\Contracts\HttpClient\ResponseInterface;
1817
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
1918

@@ -24,12 +23,7 @@
2423
*/
2524
trait AsyncDecoratorTrait
2625
{
27-
private $client;
28-
29-
public function __construct(HttpClientInterface $client = null)
30-
{
31-
$this->client = $client ?? HttpClient::create();
32-
}
26+
use DecoratorTrait;
3327

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

5246
return new ResponseStream(AsyncResponse::stream($responses, $timeout, static::class));
5347
}
54-
55-
/**
56-
* {@inheritdoc}
57-
*/
58-
public function withOptions(array $options): self
59-
{
60-
$clone = clone $this;
61-
$clone->client = $this->client->withOptions($options);
62-
63-
return $clone;
64-
}
6548
}

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

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

910
5.2.0
1011
-----
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient;
13+
14+
use Symfony\Contracts\HttpClient\HttpClientInterface;
15+
use Symfony\Contracts\HttpClient\ResponseInterface;
16+
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
17+
18+
/**
19+
* Eases with writing decorators.
20+
*
21+
* @author Nicolas Grekas <p@tchwork.com>
22+
*/
23+
trait DecoratorTrait
24+
{
25+
private $client;
26+
27+
public function __construct(HttpClientInterface $client = null)
28+
{
29+
$this->client = $client ?? HttpClient::create();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function request(string $method, string $url, array $options = []): ResponseInterface
36+
{
37+
return $this->client->request($method, $url, $options);
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function stream($responses, float $timeout = null): ResponseStreamInterface
44+
{
45+
return $this->client->stream($responses, $timeout);
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function withOptions(array $options): self
52+
{
53+
$clone = clone $this;
54+
$clone->client = $this->client->withOptions($options);
55+
56+
return $clone;
57+
}
58+
}

0 commit comments

Comments
 (0)
0