PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Rest/Client/RestClient.php

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