PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Rest/Client.php

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