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

Source for file BatchStorage.php

Documentation is available at BatchStorage.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009, 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, 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_WindowsAzure_Storage
  38.  */
  39. require_once 'Microsoft/WindowsAzure/Storage.php';
  40.  
  41. /**
  42.  * @see Microsoft_WindowsAzure_Credentials
  43.  */
  44. require_once 'Microsoft/WindowsAzure/Credentials.php';
  45.  
  46. /**
  47.  * @see Microsoft_WindowsAzure_Exception
  48.  */
  49. require_once 'Microsoft/WindowsAzure/Exception.php';
  50.  
  51. /**
  52.  * @see Microsoft_WindowsAzure_Storage_Batch
  53.  */
  54. require_once 'Microsoft/WindowsAzure/Storage/Batch.php';
  55.  
  56. /**
  57.  * @see Microsoft_Http_Transport
  58.  */
  59. require_once 'Microsoft/Http/Transport.php';
  60.  
  61. /**
  62.  * @see Microsoft_Http_Response
  63.  */
  64. require_once 'Microsoft/Http/Response.php';
  65.  
  66. /**
  67.  * @category   Microsoft
  68.  * @package    Microsoft_WindowsAzure
  69.  * @subpackage Storage
  70.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  71.  * @license    http://phpazure.codeplex.com/license
  72.  */
  73. {    
  74.     /**
  75.      * Current batch
  76.      * 
  77.      * @var Microsoft_WindowsAzure_Storage_Batch 
  78.      */
  79.     protected $_currentBatch = null;
  80.     
  81.     /**
  82.      * Set current batch
  83.      * 
  84.      * @param Microsoft_WindowsAzure_Storage_Batch $batch Current batch
  85.      * @throws Microsoft_WindowsAzure_Exception
  86.      */
  87.     public function setCurrentBatch(Microsoft_WindowsAzure_Storage_Batch $batch null)
  88.     {
  89.         if (!is_null($batch&& $this->isInBatch())
  90.         {
  91.             throw new Microsoft_WindowsAzure_Exception('Only one batch can be active at a time.');
  92.         }
  93.         $this->_currentBatch = $batch;
  94.     }
  95.     
  96.     /**
  97.      * Get current batch
  98.      * 
  99.      * @return Microsoft_WindowsAzure_Storage_Batch 
  100.      */
  101.     public function getCurrentBatch()
  102.     {
  103.         return $this->_currentBatch;
  104.     }
  105.     
  106.     /**
  107.      * Is there a current batch?
  108.      * 
  109.      * @return boolean 
  110.      */
  111.     public function isInBatch()
  112.     {
  113.         return !is_null($this->_currentBatch);
  114.     }
  115.     
  116.     /**
  117.      * Starts a new batch operation set
  118.      * 
  119.      * @return Microsoft_WindowsAzure_Storage_Batch 
  120.      * @throws Microsoft_WindowsAzure_Exception
  121.      */
  122.     public function startBatch()
  123.     {
  124.         return new Microsoft_WindowsAzure_Storage_Batch($this$this->getBaseUrl());
  125.     }
  126.     
  127.     /**
  128.      * Perform batch using Microsoft_Http_Transport channel, combining all batch operations into one request
  129.      *
  130.      * @param array $operations Operations in batch
  131.      * @param boolean $forTableStorage Is the request for table storage?
  132.      * @param boolean $isSingleSelect Is the request a single select statement?
  133.      * @param string $resourceType Resource type
  134.      * @param string $requiredPermission Required permission
  135.      * @return Microsoft_Http_Response 
  136.      */
  137.     public function performBatch($operations array()$forTableStorage false$isSingleSelect false$resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN$requiredPermission Microsoft_WindowsAzure_Credentials::PERMISSION_READ)
  138.     {
  139.         // Generate boundaries
  140.         $batchBoundary 'batch_' md5(time(microtime());
  141.         $changesetBoundary 'changeset_' md5(time(microtime());
  142.         
  143.         // Set headers
  144.         $headers array();
  145.         
  146.         // Add version header
  147.         $headers['x-ms-version'$this->_apiVersion;
  148.         
  149.         // Add content-type header
  150.         $headers['Content-Type''multipart/mixed; boundary=' $batchBoundary;
  151.  
  152.         // Set path and query string
  153.         $path           '/$batch';
  154.         $queryString    '';
  155.         
  156.         // Set verb
  157.         $httpVerb Microsoft_Http_Transport::VERB_POST;
  158.         
  159.         // Generate raw data
  160.         $rawData '';
  161.             
  162.         // Single select?
  163.         if ($isSingleSelect)
  164.         {
  165.             $operation $operations[0];
  166.             $rawData .= '--' $batchBoundary "\n";
  167.             $rawData .= 'Content-Type: application/http' "\n";
  168.             $rawData .= 'Content-Transfer-Encoding: binary' "\n\n";
  169.             $rawData .= $operation
  170.             $rawData .= '--' $batchBoundary '--';
  171.         
  172.         else 
  173.         {
  174.             $rawData .= '--' $batchBoundary "\n";
  175.             $rawData .= 'Content-Type: multipart/mixed; boundary=' $changesetBoundary "\n\n";
  176.             
  177.                 // Add operations
  178.                 foreach ($operations as $operation)
  179.                 {
  180.                     $rawData .= '--' $changesetBoundary "\n";
  181.                     $rawData .= 'Content-Type: application/http' "\n";
  182.                     $rawData .= 'Content-Transfer-Encoding: binary' "\n\n";
  183.                     $rawData .= $operation;
  184.                 }
  185.                 $rawData .= '--' $changesetBoundary '--' "\n";
  186.                             
  187.             $rawData .= '--' $batchBoundary '--';
  188.         }
  189.  
  190.         // Generate URL and sign request
  191.         $requestUrl     $this->_credentials->signRequestUrl($this->getBaseUrl($path $queryString$resourceType$requiredPermission);
  192.         $requestHeaders $this->_credentials->signRequestHeaders($httpVerb$path$queryString$headers$forTableStorage$resourceType$requiredPermission);
  193.  
  194.         $requestClient  Microsoft_Http_Transport::createChannel();
  195.         if ($this->_useProxy)
  196.         {
  197.             $requestClient->setProxy($this->_useProxy$this->_proxyUrl$this->_proxyPort$this->_proxyCredentials);
  198.         }
  199.         $response $this->_retryPolicy->execute(
  200.             array($requestClient'request'),
  201.             array($httpVerb$requestUrlarray()$requestHeaders$rawData)
  202.         );
  203.         
  204.         $requestClient null;
  205.         unset($requestClient);
  206.  
  207.         return $response;
  208.     }
  209. }

Documentation generated on Thu, 26 Nov 2009 08:04:39 +0100 by phpDocumentor 1.4.3