PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/Storage/BatchStorageAbstract.php

https://bitbucket.org/ktos/tinyshare
PHP | 184 lines | 70 code | 21 blank | 93 comment | 4 complexity | b669dfbe040710165d58f75917454d3c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2011, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @subpackage Storage
  31. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  32. * @license http://phpazure.codeplex.com/license
  33. * @version $Id: Storage.php 21617 2009-06-12 10:46:31Z unknown $
  34. */
  35. /**
  36. * @see Microsoft_AutoLoader
  37. */
  38. require_once dirname(__FILE__) . '/../../AutoLoader.php';
  39. /**
  40. * @category Microsoft
  41. * @package Microsoft_WindowsAzure
  42. * @subpackage Storage
  43. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  44. * @license http://phpazure.codeplex.com/license
  45. */
  46. abstract class Microsoft_WindowsAzure_Storage_BatchStorageAbstract
  47. extends Microsoft_WindowsAzure_Storage
  48. {
  49. /**
  50. * Current batch
  51. *
  52. * @var Microsoft_WindowsAzure_Storage_Batch
  53. */
  54. protected $_currentBatch = null;
  55. /**
  56. * Set current batch
  57. *
  58. * @param Microsoft_WindowsAzure_Storage_Batch $batch Current batch
  59. * @throws Microsoft_WindowsAzure_Exception
  60. */
  61. public function setCurrentBatch(Microsoft_WindowsAzure_Storage_Batch $batch = null)
  62. {
  63. if (!is_null($batch) && $this->isInBatch()) {
  64. throw new Microsoft_WindowsAzure_Exception('Only one batch can be active at a time.');
  65. }
  66. $this->_currentBatch = $batch;
  67. }
  68. /**
  69. * Get current batch
  70. *
  71. * @return Microsoft_WindowsAzure_Storage_Batch
  72. */
  73. public function getCurrentBatch()
  74. {
  75. return $this->_currentBatch;
  76. }
  77. /**
  78. * Is there a current batch?
  79. *
  80. * @return boolean
  81. */
  82. public function isInBatch()
  83. {
  84. return !is_null($this->_currentBatch);
  85. }
  86. /**
  87. * Starts a new batch operation set
  88. *
  89. * @return Microsoft_WindowsAzure_Storage_Batch
  90. * @throws Microsoft_WindowsAzure_Exception
  91. */
  92. public function startBatch()
  93. {
  94. return new Microsoft_WindowsAzure_Storage_Batch($this, $this->getBaseUrl());
  95. }
  96. /**
  97. * Perform batch using Microsoft_Http_Client channel, combining all batch operations into one request
  98. *
  99. * @param array $operations Operations in batch
  100. * @param boolean $forTableStorage Is the request for table storage?
  101. * @param boolean $isSingleSelect Is the request a single select statement?
  102. * @param string $resourceType Resource type
  103. * @param string $requiredPermission Required permission
  104. * @return Microsoft_Http_Response
  105. */
  106. public function performBatch($operations = array(), $forTableStorage = false, $isSingleSelect = false, $resourceType = Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN, $requiredPermission = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ)
  107. {
  108. // Generate boundaries
  109. $batchBoundary = 'batch_' . md5(time() . microtime());
  110. $changesetBoundary = 'changeset_' . md5(time() . microtime());
  111. // Set headers
  112. $headers = array();
  113. // Add version header
  114. $headers['x-ms-version'] = $this->_apiVersion;
  115. // Add dataservice headers
  116. $headers['DataServiceVersion'] = '1.0;NetFx';
  117. $headers['MaxDataServiceVersion'] = '1.0;NetFx';
  118. // Add content-type header
  119. $headers['Content-Type'] = 'multipart/mixed; boundary=' . $batchBoundary;
  120. // Set path
  121. $path = '/$batch';
  122. // Set verb
  123. $httpVerb = Microsoft_Http_Client::POST;
  124. // Generate raw data
  125. $rawData = '';
  126. // Single select?
  127. if ($isSingleSelect) {
  128. $operation = $operations[0];
  129. $rawData .= '--' . $batchBoundary . "\n";
  130. $rawData .= 'Content-Type: application/http' . "\n";
  131. $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
  132. $rawData .= $operation;
  133. $rawData .= '--' . $batchBoundary . '--';
  134. } else {
  135. $rawData .= '--' . $batchBoundary . "\n";
  136. $rawData .= 'Content-Type: multipart/mixed; boundary=' . $changesetBoundary . "\n\n";
  137. // Add operations
  138. foreach ($operations as $operation)
  139. {
  140. $rawData .= '--' . $changesetBoundary . "\n";
  141. $rawData .= 'Content-Type: application/http' . "\n";
  142. $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
  143. $rawData .= $operation;
  144. }
  145. $rawData .= '--' . $changesetBoundary . '--' . "\n";
  146. $rawData .= '--' . $batchBoundary . '--';
  147. }
  148. // Generate URL and sign request
  149. $requestUrl = $this->_credentials->signRequestUrl($this->getBaseUrl() . $path, $resourceType, $requiredPermission);
  150. $requestHeaders = $this->_credentials->signRequestHeaders($httpVerb, $path, array(), $headers, $forTableStorage, $resourceType, $requiredPermission);
  151. // Prepare request
  152. $this->_httpClientChannel->resetParameters(true);
  153. $this->_httpClientChannel->setUri($requestUrl);
  154. $this->_httpClientChannel->setHeaders($requestHeaders);
  155. $this->_httpClientChannel->setRawData($rawData);
  156. // Execute request
  157. $response = $this->_retryPolicy->execute(
  158. array($this->_httpClientChannel, 'request'),
  159. array($httpVerb)
  160. );
  161. return $response;
  162. }
  163. }