functions.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Yurun\Util\Swoole\Guzzle
  3. {
  4. use GuzzleHttp\DefaultHandler;
  5. use GuzzleHttp\Handler\CurlHandler;
  6. use GuzzleHttp\Handler\CurlMultiHandler;
  7. use GuzzleHttp\Handler\Proxy;
  8. use GuzzleHttp\Handler\StreamHandler;
  9. /**
  10. * Chooses and creates a default handler to use based on the environment.
  11. *
  12. * The returned handler is not wrapped by any default middlewares.
  13. *
  14. * @throws \RuntimeException if no viable Handler is available
  15. *
  16. * @return callable returns the best handler for the given system
  17. */
  18. function choose_handler(): callable
  19. {
  20. $handler = null;
  21. $defaultHandler = DefaultHandler::getDefaultHandler();
  22. if (\is_string($defaultHandler))
  23. {
  24. $handler = new $defaultHandler();
  25. }
  26. elseif (\is_callable($defaultHandler))
  27. {
  28. $handler = $defaultHandler();
  29. }
  30. elseif (\function_exists('curl_multi_exec') && \function_exists('curl_exec'))
  31. {
  32. $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler());
  33. }
  34. elseif (\function_exists('curl_exec'))
  35. {
  36. $handler = new CurlHandler();
  37. }
  38. elseif (\function_exists('curl_multi_exec'))
  39. {
  40. $handler = new CurlMultiHandler();
  41. }
  42. if (ini_get('allow_url_fopen'))
  43. {
  44. $handler = $handler
  45. ? Proxy::wrapStreaming($handler, new StreamHandler())
  46. : new StreamHandler();
  47. }
  48. elseif (!$handler)
  49. {
  50. throw new \RuntimeException('GuzzleHttp requires cURL, the ' . 'allow_url_fopen ini setting, or a custom HTTP handler.');
  51. }
  52. return $handler;
  53. }
  54. }
  55. namespace GuzzleHttp
  56. {
  57. if (!\function_exists('GuzzleHttp\choose_handler') && !method_exists('GuzzleHttp\Utils', 'chooseHandler'))
  58. {
  59. /**
  60. * Chooses and creates a default handler to use based on the environment.
  61. *
  62. * The returned handler is not wrapped by any default middlewares.
  63. *
  64. * @throws \RuntimeException if no viable Handler is available
  65. *
  66. * @return callable returns the best handler for the given system
  67. */
  68. function choose_handler(): callable
  69. {
  70. return \Yurun\Util\Swoole\Guzzle\choose_handler();
  71. }
  72. }
  73. }