PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Log/Writer/Db.php

https://bitbucket.org/netglue/zf-1.12-release
PHP | 147 lines | 58 code | 13 blank | 76 comment | 5 complexity | 08f8f777a5755e07ff8a274bdd023c7c 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$
  21. */
  22. /** Zend_Log_Writer_Abstract */
  23. require_once 'Zend/Log/Writer/Abstract.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Log
  27. * @subpackage Writer
  28. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id$
  31. */
  32. class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract
  33. {
  34. /**
  35. * Database adapter instance
  36. *
  37. * @var Zend_Db_Adapter
  38. */
  39. protected $_db;
  40. /**
  41. * Name of the log table in the database
  42. *
  43. * @var string
  44. */
  45. protected $_table;
  46. /**
  47. * Relates database columns names to log data field keys.
  48. *
  49. * @var null|array
  50. */
  51. protected $_columnMap;
  52. /**
  53. * Class constructor
  54. *
  55. * @param Zend_Db_Adapter $db Database adapter instance
  56. * @param string $table Log table in database
  57. * @param array $columnMap
  58. * @return void
  59. */
  60. public function __construct($db, $table, $columnMap = null)
  61. {
  62. $this->_db = $db;
  63. $this->_table = $table;
  64. $this->_columnMap = $columnMap;
  65. }
  66. /**
  67. * Create a new instance of Zend_Log_Writer_Db
  68. *
  69. * @param array|Zend_Config $config
  70. * @return Zend_Log_Writer_Db
  71. */
  72. static public function factory($config)
  73. {
  74. $config = self::_parseConfig($config);
  75. $config = array_merge(array(
  76. 'db' => null,
  77. 'table' => null,
  78. 'columnMap' => null,
  79. ), $config);
  80. if (isset($config['columnmap'])) {
  81. $config['columnMap'] = $config['columnmap'];
  82. }
  83. return new self(
  84. $config['db'],
  85. $config['table'],
  86. $config['columnMap']
  87. );
  88. }
  89. /**
  90. * Formatting is not possible on this writer
  91. *
  92. * @return void
  93. * @throws Zend_Log_Exception
  94. */
  95. public function setFormatter(Zend_Log_Formatter_Interface $formatter)
  96. {
  97. require_once 'Zend/Log/Exception.php';
  98. throw new Zend_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. * @throws Zend_Log_Exception
  115. */
  116. protected function _write($event)
  117. {
  118. if ($this->_db === null) {
  119. require_once 'Zend/Log/Exception.php';
  120. throw new Zend_Log_Exception('Database adapter is null');
  121. }
  122. if ($this->_columnMap === null) {
  123. $dataToInsert = $event;
  124. } else {
  125. $dataToInsert = array();
  126. foreach ($this->_columnMap as $columnName => $fieldKey) {
  127. if (isset($event[$fieldKey])) {
  128. $dataToInsert[$columnName] = $event[$fieldKey];
  129. }
  130. }
  131. }
  132. $this->_db->insert($this->_table, $dataToInsert);
  133. }
  134. }