Proxy.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace GuzzleHttp\Handler;
  3. use GuzzleHttp\Promise\PromiseInterface;
  4. use GuzzleHttp\RequestOptions;
  5. use Psr\Http\Message\RequestInterface;
  6. /**
  7. * Provides basic proxies for handlers.
  8. *
  9. * @final
  10. */
  11. class Proxy
  12. {
  13. /**
  14. * Sends synchronous requests to a specific handler while sending all other
  15. * requests to another handler.
  16. *
  17. * @param callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface $default Handler used for normal responses
  18. * @param callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface $sync Handler used for synchronous responses.
  19. *
  20. * @return callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface Returns the composed handler.
  21. */
  22. public static function wrapSync(callable $default, callable $sync): callable
  23. {
  24. return static function (RequestInterface $request, array $options) use ($default, $sync): PromiseInterface {
  25. return empty($options[RequestOptions::SYNCHRONOUS]) ? $default($request, $options) : $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(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface $default Handler used for non-streaming responses
  37. * @param callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface $streaming Handler used for streaming responses
  38. *
  39. * @return callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface Returns the composed handler.
  40. */
  41. public static function wrapStreaming(callable $default, callable $streaming): callable
  42. {
  43. return static function (RequestInterface $request, array $options) use ($default, $streaming): PromiseInterface {
  44. return empty($options['stream']) ? $default($request, $options) : $streaming($request, $options);
  45. };
  46. }
  47. }