/libraries/Zend/Rest/Client/RestClient.php

https://github.com/kiranatama/sagalaya · PHP · 287 lines · 122 code · 24 blank · 141 comment · 20 complexity · b6829b2dce36d136426b9a5b52cd09bb MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Rest
  17. * @subpackage Client
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. namespace Zend\Rest\Client;
  22. use Zend\Http\Client as HttpClient,
  23. Zend\Uri;
  24. /**
  25. * @category Zend
  26. * @package Zend_Rest
  27. * @subpackage Client
  28. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class RestClient extends \Zend\Service\AbstractService
  32. {
  33. /**
  34. * Data for the query
  35. * @var array
  36. */
  37. protected $data = array();
  38. /**
  39. * URI of this web service
  40. * @var Uri\Uri
  41. */
  42. protected $uri = null;
  43. /**
  44. * @var HttpClient
  45. */
  46. protected $httpClient;
  47. /**
  48. * Constructor
  49. *
  50. * @param string|Uri\Uri $uri URI for the web service
  51. * @return void
  52. */
  53. public function __construct($uri = null)
  54. {
  55. if (!empty($uri)) {
  56. $this->setUri($uri);
  57. }
  58. }
  59. /**
  60. * Set HTTP client instance to use with this service instance
  61. *
  62. * @param HttpClient $client
  63. * @return RestClient
  64. */
  65. public function setHttpClient(HttpClient $client)
  66. {
  67. $this->httpClient = $client;
  68. return $this;
  69. }
  70. /**
  71. * Get the HTTP client instance registered with this service instance
  72. *
  73. * If none set, will check for a default instance.
  74. *
  75. * @return HttpClient
  76. */
  77. public function getHttpClient()
  78. {
  79. if (null === $this->httpClient) {
  80. $this->setHttpClient(new HttpClient());
  81. }
  82. return $this->httpClient;
  83. }
  84. /**
  85. * Set the URI to use in the request
  86. *
  87. * @param string|Uri\Uri $uri URI for the web service
  88. * @return RestClient
  89. */
  90. public function setUri($uri)
  91. {
  92. if ($uri instanceof Uri\Uri) {
  93. $this->uri = $uri;
  94. } else {
  95. $this->uri = Uri\UriFactory::factory($uri);
  96. }
  97. return $this;
  98. }
  99. /**
  100. * Retrieve the current request URI object
  101. *
  102. * @return Uri\Uri
  103. */
  104. public function getUri()
  105. {
  106. return $this->uri;
  107. }
  108. /**
  109. * Call a remote REST web service URI and return the Zend_Http_Response object
  110. *
  111. * @param string $path The path to append to the URI
  112. * @throws Exception\UnexpectedValueException
  113. * @return void
  114. */
  115. protected function prepareRest($path)
  116. {
  117. // Get the URI object and configure it
  118. if (!$this->uri instanceof Uri\Uri) {
  119. throw new Exception\UnexpectedValueException('URI object must be set before performing call');
  120. }
  121. $uri = $this->uri->toString();
  122. if ($path[0] != '/' && $uri[strlen($uri)-1] != '/') {
  123. $path = '/' . $path;
  124. }
  125. $this->uri->setPath($path);
  126. /**
  127. * Get the HTTP client and configure it for the endpoint URI. Do this
  128. * each time as the Zend\Http\Client instance may be shared with other
  129. * Zend\Service\AbstractService subclasses.
  130. */
  131. $client = $this->getHttpClient();
  132. $client->resetParameters();
  133. $client->setUri($this->uri);
  134. }
  135. /**
  136. * Performs an HTTP GET request to the $path.
  137. *
  138. * @param string $path
  139. * @param array $query Array of GET parameters
  140. * @return Zend\Http\Response
  141. */
  142. public function restGet($path, array $query = null)
  143. {
  144. $this->prepareRest($path);
  145. $client = $this->getHttpClient();
  146. if (is_array($query)) {
  147. $client->setParameterGet($query);
  148. }
  149. return $client->setMethod('GET')->send();
  150. }
  151. /**
  152. * Perform a POST or PUT
  153. *
  154. * Performs a POST or PUT request. Any data provided is set in the HTTP
  155. * client. String data is pushed in as raw POST data; array or object data
  156. * is pushed in as POST parameters.
  157. *
  158. * @param mixed $method
  159. * @param mixed $data
  160. * @return \Zend\Http\Response
  161. */
  162. protected function performPost($method, $data = null)
  163. {
  164. $client = $this->getHttpClient();
  165. $client->setMethod($method);
  166. $request = $client->getRequest();
  167. if (is_string($data)) {
  168. $request->setContent($data);
  169. } elseif (is_array($data) || is_object($data)) {
  170. $request->post()->fromArray((array) $data);
  171. }
  172. return $client->send($request);
  173. }
  174. /**
  175. * Performs an HTTP POST request to $path.
  176. *
  177. * @param string $path
  178. * @param mixed $data Raw data to send
  179. * @return \Zend\Http\Response
  180. */
  181. public function restPost($path, $data = null)
  182. {
  183. $this->prepareRest($path);
  184. return $this->performPost('POST', $data);
  185. }
  186. /**
  187. * Performs an HTTP PUT request to $path.
  188. *
  189. * @param string $path
  190. * @param mixed $data Raw data to send in request
  191. * @return \Zend\Http\Response
  192. */
  193. public function restPut($path, $data = null)
  194. {
  195. $this->prepareRest($path);
  196. return $this->performPost('PUT', $data);
  197. }
  198. /**
  199. * Performs an HTTP DELETE request to $path.
  200. *
  201. * @param string $path
  202. * @return \Zend\Http\Response
  203. */
  204. public function restDelete($path)
  205. {
  206. $this->prepareRest($path);
  207. return $this->getHttpClient()->setMethod('DELETE')->send();
  208. }
  209. /**
  210. * Method call overload
  211. *
  212. * Allows calling REST actions as object methods; however, you must
  213. * follow-up by chaining the request with a request to an HTTP request
  214. * method (post, get, delete, put):
  215. * <code>
  216. * $response = $rest->sayHello('Foo', 'Manchu')->get();
  217. * </code>
  218. *
  219. * Or use them together, but in sequential calls:
  220. * <code>
  221. * $rest->sayHello('Foo', 'Manchu');
  222. * $response = $rest->get();
  223. * </code>
  224. *
  225. * @param string $method Method name
  226. * @param array $args Method args
  227. * @return \Zend\Rest\Client\RestClient_Result|\Zend\Rest\Client\RestClient \Zend\Rest\Client\RestClient if using
  228. * a remote method, Zend_Rest_Client_Result if using an HTTP request method
  229. */
  230. public function __call($method, $args)
  231. {
  232. $methods = array('post', 'get', 'delete', 'put');
  233. if (in_array(strtolower($method), $methods)) {
  234. if (!isset($args[0])) {
  235. $args[0] = $this->uri->getPath();
  236. }
  237. $this->data['rest'] = 1;
  238. $data = array_slice($args, 1) + $this->data;
  239. $response = $this->{'rest' . $method}($args[0], $data);
  240. $this->data = array(); //Initializes for next Rest method.
  241. return new Result($response->getBody());
  242. } else {
  243. // More than one arg means it's definitely a Zend_Rest_Server
  244. if (count($args) == 1) {
  245. // Uses first called function name as method name
  246. if (!isset($this->data['method'])) {
  247. $this->data['method'] = $method;
  248. $this->data['arg1'] = $args[0];
  249. }
  250. $this->data[$method] = $args[0];
  251. } else {
  252. $this->data['method'] = $method;
  253. if (count($args) > 0) {
  254. foreach ($args as $key => $arg) {
  255. $key = 'arg' . $key;
  256. $this->data[$key] = $arg;
  257. }
  258. }
  259. }
  260. return $this;
  261. }
  262. }
  263. }