~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/ecosystem/uwa-server-original/server/lib/Zend/Rest/Client.php

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

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