Microsoft_WindowsAzure
[ class tree: Microsoft_WindowsAzure ] [ index: Microsoft_WindowsAzure ] [ all elements ]

Source for file BatchStorageAbstract.php

Documentation is available at BatchStorageAbstract.php

  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.  */
  39. require_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.  */
  48. {    
  49.     /**
  50.      * Current batch
  51.      * 
  52.      * @var Microsoft_WindowsAzure_Storage_Batch 
  53.      */
  54.     protected $_currentBatch = null;
  55.     
  56.     /**
  57.      * Set current batch
  58.      * 
  59.      * @param Microsoft_WindowsAzure_Storage_Batch $batch Current batch
  60.      * @throws Microsoft_WindowsAzure_Exception
  61.      */
  62.     public function setCurrentBatch(Microsoft_WindowsAzure_Storage_Batch $batch null)
  63.     {
  64.         if (!is_null($batch&& $this->isInBatch()) {
  65.             throw new Microsoft_WindowsAzure_Exception('Only one batch can be active at a time.');
  66.         }
  67.         $this->_currentBatch = $batch;
  68.     }
  69.     
  70.     /**
  71.      * Get current batch
  72.      * 
  73.      * @return Microsoft_WindowsAzure_Storage_Batch 
  74.      */
  75.     public function getCurrentBatch()
  76.     {
  77.         return $this->_currentBatch;
  78.     }
  79.     
  80.     /**
  81.      * Is there a current batch?
  82.      * 
  83.      * @return boolean 
  84.      */
  85.     public function isInBatch()
  86.     {
  87.         return !is_null($this->_currentBatch);
  88.     }
  89.     
  90.     /**
  91.      * Starts a new batch operation set
  92.      * 
  93.      * @return Microsoft_WindowsAzure_Storage_Batch 
  94.      * @throws Microsoft_WindowsAzure_Exception
  95.      */
  96.     public function startBatch()
  97.     {
  98.         return new Microsoft_WindowsAzure_Storage_Batch($this$this->getBaseUrl());
  99.     }
  100.     
  101.     /**
  102.      * Perform batch using Microsoft_Http_Client channel, combining all batch operations into one request
  103.      *
  104.      * @param array $operations Operations in batch
  105.      * @param boolean $forTableStorage Is the request for table storage?
  106.      * @param boolean $isSingleSelect Is the request a single select statement?
  107.      * @param string $resourceType Resource type
  108.      * @param string $requiredPermission Required permission
  109.      * @return Microsoft_Http_Response 
  110.      */
  111.     public function performBatch($operations array()$forTableStorage false$isSingleSelect false$resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN$requiredPermission Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ)
  112.     {
  113.         // Generate boundaries
  114.         $batchBoundary 'batch_' md5(time(microtime());
  115.         $changesetBoundary 'changeset_' md5(time(microtime());
  116.         
  117.         // Set headers
  118.         $headers array();
  119.         
  120.         // Add version header
  121.         $headers['x-ms-version'$this->_apiVersion;
  122.         
  123.         // Add dataservice headers
  124.         $headers['DataServiceVersion''1.0;NetFx';
  125.         $headers['MaxDataServiceVersion''1.0;NetFx';
  126.         
  127.         // Add content-type header
  128.         $headers['Content-Type''multipart/mixed; boundary=' $batchBoundary;
  129.  
  130.         // Set path and query string
  131.         $path           '/$batch';
  132.         $queryString    '';
  133.         
  134.         // Set verb
  135.         $httpVerb Microsoft_Http_Client::POST;
  136.         
  137.         // Generate raw data
  138.         $rawData '';
  139.             
  140.         // Single select?
  141.         if ($isSingleSelect{
  142.             $operation $operations[0];
  143.             $rawData .= '--' $batchBoundary "\n";
  144.             $rawData .= 'Content-Type: application/http' "\n";
  145.             $rawData .= 'Content-Transfer-Encoding: binary' "\n\n";
  146.             $rawData .= $operation
  147.             $rawData .= '--' $batchBoundary '--';
  148.         else {
  149.             $rawData .= '--' $batchBoundary "\n";
  150.             $rawData .= 'Content-Type: multipart/mixed; boundary=' $changesetBoundary "\n\n";
  151.             
  152.                 // Add operations
  153.                 foreach ($operations as $operation)
  154.                 {
  155.                     $rawData .= '--' $changesetBoundary "\n";
  156.                     $rawData .= 'Content-Type: application/http' "\n";
  157.                     $rawData .= 'Content-Transfer-Encoding: binary' "\n\n";
  158.                     $rawData .= $operation;
  159.                 }
  160.                 $rawData .= '--' $changesetBoundary '--' "\n";
  161.                             
  162.             $rawData .= '--' $batchBoundary '--';
  163.         }
  164.  
  165.         // Generate URL and sign request
  166.         $requestUrl     $this->_credentials->signRequestUrl($this->getBaseUrl($path $queryString$resourceType$requiredPermission);
  167.         $requestHeaders $this->_credentials->signRequestHeaders($httpVerb$path$queryString$headers$forTableStorage$resourceType$requiredPermission);
  168.  
  169.         // Prepare request
  170.         $this->_httpClientChannel->resetParameters(true);
  171.         $this->_httpClientChannel->setUri($requestUrl);
  172.         $this->_httpClientChannel->setHeaders($requestHeaders);
  173.         $this->_httpClientChannel->setRawData($rawData);
  174.         
  175.         // Execute request
  176.         $response $this->_retryPolicy->execute(
  177.             array($this->_httpClientChannel'request'),
  178.             array($httpVerb)
  179.         );
  180.  
  181.         return $response;
  182.     }
  183. }

Documentation generated on Wed, 18 May 2011 12:06:05 +0200 by phpDocumentor 1.4.3