PageRenderTime 124ms CodeModel.GetById 61ms app.highlight 6ms RepoModel.GetById 54ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/Storage/BatchStorageAbstract.php

https://bitbucket.org/ktos/tinyshare
PHP | 184 lines | 70 code | 21 blank | 93 comment | 4 complexity | b669dfbe040710165d58f75917454d3c MD5 | raw file
  1<?php
  2/**
  3 * Copyright (c) 2009 - 2011, RealDolmen
  4 * All rights reserved.
  5 *
  6 * Redistribution and use in source and binary forms, with or without
  7 * modification, are permitted provided that the following conditions are met:
  8 *     * Redistributions of source code must retain the above copyright
  9 *       notice, this list of conditions and the following disclaimer.
 10 *     * Redistributions in binary form must reproduce the above copyright
 11 *       notice, this list of conditions and the following disclaimer in the
 12 *       documentation and/or other materials provided with the distribution.
 13 *     * Neither the name of RealDolmen nor the
 14 *       names of its contributors may be used to endorse or promote products
 15 *       derived from this software without specific prior written permission.
 16 *
 17 * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 20 * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27 *
 28 * @category   Microsoft
 29 * @package    Microsoft_WindowsAzure
 30 * @subpackage Storage
 31 * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
 32 * @license    http://phpazure.codeplex.com/license
 33 * @version    $Id: Storage.php 21617 2009-06-12 10:46:31Z unknown $
 34 */
 35
 36/**
 37 * @see Microsoft_AutoLoader
 38 */
 39require_once dirname(__FILE__) . '/../../AutoLoader.php';
 40
 41/**
 42 * @category   Microsoft
 43 * @package    Microsoft_WindowsAzure
 44 * @subpackage Storage
 45 * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
 46 * @license    http://phpazure.codeplex.com/license
 47 */
 48abstract class Microsoft_WindowsAzure_Storage_BatchStorageAbstract
 49    extends Microsoft_WindowsAzure_Storage
 50{	
 51    /**
 52     * Current batch
 53     * 
 54     * @var Microsoft_WindowsAzure_Storage_Batch
 55     */
 56    protected $_currentBatch = null;
 57    
 58    /**
 59     * Set current batch
 60     * 
 61     * @param Microsoft_WindowsAzure_Storage_Batch $batch Current batch
 62     * @throws Microsoft_WindowsAzure_Exception
 63     */
 64    public function setCurrentBatch(Microsoft_WindowsAzure_Storage_Batch $batch = null)
 65    {
 66        if (!is_null($batch) && $this->isInBatch()) {
 67            throw new Microsoft_WindowsAzure_Exception('Only one batch can be active at a time.');
 68        }
 69        $this->_currentBatch = $batch;
 70    }
 71    
 72    /**
 73     * Get current batch
 74     * 
 75     * @return Microsoft_WindowsAzure_Storage_Batch
 76     */
 77    public function getCurrentBatch()
 78    {
 79        return $this->_currentBatch;
 80    }
 81    
 82    /**
 83     * Is there a current batch?
 84     * 
 85     * @return boolean
 86     */
 87    public function isInBatch()
 88    {
 89        return !is_null($this->_currentBatch);
 90    }
 91    
 92    /**
 93     * Starts a new batch operation set
 94     * 
 95     * @return Microsoft_WindowsAzure_Storage_Batch
 96     * @throws Microsoft_WindowsAzure_Exception
 97     */
 98    public function startBatch()
 99    {
100        return new Microsoft_WindowsAzure_Storage_Batch($this, $this->getBaseUrl());
101    }
102	
103	/**
104	 * Perform batch using Microsoft_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 Microsoft_Http_Response
112	 */
113	public function performBatch($operations = array(), $forTableStorage = false, $isSingleSelect = false, $resourceType = Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN, $requiredPermission = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ)
114	{
115	    // Generate boundaries
116	    $batchBoundary = 'batch_' . md5(time() . microtime());
117	    $changesetBoundary = 'changeset_' . md5(time() . microtime());
118	    
119	    // Set headers
120	    $headers = array();
121	    
122		// Add version header
123		$headers['x-ms-version'] = $this->_apiVersion;
124		
125		// Add dataservice headers
126		$headers['DataServiceVersion'] = '1.0;NetFx';
127		$headers['MaxDataServiceVersion'] = '1.0;NetFx';
128		
129		// Add content-type header
130		$headers['Content-Type'] = 'multipart/mixed; boundary=' . $batchBoundary;
131
132		// Set path
133		$path           = '/$batch';
134		
135		// Set verb
136		$httpVerb = Microsoft_Http_Client::POST;
137		
138		// Generate raw data
139    	$rawData = '';
140    		
141		// Single select?
142		if ($isSingleSelect) {
143		    $operation = $operations[0];
144		    $rawData .= '--' . $batchBoundary . "\n";
145            $rawData .= 'Content-Type: application/http' . "\n";
146            $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
147            $rawData .= $operation; 
148            $rawData .= '--' . $batchBoundary . '--';
149		} else {
150    		$rawData .= '--' . $batchBoundary . "\n";
151    		$rawData .= 'Content-Type: multipart/mixed; boundary=' . $changesetBoundary . "\n\n";
152    		
153        		// Add operations
154        		foreach ($operations as $operation)
155        		{
156                    $rawData .= '--' . $changesetBoundary . "\n";
157                	$rawData .= 'Content-Type: application/http' . "\n";
158                	$rawData .= 'Content-Transfer-Encoding: binary' . "\n\n";
159                	$rawData .= $operation;
160        		}
161        		$rawData .= '--' . $changesetBoundary . '--' . "\n";
162    		    		    
163    		$rawData .= '--' . $batchBoundary . '--';
164		}
165
166		// Generate URL and sign request
167		$requestUrl     = $this->_credentials->signRequestUrl($this->getBaseUrl() . $path, $resourceType, $requiredPermission);
168		$requestHeaders = $this->_credentials->signRequestHeaders($httpVerb, $path, array(), $headers, $forTableStorage, $resourceType, $requiredPermission);
169
170		// Prepare request
171		$this->_httpClientChannel->resetParameters(true);
172		$this->_httpClientChannel->setUri($requestUrl);
173		$this->_httpClientChannel->setHeaders($requestHeaders);
174		$this->_httpClientChannel->setRawData($rawData);
175		
176		// Execute request
177		$response = $this->_retryPolicy->execute(
178		    array($this->_httpClientChannel, 'request'),
179		    array($httpVerb)
180		);
181
182		return $response;
183	}
184}