PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Vendor/1.12.8/Zend/Service/WindowsAzure/Storage/Batch.php

https://gitlab.com/cakephp-2-x-plugins/zendframework
PHP | 241 lines | 95 code | 29 blank | 117 comment | 12 complexity | 24a1e0a519e9d0b1c3b43d03d112710e MD5 | raw file
Possible License(s): BSD-3-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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Service_WindowsAzure
  25. * @subpackage Storage
  26. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Service_WindowsAzure_Storage_Batch
  30. {
  31. /**
  32. * Storage client the batch is defined on
  33. *
  34. * @var Zend_Service_WindowsAzure_Storage_BatchStorageAbstract
  35. */
  36. protected $_storageClient = null;
  37. /**
  38. * For table storage?
  39. *
  40. * @var boolean
  41. */
  42. protected $_forTableStorage = false;
  43. /**
  44. * Base URL
  45. *
  46. * @var string
  47. */
  48. protected $_baseUrl;
  49. /**
  50. * Pending operations
  51. *
  52. * @var unknown_type
  53. */
  54. protected $_operations = array();
  55. /**
  56. * Does the batch contain a single select?
  57. *
  58. * @var boolean
  59. */
  60. protected $_isSingleSelect = false;
  61. /**
  62. * Creates a new Zend_Service_WindowsAzure_Storage_Batch
  63. *
  64. * @param Zend_Service_WindowsAzure_Storage_BatchStorageAbstract $storageClient Storage client the batch is defined on
  65. */
  66. public function __construct(Zend_Service_WindowsAzure_Storage_BatchStorageAbstract $storageClient = null, $baseUrl = '')
  67. {
  68. $this->_storageClient = $storageClient;
  69. $this->_baseUrl = $baseUrl;
  70. $this->_beginBatch();
  71. }
  72. /**
  73. * Get base URL for creating requests
  74. *
  75. * @return string
  76. */
  77. public function getBaseUrl()
  78. {
  79. return $this->_baseUrl;
  80. }
  81. /**
  82. * Starts a new batch operation set
  83. *
  84. * @throws Zend_Service_WindowsAzure_Exception
  85. */
  86. protected function _beginBatch()
  87. {
  88. $this->_storageClient->setCurrentBatch($this);
  89. }
  90. /**
  91. * Cleanup current batch
  92. */
  93. protected function _clean()
  94. {
  95. unset($this->_operations);
  96. $this->_storageClient->setCurrentBatch(null);
  97. $this->_storageClient = null;
  98. unset($this);
  99. }
  100. /**
  101. * Enlist operation in current batch
  102. *
  103. * @param string $path Path
  104. * @param string $queryString Query string
  105. * @param string $httpVerb HTTP verb the request will use
  106. * @param array $headers x-ms headers to add
  107. * @param boolean $forTableStorage Is the request for table storage?
  108. * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
  109. * @throws Zend_Service_WindowsAzure_Exception
  110. */
  111. public function enlistOperation($path = '/', $queryString = '', $httpVerb = Zend_Http_Client::GET, $headers = array(), $forTableStorage = false, $rawData = null)
  112. {
  113. // Set _forTableStorage
  114. if ($forTableStorage) {
  115. $this->_forTableStorage = true;
  116. }
  117. // Set _isSingleSelect
  118. if ($httpVerb == Zend_Http_Client::GET) {
  119. if (count($this->_operations) > 0) {
  120. require_once 'Zend/Service/WindowsAzure/Exception.php';
  121. throw new Zend_Service_WindowsAzure_Exception("Select operations can only be performed in an empty batch transaction.");
  122. }
  123. $this->_isSingleSelect = true;
  124. }
  125. // Clean path
  126. if (strpos($path, '/') !== 0) {
  127. $path = '/' . $path;
  128. }
  129. // Clean headers
  130. if (is_null($headers)) {
  131. $headers = array();
  132. }
  133. // URL encoding
  134. $path = Zend_Service_WindowsAzure_Storage::urlencode($path);
  135. $queryString = Zend_Service_WindowsAzure_Storage::urlencode($queryString);
  136. // Generate URL
  137. $requestUrl = $this->getBaseUrl() . $path . $queryString;
  138. // Generate $rawData
  139. if (is_null($rawData)) {
  140. $rawData = '';
  141. }
  142. // Add headers
  143. if ($httpVerb != Zend_Http_Client::GET) {
  144. $headers['Content-ID'] = count($this->_operations) + 1;
  145. if ($httpVerb != Zend_Http_Client::DELETE) {
  146. $headers['Content-Type'] = 'application/atom+xml;type=entry';
  147. }
  148. $headers['Content-Length'] = strlen($rawData);
  149. }
  150. // Generate $operation
  151. $operation = '';
  152. $operation .= $httpVerb . ' ' . $requestUrl . ' HTTP/1.1' . "\n";
  153. foreach ($headers as $key => $value)
  154. {
  155. $operation .= $key . ': ' . $value . "\n";
  156. }
  157. $operation .= "\n";
  158. // Add data
  159. $operation .= $rawData;
  160. // Store operation
  161. $this->_operations[] = $operation;
  162. }
  163. /**
  164. * Commit current batch
  165. *
  166. * @return Zend_Http_Response
  167. * @throws Zend_Service_WindowsAzure_Exception
  168. */
  169. public function commit()
  170. {
  171. // Perform batch
  172. $response = $this->_storageClient->performBatch($this->_operations, $this->_forTableStorage, $this->_isSingleSelect);
  173. // Dispose
  174. $this->_clean();
  175. // Parse response
  176. $errors = null;
  177. preg_match_all('/<message (.*)>(.*)<\/message>/', $response->getBody(), $errors);
  178. // Error?
  179. if (count($errors[2]) > 0) {
  180. require_once 'Zend/Service/WindowsAzure/Exception.php';
  181. throw new Zend_Service_WindowsAzure_Exception('An error has occured while committing a batch: ' . $errors[2][0]);
  182. }
  183. // Return
  184. return $response;
  185. }
  186. /**
  187. * Rollback current batch
  188. */
  189. public function rollback()
  190. {
  191. // Dispose
  192. $this->_clean();
  193. }
  194. /**
  195. * Get operation count
  196. *
  197. * @return integer
  198. */
  199. public function getOperationCount()
  200. {
  201. return count($this->_operations);
  202. }
  203. /**
  204. * Is single select?
  205. *
  206. * @return boolean
  207. */
  208. public function isSingleSelect()
  209. {
  210. return $this->_isSingleSelect;
  211. }
  212. }