File indexing completed on 2024-06-23 05:55:45

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_WindowsAzure
0017  * @subpackage Storage
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Service_WindowsAzure_Storage
0025  */
0026 // require_once 'Zend/Service/WindowsAzure/Storage.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Service_WindowsAzure
0031  * @subpackage Storage
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 abstract class Zend_Service_WindowsAzure_Storage_BatchStorageAbstract
0036     extends Zend_Service_WindowsAzure_Storage
0037 { 
0038     /**
0039      * Current batch
0040      * 
0041      * @var Zend_Service_WindowsAzure_Storage_Batch
0042      */
0043     protected $_currentBatch = null;
0044     
0045     /**
0046      * Set current batch
0047      * 
0048      * @param Zend_Service_WindowsAzure_Storage_Batch $batch Current batch
0049      * @throws Zend_Service_WindowsAzure_Exception
0050      */
0051     public function setCurrentBatch(Zend_Service_WindowsAzure_Storage_Batch $batch = null)
0052     {
0053         if (!is_null($batch) && $this->isInBatch()) {
0054       // require_once 'Zend/Service/WindowsAzure/Exception.php';
0055             throw new Zend_Service_WindowsAzure_Exception('Only one batch can be active at a time.');
0056         }
0057         $this->_currentBatch = $batch;
0058     }
0059     
0060     /**
0061      * Get current batch
0062      * 
0063      * @return Zend_Service_WindowsAzure_Storage_Batch
0064      */
0065     public function getCurrentBatch()
0066     {
0067         return $this->_currentBatch;
0068     }
0069     
0070     /**
0071      * Is there a current batch?
0072      * 
0073      * @return boolean
0074      */
0075     public function isInBatch()
0076     {
0077         return !is_null($this->_currentBatch);
0078     }
0079     
0080     /**
0081      * Starts a new batch operation set
0082      * 
0083      * @return Zend_Service_WindowsAzure_Storage_Batch
0084      * @throws Zend_Service_WindowsAzure_Exception
0085      */
0086     public function startBatch()
0087     {
0088     // require_once 'Zend/Service/WindowsAzure/Storage/Batch.php';
0089         return new Zend_Service_WindowsAzure_Storage_Batch($this, $this->getBaseUrl());
0090     }
0091   
0092   /**
0093    * Perform batch using Zend_Http_Client channel, combining all batch operations into one request
0094    *
0095    * @param array $operations Operations in batch
0096    * @param boolean $forTableStorage Is the request for table storage?
0097    * @param boolean $isSingleSelect Is the request a single select statement?
0098    * @param string $resourceType Resource type
0099    * @param string $requiredPermission Required permission
0100    * @return Zend_Http_Response
0101    */
0102   public function performBatch($operations = array(), $forTableStorage = false, $isSingleSelect = false, $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN, $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ)
0103   {
0104       // Generate boundaries
0105       $batchBoundary = 'batch_' . md5(time() . microtime());
0106       $changesetBoundary = 'changeset_' . md5(time() . microtime());
0107       
0108       // Set headers
0109       $headers = array();
0110       
0111     // Add version header
0112     $headers['x-ms-version'] = $this->_apiVersion;
0113     
0114     // Add dataservice headers
0115     $headers['DataServiceVersion'] = '1.0;NetFx';
0116     $headers['MaxDataServiceVersion'] = '1.0;NetFx';
0117     
0118     // Add content-type header
0119     $headers['Content-Type'] = 'multipart/mixed; boundary=' . $batchBoundary;
0120 
0121     // Set path and query string
0122     $path           = '/$batch';
0123     $queryString    = '';
0124     
0125     // Set verb
0126     $httpVerb = Zend_Http_Client::POST;
0127     
0128     // Generate raw data
0129       $rawData = '';
0130         
0131     // Single select?
0132     if ($isSingleSelect) {
0133         $operation = $operations[0];
0134         $rawData .= '--' . $batchBoundary . "\n";
0135             $rawData .= 'Content-Type: application/http' . "\n";
0136             $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
0137             $rawData .= $operation; 
0138             $rawData .= '--' . $batchBoundary . '--';
0139     } else {
0140         $rawData .= '--' . $batchBoundary . "\n";
0141         $rawData .= 'Content-Type: multipart/mixed; boundary=' . $changesetBoundary . "\n\n";
0142         
0143             // Add operations
0144             foreach ($operations as $operation)
0145             {
0146                     $rawData .= '--' . $changesetBoundary . "\n";
0147                   $rawData .= 'Content-Type: application/http' . "\n";
0148                   $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
0149                   $rawData .= $operation;
0150             }
0151             $rawData .= '--' . $changesetBoundary . '--' . "\n";
0152                     
0153         $rawData .= '--' . $batchBoundary . '--';
0154     }
0155 
0156     // Generate URL and sign request
0157     $requestUrl     = $this->_credentials->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission);
0158     $requestHeaders = $this->_credentials->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission);
0159 
0160     // Prepare request
0161     $this->_httpClientChannel->resetParameters(true);
0162     $this->_httpClientChannel->setUri($requestUrl);
0163     $this->_httpClientChannel->setHeaders($requestHeaders);
0164     $this->_httpClientChannel->setRawData($rawData);
0165     
0166     // Execute request
0167     $response = $this->_retryPolicy->execute(
0168         array($this->_httpClientChannel, 'request'),
0169         array($httpVerb)
0170     );
0171 
0172     return $response;
0173   }
0174 }