|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00026 require_once 'Zend/Service/WindowsAzure/Exception.php'; 00027 00031 require_once 'Zend/Service/WindowsAzure/Storage/BatchStorageAbstract.php'; 00032 00040 class Zend_Service_WindowsAzure_Storage_Batch 00041 { 00047 protected $_storageClient = null; 00048 00054 protected $_forTableStorage = false; 00055 00061 protected $_baseUrl; 00062 00068 protected $_operations = array(); 00069 00075 protected $_isSingleSelect = false; 00076 00082 public function __construct(Zend_Service_WindowsAzure_Storage_BatchStorageAbstract $storageClient = null, $baseUrl = '') 00083 { 00084 $this->_storageClient = $storageClient; 00085 $this->_baseUrl = $baseUrl; 00086 $this->_beginBatch(); 00087 } 00088 00094 public function getBaseUrl() 00095 { 00096 return $this->_baseUrl; 00097 } 00098 00104 protected function _beginBatch() 00105 { 00106 $this->_storageClient->setCurrentBatch($this); 00107 } 00108 00112 protected function _clean() 00113 { 00114 unset($this->_operations); 00115 $this->_storageClient->setCurrentBatch(null); 00116 $this->_storageClient = null; 00117 unset($this); 00118 } 00119 00131 public function enlistOperation($path = '/', $queryString = '', $httpVerb = Zend_Http_Client::GET, $headers = array(), $forTableStorage = false, $rawData = null) 00132 { 00133 // Set _forTableStorage 00134 if ($forTableStorage) { 00135 $this->_forTableStorage = true; 00136 } 00137 00138 // Set _isSingleSelect 00139 if ($httpVerb == Zend_Http_Client::GET) { 00140 if (count($this->_operations) > 0) { 00141 throw new Zend_Service_WindowsAzure_Exception("Select operations can only be performed in an empty batch transaction."); 00142 } 00143 $this->_isSingleSelect = true; 00144 } 00145 00146 // Clean path 00147 if (strpos($path, '/') !== 0) { 00148 $path = '/' . $path; 00149 } 00150 00151 // Clean headers 00152 if (is_null($headers)) { 00153 $headers = array(); 00154 } 00155 00156 // URL encoding 00157 $path = Zend_Service_WindowsAzure_Storage::urlencode($path); 00158 $queryString = Zend_Service_WindowsAzure_Storage::urlencode($queryString); 00159 00160 // Generate URL 00161 $requestUrl = $this->getBaseUrl() . $path . $queryString; 00162 00163 // Generate $rawData 00164 if (is_null($rawData)) { 00165 $rawData = ''; 00166 } 00167 00168 // Add headers 00169 if ($httpVerb != Zend_Http_Client::GET) { 00170 $headers['Content-ID'] = count($this->_operations) + 1; 00171 if ($httpVerb != Zend_Http_Client::DELETE) { 00172 $headers['Content-Type'] = 'application/atom+xml;type=entry'; 00173 } 00174 $headers['Content-Length'] = strlen($rawData); 00175 } 00176 00177 // Generate $operation 00178 $operation = ''; 00179 $operation .= $httpVerb . ' ' . $requestUrl . ' HTTP/1.1' . "\n"; 00180 foreach ($headers as $key => $value) 00181 { 00182 $operation .= $key . ': ' . $value . "\n"; 00183 } 00184 $operation .= "\n"; 00185 00186 // Add data 00187 $operation .= $rawData; 00188 00189 // Store operation 00190 $this->_operations[] = $operation; 00191 } 00192 00199 public function commit() 00200 { 00201 // Perform batch 00202 $response = $this->_storageClient->performBatch($this->_operations, $this->_forTableStorage, $this->_isSingleSelect); 00203 00204 // Dispose 00205 $this->_clean(); 00206 00207 // Parse response 00208 $errors = null; 00209 preg_match_all('/<message (.*)>(.*)<\/message>/', $response->getBody(), $errors); 00210 00211 // Error? 00212 if (count($errors[2]) > 0) { 00213 throw new Zend_Service_WindowsAzure_Exception('An error has occured while committing a batch: ' . $errors[2][0]); 00214 } 00215 00216 // Return 00217 return $response; 00218 } 00219 00223 public function rollback() 00224 { 00225 // Dispose 00226 $this->_clean(); 00227 } 00228 00234 public function getOperationCount() 00235 { 00236 return count($this->_operations); 00237 } 00238 00244 public function isSingleSelect() 00245 { 00246 return $this->_isSingleSelect; 00247 } 00248 }