HttpClientsFactory.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright 2016 Facebook, Inc.
  4. *
  5. * You are hereby granted a non-exclusive, worldwide, royalty-free license to
  6. * use, copy, modify, and distribute this software in source code or binary
  7. * form for use in connection with the web services and APIs provided by
  8. * Facebook.
  9. *
  10. * As with any software that integrates with the Facebook platform, your use
  11. * of this software is subject to the Facebook Developer Principles and
  12. * Policies [http://developers.facebook.com/policy/]. This copyright notice
  13. * shall be included in all copies or substantial portions of the software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. namespace Facebook\HttpClients;
  25. use GuzzleHttp\Client;
  26. use InvalidArgumentException;
  27. use Exception;
  28. class HttpClientsFactory
  29. {
  30. private function __construct()
  31. {
  32. // a factory constructor should never be invoked
  33. }
  34. /**
  35. * HTTP client generation.
  36. *
  37. * @param FacebookHttpClientInterface|Client|string|null $handler
  38. *
  39. * @throws Exception If the cURL extension or the Guzzle client aren't available (if required).
  40. * @throws InvalidArgumentException If the http client handler isn't "curl", "stream", "guzzle", or an instance of Facebook\HttpClients\FacebookHttpClientInterface.
  41. *
  42. * @return FacebookHttpClientInterface
  43. */
  44. public static function createHttpClient($handler)
  45. {
  46. if (!$handler) {
  47. return self::detectDefaultClient();
  48. }
  49. if ($handler instanceof FacebookHttpClientInterface) {
  50. return $handler;
  51. }
  52. if ('stream' === $handler) {
  53. return new FacebookStreamHttpClient();
  54. }
  55. if ('curl' === $handler) {
  56. if (!extension_loaded('curl')) {
  57. throw new Exception('The cURL extension must be loaded in order to use the "curl" handler.');
  58. }
  59. return new FacebookCurlHttpClient();
  60. }
  61. if ('guzzle' === $handler && !class_exists('GuzzleHttp\Client')) {
  62. throw new Exception('The Guzzle HTTP client must be included in order to use the "guzzle" handler.');
  63. }
  64. if ($handler instanceof Client) {
  65. return new FacebookGuzzleHttpClient($handler);
  66. }
  67. if ('guzzle' === $handler) {
  68. return new FacebookGuzzleHttpClient();
  69. }
  70. throw new InvalidArgumentException('The http client handler must be set to "curl", "stream", "guzzle", be an instance of GuzzleHttp\Client or an instance of Facebook\HttpClients\FacebookHttpClientInterface');
  71. }
  72. /**
  73. * Detect default HTTP client.
  74. *
  75. * @return FacebookHttpClientInterface
  76. */
  77. private static function detectDefaultClient()
  78. {
  79. if (extension_loaded('curl')) {
  80. return new FacebookCurlHttpClient();
  81. }
  82. if (class_exists('GuzzleHttp\Client')) {
  83. return new FacebookGuzzleHttpClient();
  84. }
  85. return new FacebookStreamHttpClient();
  86. }
  87. }