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

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