PageRenderTime 123ms CodeModel.GetById 60ms app.highlight 6ms RepoModel.GetById 54ms app.codeStats 0ms

/Service/WindowsAzure/Storage/BatchStorageAbstract.php

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