DecoratorTrait.php 1.5 KB

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