PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/www/system/library/Zend/Service/WindowsAzure/Storage/BatchStorageAbstract.php

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