PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/luizbrandaoj/mini-blog
PHP | 197 lines | 76 code | 26 blank | 95 comment | 4 complexity | 5b54ec8e054b6171d7d60c25f6653cd9 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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 ($batch !== null && $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 $this->_currentBatch !== null;
  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 dataservice headers
  123. $headers['DataServiceVersion'] = '1.0;NetFx';
  124. $headers['MaxDataServiceVersion'] = '1.0;NetFx';
  125. // Add content-type header
  126. $headers['Content-Type'] = 'multipart/mixed; boundary=' . $batchBoundary;
  127. // Set path and query string
  128. $path = '/$batch';
  129. $queryString = '';
  130. // Set verb
  131. $httpVerb = Zend_Http_Client::POST;
  132. // Generate raw data
  133. $rawData = '';
  134. // Single select?
  135. if ($isSingleSelect) {
  136. $operation = $operations[0];
  137. $rawData .= '--' . $batchBoundary . "\n";
  138. $rawData .= 'Content-Type: application/http' . "\n";
  139. $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
  140. $rawData .= $operation;
  141. $rawData .= '--' . $batchBoundary . '--';
  142. } else {
  143. $rawData .= '--' . $batchBoundary . "\n";
  144. $rawData .= 'Content-Type: multipart/mixed; boundary=' . $changesetBoundary . "\n\n";
  145. // Add operations
  146. foreach ($operations as $operation)
  147. {
  148. $rawData .= '--' . $changesetBoundary . "\n";
  149. $rawData .= 'Content-Type: application/http' . "\n";
  150. $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
  151. $rawData .= $operation;
  152. }
  153. $rawData .= '--' . $changesetBoundary . '--' . "\n";
  154. $rawData .= '--' . $batchBoundary . '--';
  155. }
  156. // Generate URL and sign request
  157. $requestUrl = $this->_credentials->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission);
  158. $requestHeaders = $this->_credentials->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission);
  159. // Prepare request
  160. $this->_httpClientChannel->resetParameters(true);
  161. $this->_httpClientChannel->setUri($requestUrl);
  162. $this->_httpClientChannel->setHeaders($requestHeaders);
  163. $this->_httpClientChannel->setRawData($rawData);
  164. // Execute request
  165. $response = $this->_retryPolicy->execute(
  166. array($this->_httpClientChannel, 'request'),
  167. array($httpVerb)
  168. );
  169. return $response;
  170. }
  171. }