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

Source for file WindowsAzure.php

Documentation is available at WindowsAzure.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009 - 2010, 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 - 2010, 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 Log
  45.  * @copyright  Copyright (c) 2009 - 2010, RealDolmen (http://www.realdolmen.com)
  46.  * @license    http://phpazure.codeplex.com/license
  47.  */
  48. {
  49.     /**
  50.      * @var Microsoft_Log_Formatter_Interface 
  51.      */
  52.     protected $_formatter;
  53.  
  54.     /**
  55.      * Connection to a windows Azure
  56.      * 
  57.      * @var Microsoft_Service_WindowsAzure_Storage_Table 
  58.      */
  59.     protected $_tableStorageConnection = null;
  60.  
  61.     /**
  62.      * Name of the table to use for logging purposes
  63.      *
  64.      * @var string 
  65.      */
  66.     protected $_tableName = null;
  67.  
  68.     /**
  69.      * Whether to keep all messages to be logged in an external buffer until the script ends and
  70.      * only then to send the messages in batch to the logging component.
  71.      *
  72.      * @var bool 
  73.      */
  74.     protected $_bufferMessages = false;
  75.  
  76.     /**
  77.      * If message buffering is activated, it will store all the messages in this buffer and only
  78.      * write them to table storage (in a batch transaction) when the script ends.
  79.      *
  80.      * @var array 
  81.      */
  82.     protected $_messageBuffer = array();
  83.  
  84.     /**
  85.      * @param Microsoft_Service_WindowsAzure_Storage_Table $tableStorageConnection 
  86.      * @param string $tableName 
  87.      * @param bool   $createTable create the Windows Azure table for logging if it does not exist
  88.      */
  89.     public function __construct(Microsoft_WindowsAzure_Storage_Table $tableStorageConnection,
  90.         $tableName$createTable true$bufferMessages true)
  91.     {
  92.         if ($tableStorageConnection == null{
  93.             require_once 'Microsoft/Log/Exception.php';
  94.             throw new Microsoft_Log_Exception('No connection to the Windows Azure tables provided.');
  95.         }
  96.  
  97.         if (!is_string($tableName)) {
  98.             require_once 'Microsoft/Log/Exception.php';
  99.             throw new Microsoft_Log_Exception('Provided Windows Azure table name must be a string.');
  100.         }
  101.  
  102.         $this->_tableStorageConnection = $tableStorageConnection;
  103.         $this->_tableName              = $tableName;
  104.  
  105.         // create the logging table if it does not exist. It will add some overhead, so it's optional
  106.         if ($createTable{
  107.             $this->_tableStorageConnection->createTableIfNotExists($this->_tableName);
  108.         }
  109.  
  110.         // keep messages to be logged in an internal buffer and only send them over the wire when
  111.         // the script execution ends
  112.         if ($bufferMessages{
  113.             $this->_bufferMessages = $bufferMessages;
  114.         }
  115.  
  116.     }
  117.  
  118.     /**
  119.      * If the log messages have been stored in the internal buffer, just send them
  120.      * to table storage.
  121.      */
  122.     public function shutdown({
  123.         parent::shutdown();
  124.         if ($this->_bufferMessages{
  125.             $this->_tableStorageConnection->startBatch();
  126.             foreach ($this->_messageBuffer as $logEntity{
  127.                 $this->_tableStorageConnection->insertEntity($this->_tableName$logEntity);
  128.             }
  129.             $this->_tableStorageConnection->commit();
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Create a new instance of Microsoft_Log_Writer_WindowsAzure
  135.      *
  136.      * @param  array $config 
  137.      * @return Microsoft_Log_Writer_WindowsAzure 
  138.      * @throws Microsoft_Log_Exception
  139.      */
  140.     static public function factory($config)
  141.     {
  142.         $config self::_parseConfig($config);
  143.         $config array_merge(array(
  144.             'connection'  => null,
  145.             'tableName'   => null,
  146.             'createTable' => true,
  147.         )$config);
  148.  
  149.         return new self(
  150.             $config['connection'],
  151.             $config['tableName'],
  152.             $config['createTable']
  153.         );
  154.     }
  155.  
  156.     /**
  157.      * The only formatter accepted is already  loaded in the constructor
  158.      *
  159.      * @todo enable custom formatters using the WindowsAzure_Storage_DynamicTableEntity class
  160.      */
  161.     public function setFormatter(Microsoft_Log_Formatter_Interface $formatter)
  162.     {
  163.         require_once 'Microsoft/Log/Exception.php';
  164.         throw new Microsoft_Log_Exception(get_class($this' does not support formatting');
  165.     }
  166.  
  167.     /**
  168.      * Write a message to the table storage. If buffering is activated, then messages will just be
  169.      * added to an internal buffer.
  170.      *
  171.      * @param  array $event 
  172.      * @return void 
  173.      * @todo   format the event using a formatted, not in this method
  174.      */
  175.     protected function _write($event)
  176.     {
  177.         $logEntity $this->_formatter->format($event);
  178.  
  179.         if ($this->_bufferMessages{
  180.             $this->_messageBuffer[$logEntity;
  181.         else {
  182.             $this->_tableStorageConnection->insertEntity($this->_tableName$logEntity);
  183.         }
  184.     }
  185. }

Documentation generated on Wed, 18 May 2011 12:07:01 +0200 by phpDocumentor 1.4.3