AsyncDecoratorTrait.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\Component\HttpClient\Response\AsyncResponse;
  12. use Symfony\Component\HttpClient\Response\ResponseStream;
  13. use Symfony\Contracts\HttpClient\ResponseInterface;
  14. use Symfony\Contracts\HttpClient\ResponseStreamInterface;
  15. /**
  16. * Eases with processing responses while streaming them.
  17. *
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. trait AsyncDecoratorTrait
  21. {
  22. use DecoratorTrait;
  23. /**
  24. * {@inheritdoc}
  25. *
  26. * @return AsyncResponse
  27. */
  28. abstract public function request(string $method, string $url, array $options = []): ResponseInterface;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function stream($responses, float $timeout = null): ResponseStreamInterface
  33. {
  34. if ($responses instanceof AsyncResponse) {
  35. $responses = [$responses];
  36. } elseif (!is_iterable($responses)) {
  37. throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of AsyncResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
  38. }
  39. return new ResponseStream(AsyncResponse::stream($responses, $timeout, static::class));
  40. }
  41. }