|
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_Service |
|
17 * @subpackage Nirvanix |
|
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: Base.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Http_Client |
|
25 */ |
|
26 require_once 'Zend/Http/Client.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Service_Nirvanix_Response |
|
30 */ |
|
31 require_once 'Zend/Service/Nirvanix/Response.php'; |
|
32 |
|
33 /** |
|
34 * The Nirvanix web services are split into namespaces. This is a proxy class |
|
35 * representing one namespace. It allows calls to the namespace to be made by |
|
36 * PHP object calls rather than by having to construct HTTP client requests. |
|
37 * |
|
38 * @category Zend |
|
39 * @package Zend_Service |
|
40 * @subpackage Nirvanix |
|
41 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
42 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
43 */ |
|
44 class Zend_Service_Nirvanix_Namespace_Base |
|
45 { |
|
46 /** |
|
47 * HTTP client instance that will be used to make calls to |
|
48 * the Nirvanix web services. |
|
49 * @var Zend_Http_Client |
|
50 */ |
|
51 protected $_httpClient; |
|
52 |
|
53 /** |
|
54 * Host to use for calls to this Nirvanix namespace. It is possible |
|
55 * that the user will wish to use different hosts for different namespaces. |
|
56 * @var string |
|
57 */ |
|
58 protected $_host = 'http://services.nirvanix.com'; |
|
59 |
|
60 /** |
|
61 * Name of this namespace as used in the URL. |
|
62 * @var string |
|
63 */ |
|
64 protected $_namespace = ''; |
|
65 |
|
66 /** |
|
67 * Defaults for POST parameters. When a request to the service is to be |
|
68 * made, the POST parameters are merged into these. This is a convenience |
|
69 * feature so parameters that are repeatedly required like sessionToken |
|
70 * do not need to be supplied again and again by the user. |
|
71 * |
|
72 * @param array |
|
73 */ |
|
74 protected $_defaults = array(); |
|
75 |
|
76 /** |
|
77 * Class constructor. |
|
78 * |
|
79 * @param $options array Options and dependency injection |
|
80 */ |
|
81 public function __construct($options = array()) |
|
82 { |
|
83 if (isset($options['baseUrl'])) { |
|
84 $this->_host = $options['baseUrl']; |
|
85 } |
|
86 |
|
87 if (isset($options['namespace'])) { |
|
88 $this->_namespace = $options['namespace']; |
|
89 } |
|
90 |
|
91 if (isset($options['defaults'])) { |
|
92 $this->_defaults = $options['defaults']; |
|
93 } |
|
94 |
|
95 if (! isset($options['httpClient'])) { |
|
96 $options['httpClient'] = new Zend_Http_Client(); |
|
97 } |
|
98 $this->_httpClient = $options['httpClient']; |
|
99 } |
|
100 |
|
101 /** |
|
102 * When a method call is made against this proxy, convert it to |
|
103 * an HTTP request to make against the Nirvanix REST service. |
|
104 * |
|
105 * $imfs->DeleteFiles(array('filePath' => 'foo')); |
|
106 * |
|
107 * Assuming this object was proxying the IMFS namespace, the |
|
108 * method call above would call the DeleteFiles command. The |
|
109 * POST parameters would be filePath, merged with the |
|
110 * $this->_defaults (containing the sessionToken). |
|
111 * |
|
112 * @param string $methodName Name of the command to call |
|
113 * on this namespace. |
|
114 * @param array $args Only the first is used and it must be |
|
115 * an array. It contains the POST params. |
|
116 * |
|
117 * @return Zend_Service_Nirvanix_Response |
|
118 */ |
|
119 public function __call($methodName, $args) |
|
120 { |
|
121 $uri = $this->_makeUri($methodName); |
|
122 $this->_httpClient->setUri($uri); |
|
123 |
|
124 if (!isset($args[0]) || !is_array($args[0])) { |
|
125 $args[0] = array(); |
|
126 } |
|
127 |
|
128 $params = array_merge($this->_defaults, $args[0]); |
|
129 $this->_httpClient->resetParameters(); |
|
130 $this->_httpClient->setParameterPost($params); |
|
131 |
|
132 $httpResponse = $this->_httpClient->request(Zend_Http_Client::POST); |
|
133 return $this->_wrapResponse($httpResponse); |
|
134 } |
|
135 |
|
136 /** |
|
137 * Return the HTTP client used for this namespace. This is useful |
|
138 * for inspecting the last request or directly interacting with the |
|
139 * HTTP client. |
|
140 * |
|
141 * @return Zend_Http_Client |
|
142 */ |
|
143 public function getHttpClient() |
|
144 { |
|
145 return $this->_httpClient; |
|
146 } |
|
147 |
|
148 /** |
|
149 * Make a complete URI from an RPC method name. All Nirvanix REST |
|
150 * service URIs use the same format. |
|
151 * |
|
152 * @param string $methodName RPC method name |
|
153 * @return string |
|
154 */ |
|
155 protected function _makeUri($methodName) |
|
156 { |
|
157 $methodName = ucfirst($methodName); |
|
158 return "{$this->_host}/ws/{$this->_namespace}/{$methodName}.ashx"; |
|
159 } |
|
160 |
|
161 /** |
|
162 * All Nirvanix REST service calls return an XML payload. This method |
|
163 * makes a Zend_Service_Nirvanix_Response from that XML payload. |
|
164 * |
|
165 * @param Zend_Http_Response $httpResponse Raw response from Nirvanix |
|
166 * @return Zend_Service_Nirvanix_Response Wrapped response |
|
167 */ |
|
168 protected function _wrapResponse($httpResponse) |
|
169 { |
|
170 return new Zend_Service_Nirvanix_Response($httpResponse->getBody()); |
|
171 } |
|
172 } |