File indexing completed on 2024-06-16 05:30:12

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_Log
0017  * @subpackage Writer
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 /** Zend_Log_Writer_Abstract */
0024 // require_once 'Zend/Log/Writer/Abstract.php';
0025 
0026 /**
0027  * @category   Zend
0028  * @package    Zend_Log
0029  * @subpackage Writer
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  * @version    $Id$
0033  */
0034 class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract
0035 {
0036     /**
0037      * Database adapter instance
0038      *
0039      * @var Zend_Db_Adapter
0040      */
0041     protected $_db;
0042 
0043     /**
0044      * Name of the log table in the database
0045      *
0046      * @var string
0047      */
0048     protected $_table;
0049 
0050     /**
0051      * Relates database columns names to log data field keys.
0052      *
0053      * @var null|array
0054      */
0055     protected $_columnMap;
0056 
0057     /**
0058      * Class constructor
0059      *
0060      * @param Zend_Db_Adapter $db   Database adapter instance
0061      * @param string $table         Log table in database
0062      * @param array $columnMap
0063      * @return void
0064      */
0065     public function __construct($db, $table, $columnMap = null)
0066     {
0067         $this->_db    = $db;
0068         $this->_table = $table;
0069         $this->_columnMap = $columnMap;
0070     }
0071 
0072     /**
0073      * Create a new instance of Zend_Log_Writer_Db
0074      *
0075      * @param  array|Zend_Config $config
0076      * @return Zend_Log_Writer_Db
0077      */
0078     static public function factory($config)
0079     {
0080         $config = self::_parseConfig($config);
0081         $config = array_merge(array(
0082             'db'        => null,
0083             'table'     => null,
0084             'columnMap' => null,
0085         ), $config);
0086 
0087         if (isset($config['columnmap'])) {
0088             $config['columnMap'] = $config['columnmap'];
0089         }
0090 
0091         return new self(
0092             $config['db'],
0093             $config['table'],
0094             $config['columnMap']
0095         );
0096     }
0097 
0098     /**
0099      * Formatting is not possible on this writer
0100      *
0101      * @return void
0102      * @throws Zend_Log_Exception
0103      */
0104     public function setFormatter(Zend_Log_Formatter_Interface $formatter)
0105     {
0106         // require_once 'Zend/Log/Exception.php';
0107         throw new Zend_Log_Exception(get_class($this) . ' does not support formatting');
0108     }
0109 
0110     /**
0111      * Remove reference to database adapter
0112      *
0113      * @return void
0114      */
0115     public function shutdown()
0116     {
0117         $this->_db = null;
0118     }
0119 
0120     /**
0121      * Write a message to the log.
0122      *
0123      * @param  array  $event  event data
0124      * @return void
0125      * @throws Zend_Log_Exception
0126      */
0127     protected function _write($event)
0128     {
0129         if ($this->_db === null) {
0130             // require_once 'Zend/Log/Exception.php';
0131             throw new Zend_Log_Exception('Database adapter is null');
0132         }
0133 
0134         if ($this->_columnMap === null) {
0135             $dataToInsert = $event;
0136         } else {
0137             $dataToInsert = array();
0138             foreach ($this->_columnMap as $columnName => $fieldKey) {
0139                 if (isset($event[$fieldKey])) {
0140                     $dataToInsert[$columnName] = $event[$fieldKey];
0141                 }
0142             }
0143         }
0144 
0145         $this->_db->insert($this->_table, $dataToInsert);
0146     }
0147 }