PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/frapi/library/Zend/Service/WindowsAzure/Storage/BatchStorageAbstract.php

http://github.com/frapi/frapi
PHP | 193 lines | 68 code | 25 blank | 100 comment | 4 complexity | fbf4d498578109df3df909d8231bd01f MD5 | raw file
Possible License(s): BSD-2-Clause
  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: Storage.php 21617 2009-06-12 10:46:31Z unknown $
  21. */
  22. /**
  23. * @see Zend_Service_WindowsAzure_Storage
  24. */
  25. // require_once 'Zend/Service/WindowsAzure/Storage.php';
  26. /**
  27. * @see Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  28. */
  29. // require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php';
  30. /**
  31. * @see Zend_Service_WindowsAzure_Exception
  32. */
  33. // require_once 'Zend/Service/WindowsAzure/Exception.php';
  34. /**
  35. * @see Zend_Service_WindowsAzure_Storage_Batch
  36. */
  37. // require_once 'Zend/Service/WindowsAzure/Storage/Batch.php';
  38. /**
  39. * @see Zend_Http_Client
  40. */
  41. // require_once 'Zend/Http/Client.php';
  42. /**
  43. * @see Zend_Http_Response
  44. */
  45. // require_once 'Zend/Http/Response.php';
  46. /**
  47. * @category Zend
  48. * @package Zend_Service_WindowsAzure
  49. * @subpackage Storage
  50. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  51. * @license http://framework.zend.com/license/new-bsd New BSD License
  52. */
  53. abstract class Zend_Service_WindowsAzure_Storage_BatchStorageAbstract
  54. extends Zend_Service_WindowsAzure_Storage
  55. {
  56. /**
  57. * Current batch
  58. *
  59. * @var Zend_Service_WindowsAzure_Storage_Batch
  60. */
  61. protected $_currentBatch = null;
  62. /**
  63. * Set current batch
  64. *
  65. * @param Zend_Service_WindowsAzure_Storage_Batch $batch Current batch
  66. * @throws Zend_Service_WindowsAzure_Exception
  67. */
  68. public function setCurrentBatch(Zend_Service_WindowsAzure_Storage_Batch $batch = null)
  69. {
  70. if (!is_null($batch) && $this->isInBatch()) {
  71. throw new Zend_Service_WindowsAzure_Exception('Only one batch can be active at a time.');
  72. }
  73. $this->_currentBatch = $batch;
  74. }
  75. /**
  76. * Get current batch
  77. *
  78. * @return Zend_Service_WindowsAzure_Storage_Batch
  79. */
  80. public function getCurrentBatch()
  81. {
  82. return $this->_currentBatch;
  83. }
  84. /**
  85. * Is there a current batch?
  86. *
  87. * @return boolean
  88. */
  89. public function isInBatch()
  90. {
  91. return !is_null($this->_currentBatch);
  92. }
  93. /**
  94. * Starts a new batch operation set
  95. *
  96. * @return Zend_Service_WindowsAzure_Storage_Batch
  97. * @throws Zend_Service_WindowsAzure_Exception
  98. */
  99. public function startBatch()
  100. {
  101. return new Zend_Service_WindowsAzure_Storage_Batch($this, $this->getBaseUrl());
  102. }
  103. /**
  104. * Perform batch using Zend_Http_Client channel, combining all batch operations into one request
  105. *
  106. * @param array $operations Operations in batch
  107. * @param boolean $forTableStorage Is the request for table storage?
  108. * @param boolean $isSingleSelect Is the request a single select statement?
  109. * @param string $resourceType Resource type
  110. * @param string $requiredPermission Required permission
  111. * @return Zend_Http_Response
  112. */
  113. public function performBatch($operations = array(), $forTableStorage = false, $isSingleSelect = false, $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN, $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ)
  114. {
  115. // Generate boundaries
  116. $batchBoundary = 'batch_' . md5(time() . microtime());
  117. $changesetBoundary = 'changeset_' . md5(time() . microtime());
  118. // Set headers
  119. $headers = array();
  120. // Add version header
  121. $headers['x-ms-version'] = $this->_apiVersion;
  122. // Add content-type header
  123. $headers['Content-Type'] = 'multipart/mixed; boundary=' . $batchBoundary;
  124. // Set path and query string
  125. $path = '/$batch';
  126. $queryString = '';
  127. // Set verb
  128. $httpVerb = Zend_Http_Client::POST;
  129. // Generate raw data
  130. $rawData = '';
  131. // Single select?
  132. if ($isSingleSelect) {
  133. $operation = $operations[0];
  134. $rawData .= '--' . $batchBoundary . "\n";
  135. $rawData .= 'Content-Type: application/http' . "\n";
  136. $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
  137. $rawData .= $operation;
  138. $rawData .= '--' . $batchBoundary . '--';
  139. } else {
  140. $rawData .= '--' . $batchBoundary . "\n";
  141. $rawData .= 'Content-Type: multipart/mixed; boundary=' . $changesetBoundary . "\n\n";
  142. // Add operations
  143. foreach ($operations as $operation)
  144. {
  145. $rawData .= '--' . $changesetBoundary . "\n";
  146. $rawData .= 'Content-Type: application/http' . "\n";
  147. $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
  148. $rawData .= $operation;
  149. }
  150. $rawData .= '--' . $changesetBoundary . '--' . "\n";
  151. $rawData .= '--' . $batchBoundary . '--';
  152. }
  153. // Generate URL and sign request
  154. $requestUrl = $this->_credentials->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission);
  155. $requestHeaders = $this->_credentials->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission);
  156. // Prepare request
  157. $this->_httpClientChannel->resetParameters(true);
  158. $this->_httpClientChannel->setUri($requestUrl);
  159. $this->_httpClientChannel->setHeaders($requestHeaders);
  160. $this->_httpClientChannel->setRawData($rawData);
  161. // Execute request
  162. $response = $this->_retryPolicy->execute(
  163. array($this->_httpClientChannel, 'request'),
  164. array($httpVerb)
  165. );
  166. return $response;
  167. }
  168. }