PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/zend/Zend/Rest/Client.php

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