PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/Storage/Batch.php

https://bitbucket.org/ktos/tinyshare
PHP | 259 lines | 99 code | 28 blank | 132 comment | 13 complexity | b3eb520aedc162cea61c9975416303af 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. class Microsoft_WindowsAzure_Storage_Batch
  47. {
  48. /**
  49. * Storage client the batch is defined on
  50. *
  51. * @var Microsoft_WindowsAzure_Storage_BatchStorageAbstract
  52. */
  53. protected $_storageClient = null;
  54. /**
  55. * For table storage?
  56. *
  57. * @var boolean
  58. */
  59. protected $_forTableStorage = false;
  60. /**
  61. * Base URL
  62. *
  63. * @var string
  64. */
  65. protected $_baseUrl;
  66. /**
  67. * Pending operations
  68. *
  69. * @var unknown_type
  70. */
  71. protected $_operations = array();
  72. /**
  73. * Does the batch contain a single select?
  74. *
  75. * @var boolean
  76. */
  77. protected $_isSingleSelect = false;
  78. /**
  79. * Creates a new Microsoft_WindowsAzure_Storage_Batch
  80. *
  81. * @param Microsoft_WindowsAzure_Storage_BatchStorageAbstract $storageClient Storage client the batch is defined on
  82. */
  83. public function __construct(Microsoft_WindowsAzure_Storage_BatchStorageAbstract $storageClient = null, $baseUrl = '')
  84. {
  85. $this->_storageClient = $storageClient;
  86. $this->_baseUrl = $baseUrl;
  87. $this->_beginBatch();
  88. }
  89. /**
  90. * Get base URL for creating requests
  91. *
  92. * @return string
  93. */
  94. public function getBaseUrl()
  95. {
  96. return $this->_baseUrl;
  97. }
  98. /**
  99. * Starts a new batch operation set
  100. *
  101. * @throws Microsoft_WindowsAzure_Exception
  102. */
  103. protected function _beginBatch()
  104. {
  105. $this->_storageClient->setCurrentBatch($this);
  106. }
  107. /**
  108. * Cleanup current batch
  109. */
  110. protected function _clean()
  111. {
  112. unset($this->_operations);
  113. $this->_storageClient->setCurrentBatch(null);
  114. $this->_storageClient = null;
  115. unset($this);
  116. }
  117. /**
  118. * Enlist operation in current batch
  119. *
  120. * @param string $path Path
  121. * @param array $query Query parameters
  122. * @param string $httpVerb HTTP verb the request will use
  123. * @param array $headers x-ms headers to add
  124. * @param boolean $forTableStorage Is the request for table storage?
  125. * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
  126. * @throws Microsoft_WindowsAzure_Exception
  127. */
  128. public function enlistOperation($path = '/', $query = array(), $httpVerb = Microsoft_Http_Client::GET, $headers = array(), $forTableStorage = false, $rawData = null)
  129. {
  130. // Set _forTableStorage
  131. if ($forTableStorage) {
  132. $this->_forTableStorage = true;
  133. }
  134. // Set _isSingleSelect
  135. if ($httpVerb == Microsoft_Http_Client::GET) {
  136. if (count($this->_operations) > 0) {
  137. throw new Microsoft_WindowsAzure_Exception("Select operations can only be performed in an empty batch transaction.");
  138. }
  139. $this->_isSingleSelect = true;
  140. }
  141. // Clean path
  142. if (strpos($path, '/') !== 0) {
  143. $path = '/' . $path;
  144. }
  145. // Clean headers
  146. if (is_null($headers)) {
  147. $headers = array();
  148. }
  149. // Generate URL
  150. $requestUrl = $this->getBaseUrl() . $path;
  151. if (count($query) > 0) {
  152. $queryString = '';
  153. foreach ($query as $key => $value) {
  154. $queryString .= ($queryString ? '&' : '?') . rawurlencode($key) . '=' . rawurlencode($value);
  155. }
  156. $requestUrl .= $queryString;
  157. }
  158. // Generate $rawData
  159. if (is_null($rawData)) {
  160. $rawData = '';
  161. }
  162. // Add headers
  163. if ($httpVerb != Microsoft_Http_Client::GET) {
  164. $headers['Content-ID'] = count($this->_operations) + 1;
  165. if ($httpVerb != Microsoft_Http_Client::DELETE) {
  166. $headers['Content-Type'] = 'application/atom+xml;type=entry';
  167. }
  168. $headers['Content-Length'] = strlen($rawData);
  169. }
  170. // Generate $operation
  171. $operation = '';
  172. $operation .= $httpVerb . ' ' . $requestUrl . ' HTTP/1.1' . "\n";
  173. foreach ($headers as $key => $value)
  174. {
  175. $operation .= $key . ': ' . $value . "\n";
  176. }
  177. $operation .= "\n";
  178. // Add data
  179. $operation .= $rawData;
  180. // Store operation
  181. $this->_operations[] = $operation;
  182. }
  183. /**
  184. * Commit current batch
  185. *
  186. * @return Microsoft_Http_Response
  187. * @throws Microsoft_WindowsAzure_Exception
  188. */
  189. public function commit()
  190. {
  191. // Perform batch
  192. $response = $this->_storageClient->performBatch($this->_operations, $this->_forTableStorage, $this->_isSingleSelect);
  193. // Dispose
  194. $this->_clean();
  195. // Parse response
  196. $errors = null;
  197. preg_match_all('/<message (.*?)>(.*?)<\/message>/s', $response->getBody(), $errors);
  198. // Error?
  199. if (count($errors[2]) > 0) {
  200. throw new Microsoft_WindowsAzure_Exception('An error has occured while committing a batch: ' . $errors[2][0]);
  201. }
  202. // Return
  203. return $response;
  204. }
  205. /**
  206. * Rollback current batch
  207. */
  208. public function rollback()
  209. {
  210. // Dispose
  211. $this->_clean();
  212. }
  213. /**
  214. * Get operation count
  215. *
  216. * @return integer
  217. */
  218. public function getOperationCount()
  219. {
  220. return count($this->_operations);
  221. }
  222. /**
  223. * Is single select?
  224. *
  225. * @return boolean
  226. */
  227. public function isSingleSelect()
  228. {
  229. return $this->_isSingleSelect;
  230. }
  231. }