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

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

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