Xml.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 Formatter
  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$
  21. */
  22. /** Zend_Log_Formatter_Interface */
  23. require_once 'Zend/Log/Formatter/Interface.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Log
  27. * @subpackage Formatter
  28. * @copyright Copyright (c) 2005-2010 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_Formatter_Xml implements Zend_Log_Formatter_Interface
  33. {
  34. /**
  35. * @var string Name of root element
  36. */
  37. protected $_rootElement;
  38. /**
  39. * @var array Relates XML elements to log data field keys.
  40. */
  41. protected $_elementMap;
  42. /**
  43. * @var string Encoding to use in XML
  44. */
  45. protected $_encoding;
  46. /**
  47. * Class constructor
  48. *
  49. * @param string $rootElement Name of root element
  50. * @param array $elementMap Relates XML elements to log data field keys
  51. * @param string $encoding Encoding to use (defaults to UTF-8)
  52. * @return void
  53. */
  54. public function __construct($rootElement = 'logEntry', $elementMap = null, $encoding = 'UTF-8')
  55. {
  56. $this->_rootElement = $rootElement;
  57. $this->_elementMap = $elementMap;
  58. $this->setEncoding($encoding);
  59. }
  60. /**
  61. * Get encoding
  62. *
  63. * @return string
  64. */
  65. public function getEncoding()
  66. {
  67. return $this->_encoding;
  68. }
  69. /**
  70. * Set encoding
  71. *
  72. * @param string $value
  73. * @return Zend_Log_Formatter_Xml
  74. */
  75. public function setEncoding($value)
  76. {
  77. $this->_encoding = (string) $value;
  78. return $this;
  79. }
  80. /**
  81. * Formats data into a single line to be written by the writer.
  82. *
  83. * @param array $event event data
  84. * @return string formatted line to write to the log
  85. */
  86. public function format($event)
  87. {
  88. if ($this->_elementMap === null) {
  89. $dataToInsert = $event;
  90. } else {
  91. $dataToInsert = array();
  92. foreach ($this->_elementMap as $elementName => $fieldKey) {
  93. $dataToInsert[$elementName] = $event[$fieldKey];
  94. }
  95. }
  96. $enc = $this->getEncoding();
  97. $dom = new DOMDocument('1.0', $enc);
  98. $elt = $dom->appendChild(new DOMElement($this->_rootElement));
  99. foreach ($dataToInsert as $key => $value) {
  100. if($key == "message") {
  101. $value = htmlspecialchars($value, ENT_COMPAT, $enc);
  102. }
  103. $elt->appendChild(new DOMElement($key, $value));
  104. }
  105. $xml = $dom->saveXML();
  106. $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
  107. return $xml . PHP_EOL;
  108. }
  109. }