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

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