PageRenderTime 17ms CodeModel.GetById 13ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 1ms

/Log/Writer/Db.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 145 lines | 56 code | 13 blank | 76 comment | 4 complexity | 43a76a5b0f0c007bc9057fa22b83fd17 MD5 | raw file
  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-2012 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 24593 2012-01-05 20:35:02Z matthew $
 21 */
 22
 23/** Zend_Log_Writer_Abstract */
 24require_once 'Zend/Log/Writer/Abstract.php';
 25
 26/**
 27 * @category   Zend
 28 * @package    Zend_Log
 29 * @subpackage Writer
 30 * @copyright  Copyright (c) 2005-2012 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 24593 2012-01-05 20:35:02Z matthew $
 33 */
 34class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract
 35{
 36    /**
 37     * Database adapter instance
 38     *
 39     * @var Zend_Db_Adapter
 40     */
 41    private $_db;
 42
 43    /**
 44     * Name of the log table in the database
 45     *
 46     * @var string
 47     */
 48    private $_table;
 49
 50    /**
 51     * Relates database columns names to log data field keys.
 52     *
 53     * @var null|array
 54     */
 55    private $_columnMap;
 56
 57    /**
 58     * Class constructor
 59     *
 60     * @param Zend_Db_Adapter $db   Database adapter instance
 61     * @param string $table         Log table in database
 62     * @param array $columnMap
 63     * @return void
 64     */
 65    public function __construct($db, $table, $columnMap = null)
 66    {
 67        $this->_db    = $db;
 68        $this->_table = $table;
 69        $this->_columnMap = $columnMap;
 70    }
 71
 72    /**
 73     * Create a new instance of Zend_Log_Writer_Db
 74     *
 75     * @param  array|Zend_Config $config
 76     * @return Zend_Log_Writer_Db
 77     */
 78    static public function factory($config)
 79    {
 80        $config = self::_parseConfig($config);
 81        $config = array_merge(array(
 82            'db'        => null,
 83            'table'     => null,
 84            'columnMap' => null,
 85        ), $config);
 86
 87        if (isset($config['columnmap'])) {
 88            $config['columnMap'] = $config['columnmap'];
 89        }
 90
 91        return new self(
 92            $config['db'],
 93            $config['table'],
 94            $config['columnMap']
 95        );
 96    }
 97
 98    /**
 99     * Formatting is not possible on this writer
100     *
101     * @return void
102     * @throws Zend_Log_Exception
103     */
104    public function setFormatter(Zend_Log_Formatter_Interface $formatter)
105    {
106        require_once 'Zend/Log/Exception.php';
107        throw new Zend_Log_Exception(get_class($this) . ' does not support formatting');
108    }
109
110    /**
111     * Remove reference to database adapter
112     *
113     * @return void
114     */
115    public function shutdown()
116    {
117        $this->_db = null;
118    }
119
120    /**
121     * Write a message to the log.
122     *
123     * @param  array  $event  event data
124     * @return void
125     * @throws Zend_Log_Exception
126     */
127    protected function _write($event)
128    {
129        if ($this->_db === null) {
130            require_once 'Zend/Log/Exception.php';
131            throw new Zend_Log_Exception('Database adapter is null');
132        }
133
134        if ($this->_columnMap === null) {
135            $dataToInsert = $event;
136        } else {
137            $dataToInsert = array();
138            foreach ($this->_columnMap as $columnName => $fieldKey) {
139                $dataToInsert[$columnName] = $event[$fieldKey];
140            }
141        }
142
143        $this->_db->insert($this->_table, $dataToInsert);
144    }
145}