File indexing completed on 2024-06-23 05:55:45

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_WindowsAzure
0017  * @subpackage Storage
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /**
0025  * @category   Zend
0026  * @package    Zend_Service_WindowsAzure
0027  * @subpackage Storage
0028  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0029  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0030  */
0031 class Zend_Service_WindowsAzure_Storage_Batch
0032 { 
0033     /**
0034      * Storage client the batch is defined on
0035      * 
0036      * @var Zend_Service_WindowsAzure_Storage_BatchStorageAbstract
0037      */
0038     protected $_storageClient = null;
0039     
0040     /**
0041      * For table storage?
0042      * 
0043      * @var boolean
0044      */
0045     protected $_forTableStorage = false;
0046     
0047     /**
0048      * Base URL
0049      * 
0050      * @var string
0051      */
0052     protected $_baseUrl;
0053     
0054     /**
0055      * Pending operations
0056      * 
0057      * @var unknown_type
0058      */
0059     protected $_operations = array();
0060     
0061     /**
0062      * Does the batch contain a single select?
0063      * 
0064      * @var boolean
0065      */
0066     protected $_isSingleSelect = false;
0067     
0068     /**
0069      * Creates a new Zend_Service_WindowsAzure_Storage_Batch
0070      * 
0071      * @param Zend_Service_WindowsAzure_Storage_BatchStorageAbstract $storageClient Storage client the batch is defined on
0072      */
0073     public function __construct(Zend_Service_WindowsAzure_Storage_BatchStorageAbstract $storageClient = null, $baseUrl = '')
0074     {
0075         $this->_storageClient = $storageClient;
0076         $this->_baseUrl       = $baseUrl;
0077         $this->_beginBatch();
0078     }
0079     
0080   /**
0081    * Get base URL for creating requests
0082    *
0083    * @return string
0084    */
0085   public function getBaseUrl()
0086   {
0087     return $this->_baseUrl;
0088   }
0089     
0090     /**
0091      * Starts a new batch operation set
0092      * 
0093      * @throws Zend_Service_WindowsAzure_Exception
0094      */
0095     protected function _beginBatch()
0096     {
0097         $this->_storageClient->setCurrentBatch($this);
0098     }
0099     
0100     /**
0101      * Cleanup current batch
0102      */
0103     protected function _clean()
0104     {
0105         unset($this->_operations);
0106         $this->_storageClient->setCurrentBatch(null);
0107         $this->_storageClient = null;
0108         unset($this);
0109     }
0110 
0111   /**
0112    * Enlist operation in current batch
0113    *
0114    * @param string $path Path
0115    * @param string $queryString Query string
0116    * @param string $httpVerb HTTP verb the request will use
0117    * @param array $headers x-ms headers to add
0118    * @param boolean $forTableStorage Is the request for table storage?
0119    * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
0120    * @throws Zend_Service_WindowsAzure_Exception
0121    */
0122   public function enlistOperation($path = '/', $queryString = '', $httpVerb = Zend_Http_Client::GET, $headers = array(), $forTableStorage = false, $rawData = null)
0123   {
0124       // Set _forTableStorage
0125       if ($forTableStorage) {
0126           $this->_forTableStorage = true;
0127       }
0128       
0129       // Set _isSingleSelect
0130       if ($httpVerb == Zend_Http_Client::GET) {
0131           if (count($this->_operations) > 0) {
0132                     // require_once 'Zend/Service/WindowsAzure/Exception.php';
0133               throw new Zend_Service_WindowsAzure_Exception("Select operations can only be performed in an empty batch transaction.");
0134           }
0135           $this->_isSingleSelect = true;
0136       }
0137       
0138       // Clean path
0139     if (strpos($path, '/') !== 0) {
0140       $path = '/' . $path;
0141     }
0142       
0143     // Clean headers
0144     if (is_null($headers)) {
0145         $headers = array();
0146     }
0147         
0148     // URL encoding
0149     $path           = Zend_Service_WindowsAzure_Storage::urlencode($path);
0150     $queryString    = Zend_Service_WindowsAzure_Storage::urlencode($queryString);
0151 
0152     // Generate URL
0153     $requestUrl     = $this->getBaseUrl() . $path . $queryString;
0154     
0155     // Generate $rawData
0156     if (is_null($rawData)) {
0157         $rawData = '';
0158     }
0159         
0160     // Add headers
0161     if ($httpVerb != Zend_Http_Client::GET) {
0162         $headers['Content-ID'] = count($this->_operations) + 1;
0163         if ($httpVerb != Zend_Http_Client::DELETE) {
0164             $headers['Content-Type'] = 'application/atom+xml;type=entry';
0165         }
0166         $headers['Content-Length'] = strlen($rawData);
0167     }
0168         
0169     // Generate $operation
0170     $operation = '';
0171     $operation .= $httpVerb . ' ' . $requestUrl . ' HTTP/1.1' . "\n";
0172     foreach ($headers as $key => $value)
0173     {
0174         $operation .= $key . ': ' . $value . "\n";
0175     }
0176     $operation .= "\n";
0177     
0178     // Add data
0179     $operation .= $rawData;
0180 
0181     // Store operation
0182     $this->_operations[] = $operation;          
0183   }
0184     
0185     /**
0186      * Commit current batch
0187      * 
0188      * @return Zend_Http_Response
0189      * @throws Zend_Service_WindowsAzure_Exception
0190      */
0191     public function commit()
0192     {     
0193         // Perform batch
0194         $response = $this->_storageClient->performBatch($this->_operations, $this->_forTableStorage, $this->_isSingleSelect);
0195         
0196         // Dispose
0197         $this->_clean();
0198         
0199         // Parse response
0200         $errors = null;
0201         preg_match_all('/<message (.*)>(.*)<\/message>/', $response->getBody(), $errors);
0202         
0203         // Error?
0204         if (count($errors[2]) > 0) {
0205             // require_once 'Zend/Service/WindowsAzure/Exception.php';
0206             throw new Zend_Service_WindowsAzure_Exception('An error has occured while committing a batch: ' . $errors[2][0]);
0207         }
0208         
0209         // Return
0210         return $response;
0211     }
0212     
0213     /**
0214      * Rollback current batch
0215      */
0216     public function rollback()
0217     {
0218         // Dispose
0219         $this->_clean();
0220     }
0221     
0222     /**
0223      * Get operation count
0224      * 
0225      * @return integer
0226      */
0227     public function getOperationCount()
0228     {
0229         return count($this->_operations);
0230     }
0231     
0232     /**
0233      * Is single select?
0234      * 
0235      * @return boolean
0236      */
0237     public function isSingleSelect()
0238     {
0239         return $this->_isSingleSelect;
0240     }
0241 }