暂无描述

Proxy.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace GuzzleHttp\Handler;
  3. use Psr\Http\Message\RequestInterface;
  4. /**
  5. * Provides basic proxies for handlers.
  6. */
  7. class Proxy
  8. {
  9. /**
  10. * Sends synchronous requests to a specific handler while sending all other
  11. * requests to another handler.
  12. *
  13. * @param callable $default Handler used for normal responses
  14. * @param callable $sync Handler used for synchronous responses.
  15. *
  16. * @return callable Returns the composed handler.
  17. */
  18. public static function wrapSync(
  19. callable $default,
  20. callable $sync
  21. ) {
  22. return function (RequestInterface $request, array $options) use ($default, $sync) {
  23. return empty($options['sync'])
  24. ? $default($request, $options)
  25. : $sync($request, $options);
  26. };
  27. }
  28. /**
  29. * Sends streaming requests to a streaming compatible handler while sending
  30. * all other requests to a default handler.
  31. *
  32. * This, for example, could be useful for taking advantage of the
  33. * performance benefits of curl while still supporting true streaming
  34. * through the StreamHandler.
  35. *
  36. * @param callable $default Handler used for non-streaming responses
  37. * @param callable $streaming Handler used for streaming responses
  38. *
  39. * @return callable Returns the composed handler.
  40. */
  41. public static function wrapStreaming(
  42. callable $default,
  43. callable $streaming
  44. ) {
  45. return function (RequestInterface $request, array $options) use ($default, $streaming) {
  46. return empty($options['stream'])
  47. ? $default($request, $options)
  48. : $streaming($request, $options);
  49. };
  50. }
  51. }