/Zend/Log/Writer/Db.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 145 lines · 58 code · 14 blank · 73 comment · 4 complexity · 0020914ef0244260b0a56189866aa02e 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-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. * @namespace
  24. */
  25. namespace Zend\Log\Writer;
  26. use Zend\Log;
  27. /** Zend_Log_Writer_Abstract */
  28. require_once 'Zend/Log/Writer/Abstract.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Log
  32. * @subpackage Writer
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @version $Id: Db.php 22513 2010-07-01 13:48:39Z ramon $
  36. */
  37. class Db extends AbstractWriter
  38. {
  39. /**
  40. * Database adapter instance
  41. * @var Zend_Db_Adapter
  42. */
  43. private $_db;
  44. /**
  45. * Name of the log table in the database
  46. * @var string
  47. */
  48. private $_table;
  49. /**
  50. * Relates database columns names to log data field keys.
  51. *
  52. * @var null|array
  53. */
  54. private $_columnMap;
  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. * Create a new instance of Zend_Log_Writer_Db
  70. *
  71. * @param array|Zend_Config $config
  72. * @return Zend_Log_Writer_Db
  73. * @throws Zend_Log_Exception
  74. */
  75. static public function factory($config)
  76. {
  77. $config = self::_parseConfig($config);
  78. $config = array_merge(array(
  79. 'db' => null,
  80. 'table' => null,
  81. 'columnMap' => null,
  82. ), $config);
  83. if (isset($config['columnmap'])) {
  84. $config['columnMap'] = $config['columnmap'];
  85. }
  86. return new self(
  87. $config['db'],
  88. $config['table'],
  89. $config['columnMap']
  90. );
  91. }
  92. /**
  93. * Formatting is not possible on this writer
  94. */
  95. public function setFormatter(Log\Formatter\FormatterInterface $formatter)
  96. {
  97. require_once 'Zend/Log/Exception.php';
  98. throw new Log\Exception(get_class($this) . ' does not support formatting');
  99. }
  100. /**
  101. * Remove reference to database adapter
  102. *
  103. * @return void
  104. */
  105. public function shutdown()
  106. {
  107. $this->_db = null;
  108. }
  109. /**
  110. * Write a message to the log.
  111. *
  112. * @param array $event event data
  113. * @return void
  114. */
  115. protected function _write($event)
  116. {
  117. if ($this->_db === null) {
  118. require_once 'Zend/Log/Exception.php';
  119. throw new Log\Exception('Database adapter is null');
  120. }
  121. if ($this->_columnMap === null) {
  122. $dataToInsert = $event;
  123. } else {
  124. $dataToInsert = array();
  125. foreach ($this->_columnMap as $columnName => $fieldKey) {
  126. $dataToInsert[$columnName] = $event[$fieldKey];
  127. }
  128. }
  129. $this->_db->insert($this->_table, $dataToInsert);
  130. }
  131. }