PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Log/Formatter/Xml.php

https://bitbucket.org/ksekar/campus
PHP | 165 lines | 77 code | 20 blank | 68 comment | 14 complexity | 270360c944241308ce1aaf01014de7c4 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Xml.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. /** Zend_Log_Formatter_Abstract */
  23. require_once 'Zend/Log/Formatter/Abstract.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Log
  27. * @subpackage Formatter
  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: Xml.php 24594 2012-01-05 21:27:01Z matthew $
  31. */
  32. class Zend_Log_Formatter_Xml extends Zend_Log_Formatter_Abstract
  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. * (the default encoding is UTF-8)
  49. *
  50. * @param array|Zend_Config $options
  51. * @return void
  52. */
  53. public function __construct($options = array())
  54. {
  55. if ($options instanceof Zend_Config) {
  56. $options = $options->toArray();
  57. } elseif (!is_array($options)) {
  58. $args = func_get_args();
  59. $options = array(
  60. 'rootElement' => array_shift($args)
  61. );
  62. if (count($args)) {
  63. $options['elementMap'] = array_shift($args);
  64. }
  65. if (count($args)) {
  66. $options['encoding'] = array_shift($args);
  67. }
  68. }
  69. if (!array_key_exists('rootElement', $options)) {
  70. $options['rootElement'] = 'logEntry';
  71. }
  72. if (!array_key_exists('encoding', $options)) {
  73. $options['encoding'] = 'UTF-8';
  74. }
  75. $this->_rootElement = $options['rootElement'];
  76. $this->setEncoding($options['encoding']);
  77. if (array_key_exists('elementMap', $options)) {
  78. $this->_elementMap = $options['elementMap'];
  79. }
  80. }
  81. /**
  82. * Factory for Zend_Log_Formatter_Xml classe
  83. *
  84. * @param array|Zend_Config $options
  85. * @return Zend_Log_Formatter_Xml
  86. */
  87. public static function factory($options)
  88. {
  89. return new self($options);
  90. }
  91. /**
  92. * Get encoding
  93. *
  94. * @return string
  95. */
  96. public function getEncoding()
  97. {
  98. return $this->_encoding;
  99. }
  100. /**
  101. * Set encoding
  102. *
  103. * @param string $value
  104. * @return Zend_Log_Formatter_Xml
  105. */
  106. public function setEncoding($value)
  107. {
  108. $this->_encoding = (string) $value;
  109. return $this;
  110. }
  111. /**
  112. * Formats data into a single line to be written by the writer.
  113. *
  114. * @param array $event event data
  115. * @return string formatted line to write to the log
  116. */
  117. public function format($event)
  118. {
  119. if ($this->_elementMap === null) {
  120. $dataToInsert = $event;
  121. } else {
  122. $dataToInsert = array();
  123. foreach ($this->_elementMap as $elementName => $fieldKey) {
  124. $dataToInsert[$elementName] = $event[$fieldKey];
  125. }
  126. }
  127. $enc = $this->getEncoding();
  128. $dom = new DOMDocument('1.0', $enc);
  129. $elt = $dom->appendChild(new DOMElement($this->_rootElement));
  130. foreach ($dataToInsert as $key => $value) {
  131. if (empty($value)
  132. || is_scalar($value)
  133. || (is_object($value) && method_exists($value,'__toString'))
  134. ) {
  135. if($key == "message") {
  136. $value = htmlspecialchars($value, ENT_COMPAT, $enc);
  137. }
  138. $elt->appendChild(new DOMElement($key, (string)$value));
  139. }
  140. }
  141. $xml = $dom->saveXML();
  142. $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
  143. return $xml . PHP_EOL;
  144. }
  145. }