web/enmi/Zend/Log/Writer/Db.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Log
       
    17  * @subpackage Writer
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Db.php 22513 2010-07-01 13:48:39Z ramon $
       
    21  */
       
    22 
       
    23 /** Zend_Log_Writer_Abstract */
       
    24 require_once 'Zend/Log/Writer/Abstract.php';
       
    25 
       
    26 /**
       
    27  * @category   Zend
       
    28  * @package    Zend_Log
       
    29  * @subpackage Writer
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  * @version    $Id: Db.php 22513 2010-07-01 13:48:39Z ramon $
       
    33  */
       
    34 class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract
       
    35 {
       
    36     /**
       
    37      * Database adapter instance
       
    38      * @var Zend_Db_Adapter
       
    39      */
       
    40     private $_db;
       
    41 
       
    42     /**
       
    43      * Name of the log table in the database
       
    44      * @var string
       
    45      */
       
    46     private $_table;
       
    47 
       
    48     /**
       
    49      * Relates database columns names to log data field keys.
       
    50      *
       
    51      * @var null|array
       
    52      */
       
    53     private $_columnMap;
       
    54 
       
    55     /**
       
    56      * Class constructor
       
    57      *
       
    58      * @param Zend_Db_Adapter $db   Database adapter instance
       
    59      * @param string $table         Log table in database
       
    60      * @param array $columnMap
       
    61      */
       
    62     public function __construct($db, $table, $columnMap = null)
       
    63     {
       
    64         $this->_db    = $db;
       
    65         $this->_table = $table;
       
    66         $this->_columnMap = $columnMap;
       
    67     }
       
    68 
       
    69     /**
       
    70      * Create a new instance of Zend_Log_Writer_Db
       
    71      *
       
    72      * @param  array|Zend_Config $config
       
    73      * @return Zend_Log_Writer_Db
       
    74      * @throws Zend_Log_Exception
       
    75      */
       
    76     static public function factory($config)
       
    77     {
       
    78         $config = self::_parseConfig($config);
       
    79         $config = array_merge(array(
       
    80             'db'        => null,
       
    81             'table'     => null,
       
    82             'columnMap' => null,
       
    83         ), $config);
       
    84 
       
    85         if (isset($config['columnmap'])) {
       
    86             $config['columnMap'] = $config['columnmap'];
       
    87         }
       
    88 
       
    89         return new self(
       
    90             $config['db'],
       
    91             $config['table'],
       
    92             $config['columnMap']
       
    93         );
       
    94     }
       
    95 
       
    96     /**
       
    97      * Formatting is not possible on this writer
       
    98      */
       
    99     public function setFormatter(Zend_Log_Formatter_Interface $formatter)
       
   100     {
       
   101         require_once 'Zend/Log/Exception.php';
       
   102         throw new Zend_Log_Exception(get_class($this) . ' does not support formatting');
       
   103     }
       
   104 
       
   105     /**
       
   106      * Remove reference to database adapter
       
   107      *
       
   108      * @return void
       
   109      */
       
   110     public function shutdown()
       
   111     {
       
   112         $this->_db = null;
       
   113     }
       
   114 
       
   115     /**
       
   116      * Write a message to the log.
       
   117      *
       
   118      * @param  array  $event  event data
       
   119      * @return void
       
   120      */
       
   121     protected function _write($event)
       
   122     {
       
   123         if ($this->_db === null) {
       
   124             require_once 'Zend/Log/Exception.php';
       
   125             throw new Zend_Log_Exception('Database adapter is null');
       
   126         }
       
   127 
       
   128         if ($this->_columnMap === null) {
       
   129             $dataToInsert = $event;
       
   130         } else {
       
   131             $dataToInsert = array();
       
   132             foreach ($this->_columnMap as $columnName => $fieldKey) {
       
   133                 $dataToInsert[$columnName] = $event[$fieldKey];
       
   134             }
       
   135         }
       
   136 
       
   137         $this->_db->insert($this->_table, $dataToInsert);
       
   138     }
       
   139 }