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

Source for file Batch.php

Documentation is available at Batch.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_Exception
  38.  */
  39. require_once 'Microsoft/WindowsAzure/Exception.php';
  40.  
  41. /**
  42.  * @see Microsoft_WindowsAzure_Storage_BatchStorage
  43.  */
  44. require_once 'Microsoft/WindowsAzure/Storage/BatchStorage.php';
  45.  
  46. /**
  47.  * @category   Microsoft
  48.  * @package    Microsoft_WindowsAzure
  49.  * @subpackage Storage
  50.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  51.  * @license    http://phpazure.codeplex.com/license
  52.  */
  53. {    
  54.     /**
  55.      * Storage client the batch is defined on
  56.      * 
  57.      * @var Microsoft_WindowsAzure_Storage_BatchStorage 
  58.      */
  59.     protected $_storageClient = null;
  60.     
  61.     /**
  62.      * For table storage?
  63.      * 
  64.      * @var boolean 
  65.      */
  66.     protected $_forTableStorage = false;
  67.     
  68.     /**
  69.      * Base URL
  70.      * 
  71.      * @var string 
  72.      */
  73.     protected $_baseUrl;
  74.     
  75.     /**
  76.      * Pending operations
  77.      * 
  78.      * @var unknown_type 
  79.      */
  80.     protected $_operations = array();
  81.     
  82.     /**
  83.      * Does the batch contain a single select?
  84.      * 
  85.      * @var boolean 
  86.      */
  87.     protected $_isSingleSelect = false;
  88.     
  89.     /**
  90.      * Creates a new Microsoft_WindowsAzure_Storage_Batch
  91.      * 
  92.      * @param Microsoft_WindowsAzure_Storage_BatchStorage $storageClient Storage client the batch is defined on
  93.      */
  94.     public function __construct(Microsoft_WindowsAzure_Storage_BatchStorage $storageClient null$baseUrl '')
  95.     {
  96.         $this->_storageClient = $storageClient;
  97.         $this->_baseUrl = $baseUrl;
  98.         $this->beginBatch();
  99.     }
  100.     
  101.     /**
  102.      * Get base URL for creating requests
  103.      *
  104.      * @return string 
  105.      */
  106.     public function getBaseUrl()
  107.     {
  108.         return $this->_baseUrl;
  109.     }
  110.     
  111.     /**
  112.      * Starts a new batch operation set
  113.      * 
  114.      * @throws Microsoft_WindowsAzure_Exception
  115.      */
  116.     protected function beginBatch()
  117.     {
  118.         $this->_storageClient->setCurrentBatch($this);
  119.     }
  120.     
  121.     /**
  122.      * Cleanup current batch
  123.      */
  124.     protected function clean()
  125.     {
  126.         unset($this->_operations);
  127.         $this->_storageClient->setCurrentBatch(null);
  128.         $this->_storageClient = null;
  129.         unset($this);
  130.     }
  131.  
  132.     /**
  133.      * Enlist operation in current batch
  134.      *
  135.      * @param string $path Path
  136.      * @param string $queryString Query string
  137.      * @param string $httpVerb HTTP verb the request will use
  138.      * @param array $headers x-ms headers to add
  139.      * @param boolean $forTableStorage Is the request for table storage?
  140.      * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
  141.      * @throws Microsoft_WindowsAzure_Exception
  142.      */
  143.     public function enlistOperation($path '/'$queryString ''$httpVerb Microsoft_Http_Transport::VERB_GET$headers array()$forTableStorage false$rawData null)
  144.     {
  145.         // Set _forTableStorage
  146.         if ($forTableStorage)
  147.         {
  148.             $this->_forTableStorage = true;
  149.         }
  150.         
  151.         // Set _isSingleSelect
  152.         if ($httpVerb == Microsoft_Http_Transport::VERB_GET)
  153.         {
  154.             if (count($this->_operations0)
  155.                 throw new Microsoft_WindowsAzure_Exception("Select operations can only be performed in an empty batch transaction.");
  156.             $this->_isSingleSelect = true;
  157.         }
  158.         
  159.         // Clean path
  160.         if (strpos($path'/'!== 0
  161.             $path '/' $path;
  162.             
  163.         // Clean headers
  164.         if (is_null($headers))
  165.             $headers array();
  166.             
  167.         // URL encoding
  168.         $path           Microsoft_WindowsAzure_Storage::urlencode($path);
  169.         $queryString    Microsoft_WindowsAzure_Storage::urlencode($queryString);
  170.  
  171.         // Generate URL
  172.         $requestUrl     $this->getBaseUrl($path $queryString;
  173.         
  174.         // Generate $rawData
  175.         if (is_null($rawData))
  176.             $rawData '';
  177.             
  178.         // Add headers
  179.         if ($httpVerb != Microsoft_Http_Transport::VERB_GET)
  180.         {
  181.             $headers['Content-ID'count($this->_operations1;
  182.             if ($httpVerb != Microsoft_Http_Transport::VERB_DELETE)
  183.                 $headers['Content-Type''application/atom+xml;type=entry';
  184.             $headers['Content-Length'strlen($rawData);
  185.         }
  186.             
  187.         // Generate $operation
  188.         $operation '';
  189.         $operation .= $httpVerb ' ' $requestUrl ' HTTP/1.1' "\n";
  190.         foreach ($headers as $key => $value)
  191.         {
  192.             $operation .= $key ': ' $value "\n";
  193.         }
  194.         $operation .= "\n";
  195.         
  196.         // Add data
  197.         $operation .= $rawData;
  198.  
  199.         // Store operation
  200.         $this->_operations[$operation;            
  201.     }
  202.     
  203.     /**
  204.      * Commit current batch
  205.      * 
  206.      * @return Microsoft_Http_Response 
  207.      * @throws Microsoft_WindowsAzure_Exception
  208.      */
  209.     public function commit()
  210.     {
  211.         // Perform batch
  212.         $response $this->_storageClient->performBatch($this->_operations$this->_forTableStorage$this->_isSingleSelect);
  213.         
  214.         // Dispose
  215.         $this->clean();
  216.         
  217.         // Parse response
  218.         $errors null;
  219.         preg_match_all('/<message (.*)>(.*)<\/message>/'$response->getBody()$errors);
  220.         
  221.         // Error?
  222.         if (count($errors[2]0)
  223.         {
  224.             throw new Microsoft_WindowsAzure_Exception('An error has occured while committing a batch: ' $errors[2][0]);
  225.         }
  226.         
  227.         // Return
  228.         return $response;
  229.     }
  230.     
  231.     /**
  232.      * Rollback current batch
  233.      */
  234.     public function rollback()
  235.     {
  236.         // Dispose
  237.         $this->clean();
  238.     }
  239.     
  240.     /**
  241.      * Get operation count
  242.      * 
  243.      * @return integer 
  244.      */
  245.     public function getOperationCount()
  246.     {
  247.         return count($this->_operations);
  248.     }
  249.     
  250.     /**
  251.      * Is single select?
  252.      * 
  253.      * @return boolean 
  254.      */
  255.     public function isSingleSelect()
  256.     {
  257.         return $this->_isSingleSelect;
  258.     }
  259. }

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