|
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_WindowsAzure |
|
17 * @subpackage Storage |
|
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: Batch.php 23167 2010-10-19 17:53:31Z mabe $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Service_WindowsAzure_Exception |
|
25 */ |
|
26 require_once 'Zend/Service/WindowsAzure/Exception.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Service_WindowsAzure_Storage_BatchStorageAbstract |
|
30 */ |
|
31 require_once 'Zend/Service/WindowsAzure/Storage/BatchStorageAbstract.php'; |
|
32 |
|
33 /** |
|
34 * @category Zend |
|
35 * @package Zend_Service_WindowsAzure |
|
36 * @subpackage Storage |
|
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 */ |
|
40 class Zend_Service_WindowsAzure_Storage_Batch |
|
41 { |
|
42 /** |
|
43 * Storage client the batch is defined on |
|
44 * |
|
45 * @var Zend_Service_WindowsAzure_Storage_BatchStorageAbstract |
|
46 */ |
|
47 protected $_storageClient = null; |
|
48 |
|
49 /** |
|
50 * For table storage? |
|
51 * |
|
52 * @var boolean |
|
53 */ |
|
54 protected $_forTableStorage = false; |
|
55 |
|
56 /** |
|
57 * Base URL |
|
58 * |
|
59 * @var string |
|
60 */ |
|
61 protected $_baseUrl; |
|
62 |
|
63 /** |
|
64 * Pending operations |
|
65 * |
|
66 * @var unknown_type |
|
67 */ |
|
68 protected $_operations = array(); |
|
69 |
|
70 /** |
|
71 * Does the batch contain a single select? |
|
72 * |
|
73 * @var boolean |
|
74 */ |
|
75 protected $_isSingleSelect = false; |
|
76 |
|
77 /** |
|
78 * Creates a new Zend_Service_WindowsAzure_Storage_Batch |
|
79 * |
|
80 * @param Zend_Service_WindowsAzure_Storage_BatchStorageAbstract $storageClient Storage client the batch is defined on |
|
81 */ |
|
82 public function __construct(Zend_Service_WindowsAzure_Storage_BatchStorageAbstract $storageClient = null, $baseUrl = '') |
|
83 { |
|
84 $this->_storageClient = $storageClient; |
|
85 $this->_baseUrl = $baseUrl; |
|
86 $this->_beginBatch(); |
|
87 } |
|
88 |
|
89 /** |
|
90 * Get base URL for creating requests |
|
91 * |
|
92 * @return string |
|
93 */ |
|
94 public function getBaseUrl() |
|
95 { |
|
96 return $this->_baseUrl; |
|
97 } |
|
98 |
|
99 /** |
|
100 * Starts a new batch operation set |
|
101 * |
|
102 * @throws Zend_Service_WindowsAzure_Exception |
|
103 */ |
|
104 protected function _beginBatch() |
|
105 { |
|
106 $this->_storageClient->setCurrentBatch($this); |
|
107 } |
|
108 |
|
109 /** |
|
110 * Cleanup current batch |
|
111 */ |
|
112 protected function _clean() |
|
113 { |
|
114 unset($this->_operations); |
|
115 $this->_storageClient->setCurrentBatch(null); |
|
116 $this->_storageClient = null; |
|
117 unset($this); |
|
118 } |
|
119 |
|
120 /** |
|
121 * Enlist operation in current batch |
|
122 * |
|
123 * @param string $path Path |
|
124 * @param string $queryString Query string |
|
125 * @param string $httpVerb HTTP verb the request will use |
|
126 * @param array $headers x-ms headers to add |
|
127 * @param boolean $forTableStorage Is the request for table storage? |
|
128 * @param mixed $rawData Optional RAW HTTP data to be sent over the wire |
|
129 * @throws Zend_Service_WindowsAzure_Exception |
|
130 */ |
|
131 public function enlistOperation($path = '/', $queryString = '', $httpVerb = Zend_Http_Client::GET, $headers = array(), $forTableStorage = false, $rawData = null) |
|
132 { |
|
133 // Set _forTableStorage |
|
134 if ($forTableStorage) { |
|
135 $this->_forTableStorage = true; |
|
136 } |
|
137 |
|
138 // Set _isSingleSelect |
|
139 if ($httpVerb == Zend_Http_Client::GET) { |
|
140 if (count($this->_operations) > 0) { |
|
141 throw new Zend_Service_WindowsAzure_Exception("Select operations can only be performed in an empty batch transaction."); |
|
142 } |
|
143 $this->_isSingleSelect = true; |
|
144 } |
|
145 |
|
146 // Clean path |
|
147 if (strpos($path, '/') !== 0) { |
|
148 $path = '/' . $path; |
|
149 } |
|
150 |
|
151 // Clean headers |
|
152 if ($headers === null) { |
|
153 $headers = array(); |
|
154 } |
|
155 |
|
156 // URL encoding |
|
157 $path = Zend_Service_WindowsAzure_Storage::urlencode($path); |
|
158 $queryString = Zend_Service_WindowsAzure_Storage::urlencode($queryString); |
|
159 |
|
160 // Generate URL |
|
161 $requestUrl = $this->getBaseUrl() . $path . $queryString; |
|
162 |
|
163 // Generate $rawData |
|
164 if ($rawData === null) { |
|
165 $rawData = ''; |
|
166 } |
|
167 |
|
168 // Add headers |
|
169 if ($httpVerb != Zend_Http_Client::GET) { |
|
170 $headers['Content-ID'] = count($this->_operations) + 1; |
|
171 if ($httpVerb != Zend_Http_Client::DELETE) { |
|
172 $headers['Content-Type'] = 'application/atom+xml;type=entry'; |
|
173 } |
|
174 $headers['Content-Length'] = strlen($rawData); |
|
175 } |
|
176 |
|
177 // Generate $operation |
|
178 $operation = ''; |
|
179 $operation .= $httpVerb . ' ' . $requestUrl . ' HTTP/1.1' . "\n"; |
|
180 foreach ($headers as $key => $value) |
|
181 { |
|
182 $operation .= $key . ': ' . $value . "\n"; |
|
183 } |
|
184 $operation .= "\n"; |
|
185 |
|
186 // Add data |
|
187 $operation .= $rawData; |
|
188 |
|
189 // Store operation |
|
190 $this->_operations[] = $operation; |
|
191 } |
|
192 |
|
193 /** |
|
194 * Commit current batch |
|
195 * |
|
196 * @return Zend_Http_Response |
|
197 * @throws Zend_Service_WindowsAzure_Exception |
|
198 */ |
|
199 public function commit() |
|
200 { |
|
201 // Perform batch |
|
202 $response = $this->_storageClient->performBatch($this->_operations, $this->_forTableStorage, $this->_isSingleSelect); |
|
203 |
|
204 // Dispose |
|
205 $this->_clean(); |
|
206 |
|
207 // Parse response |
|
208 $errors = null; |
|
209 preg_match_all('/<message (.*)>(.*)<\/message>/', $response->getBody(), $errors); |
|
210 |
|
211 // Error? |
|
212 if (count($errors[2]) > 0) { |
|
213 throw new Zend_Service_WindowsAzure_Exception('An error has occured while committing a batch: ' . $errors[2][0]); |
|
214 } |
|
215 |
|
216 // Return |
|
217 return $response; |
|
218 } |
|
219 |
|
220 /** |
|
221 * Rollback current batch |
|
222 */ |
|
223 public function rollback() |
|
224 { |
|
225 // Dispose |
|
226 $this->_clean(); |
|
227 } |
|
228 |
|
229 /** |
|
230 * Get operation count |
|
231 * |
|
232 * @return integer |
|
233 */ |
|
234 public function getOperationCount() |
|
235 { |
|
236 return count($this->_operations); |
|
237 } |
|
238 |
|
239 /** |
|
240 * Is single select? |
|
241 * |
|
242 * @return boolean |
|
243 */ |
|
244 public function isSingleSelect() |
|
245 { |
|
246 return $this->_isSingleSelect; |
|
247 } |
|
248 } |