|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00026 require_once 'Zend/Service/WindowsAzure/Storage.php'; 00027 00031 require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php'; 00032 00036 require_once 'Zend/Service/WindowsAzure/Exception.php'; 00037 00041 require_once 'Zend/Service/WindowsAzure/Storage/Batch.php'; 00042 00046 require_once 'Zend/Http/Client.php'; 00047 00051 require_once 'Zend/Http/Response.php'; 00052 00060 abstract class Zend_Service_WindowsAzure_Storage_BatchStorageAbstract 00061 extends Zend_Service_WindowsAzure_Storage 00062 { 00068 protected $_currentBatch = null; 00069 00076 public function setCurrentBatch(Zend_Service_WindowsAzure_Storage_Batch $batch = null) 00077 { 00078 if (!is_null($batch) && $this->isInBatch()) { 00079 throw new Zend_Service_WindowsAzure_Exception('Only one batch can be active at a time.'); 00080 } 00081 $this->_currentBatch = $batch; 00082 } 00083 00089 public function getCurrentBatch() 00090 { 00091 return $this->_currentBatch; 00092 } 00093 00099 public function isInBatch() 00100 { 00101 return !is_null($this->_currentBatch); 00102 } 00103 00110 public function startBatch() 00111 { 00112 return new Zend_Service_WindowsAzure_Storage_Batch($this, $this->getBaseUrl()); 00113 } 00114 00125 public function performBatch($operations = array(), $forTableStorage = false, $isSingleSelect = false, $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN, $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ) 00126 { 00127 // Generate boundaries 00128 $batchBoundary = 'batch_' . md5(time() . microtime()); 00129 $changesetBoundary = 'changeset_' . md5(time() . microtime()); 00130 00131 // Set headers 00132 $headers = array(); 00133 00134 // Add version header 00135 $headers['x-ms-version'] = $this->_apiVersion; 00136 00137 // Add content-type header 00138 $headers['Content-Type'] = 'multipart/mixed; boundary=' . $batchBoundary; 00139 00140 // Set path and query string 00141 $path = '/$batch'; 00142 $queryString = ''; 00143 00144 // Set verb 00145 $httpVerb = Zend_Http_Client::POST; 00146 00147 // Generate raw data 00148 $rawData = ''; 00149 00150 // Single select? 00151 if ($isSingleSelect) { 00152 $operation = $operations[0]; 00153 $rawData .= '--' . $batchBoundary . "\n"; 00154 $rawData .= 'Content-Type: application/http' . "\n"; 00155 $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n"; 00156 $rawData .= $operation; 00157 $rawData .= '--' . $batchBoundary . '--'; 00158 } else { 00159 $rawData .= '--' . $batchBoundary . "\n"; 00160 $rawData .= 'Content-Type: multipart/mixed; boundary=' . $changesetBoundary . "\n\n"; 00161 00162 // Add operations 00163 foreach ($operations as $operation) 00164 { 00165 $rawData .= '--' . $changesetBoundary . "\n"; 00166 $rawData .= 'Content-Type: application/http' . "\n"; 00167 $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n"; 00168 $rawData .= $operation; 00169 } 00170 $rawData .= '--' . $changesetBoundary . '--' . "\n"; 00171 00172 $rawData .= '--' . $batchBoundary . '--'; 00173 } 00174 00175 // Generate URL and sign request 00176 $requestUrl = $this->_credentials->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission); 00177 $requestHeaders = $this->_credentials->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission); 00178 00179 // Prepare request 00180 $this->_httpClientChannel->resetParameters(true); 00181 $this->_httpClientChannel->setUri($requestUrl); 00182 $this->_httpClientChannel->setHeaders($requestHeaders); 00183 $this->_httpClientChannel->setRawData($rawData); 00184 00185 // Execute request 00186 $response = $this->_retryPolicy->execute( 00187 array($this->_httpClientChannel, 'request'), 00188 array($httpVerb) 00189 ); 00190 00191 return $response; 00192 } 00193 }