StaticClient.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Guzzle\Http;
  3. use Guzzle\Http\Client;
  4. use Guzzle\Http\ClientInterface;
  5. use Guzzle\Stream\StreamRequestFactoryInterface;
  6. use Guzzle\Stream\PhpStreamRequestFactory;
  7. /**
  8. * Simplified interface to Guzzle that does not require a class to be instantiated
  9. */
  10. final class StaticClient
  11. {
  12. /** @var Client Guzzle client */
  13. private static $client;
  14. /**
  15. * Mount the client to a simpler class name for a specific client
  16. *
  17. * @param string $className Class name to use to mount
  18. * @param ClientInterface $client Client used to send requests
  19. */
  20. public static function mount($className = 'Guzzle', ClientInterface $client = null)
  21. {
  22. class_alias(__CLASS__, $className);
  23. if ($client) {
  24. self::$client = $client;
  25. }
  26. }
  27. /**
  28. * @param string $method HTTP request method (GET, POST, HEAD, DELETE, PUT, etc)
  29. * @param string $url URL of the request
  30. * @param array $options Options to use with the request. See: Guzzle\Http\Message\RequestFactory::applyOptions()
  31. * @return \Guzzle\Http\Message\Response|\Guzzle\Stream\Stream
  32. */
  33. public static function request($method, $url, $options = array())
  34. {
  35. // @codeCoverageIgnoreStart
  36. if (!self::$client) {
  37. self::$client = new Client();
  38. }
  39. // @codeCoverageIgnoreEnd
  40. $request = self::$client->createRequest($method, $url, null, null, $options);
  41. if (isset($options['stream'])) {
  42. if ($options['stream'] instanceof StreamRequestFactoryInterface) {
  43. return $options['stream']->fromRequest($request);
  44. } elseif ($options['stream'] == true) {
  45. $streamFactory = new PhpStreamRequestFactory();
  46. return $streamFactory->fromRequest($request);
  47. }
  48. }
  49. return $request->send();
  50. }
  51. /**
  52. * Send a GET request
  53. *
  54. * @param string $url URL of the request
  55. * @param array $options Array of request options
  56. *
  57. * @return \Guzzle\Http\Message\Response
  58. * @see Guzzle::request for a list of available options
  59. */
  60. public static function get($url, $options = array())
  61. {
  62. return self::request('GET', $url, $options);
  63. }
  64. /**
  65. * Send a HEAD request
  66. *
  67. * @param string $url URL of the request
  68. * @param array $options Array of request options
  69. *
  70. * @return \Guzzle\Http\Message\Response
  71. * @see Guzzle::request for a list of available options
  72. */
  73. public static function head($url, $options = array())
  74. {
  75. return self::request('HEAD', $url, $options);
  76. }
  77. /**
  78. * Send a DELETE request
  79. *
  80. * @param string $url URL of the request
  81. * @param array $options Array of request options
  82. *
  83. * @return \Guzzle\Http\Message\Response
  84. * @see Guzzle::request for a list of available options
  85. */
  86. public static function delete($url, $options = array())
  87. {
  88. return self::request('DELETE', $url, $options);
  89. }
  90. /**
  91. * Send a POST request
  92. *
  93. * @param string $url URL of the request
  94. * @param array $options Array of request options
  95. *
  96. * @return \Guzzle\Http\Message\Response
  97. * @see Guzzle::request for a list of available options
  98. */
  99. public static function post($url, $options = array())
  100. {
  101. return self::request('POST', $url, $options);
  102. }
  103. /**
  104. * Send a PUT request
  105. *
  106. * @param string $url URL of the request
  107. * @param array $options Array of request options
  108. *
  109. * @return \Guzzle\Http\Message\Response
  110. * @see Guzzle::request for a list of available options
  111. */
  112. public static function put($url, $options = array())
  113. {
  114. return self::request('PUT', $url, $options);
  115. }
  116. /**
  117. * Send a PATCH request
  118. *
  119. * @param string $url URL of the request
  120. * @param array $options Array of request options
  121. *
  122. * @return \Guzzle\Http\Message\Response
  123. * @see Guzzle::request for a list of available options
  124. */
  125. public static function patch($url, $options = array())
  126. {
  127. return self::request('PATCH', $url, $options);
  128. }
  129. /**
  130. * Send an OPTIONS request
  131. *
  132. * @param string $url URL of the request
  133. * @param array $options Array of request options
  134. *
  135. * @return \Guzzle\Http\Message\Response
  136. * @see Guzzle::request for a list of available options
  137. */
  138. public static function options($url, $options = array())
  139. {
  140. return self::request('OPTIONS', $url, $options);
  141. }
  142. }