Source of file Xml.php
Size: 5,354 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Log/Formatter/Xml.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Log\Formatter; use DateTime; use DOMDocument; use DOMElement; use Traversable; use Zend\Escaper\Escaper; use Zend\Stdlib\ArrayUtils; class Xml implements FormatterInterface { /** * @var string Name of root element */ protected $rootElement; /** * @var array Relates XML elements to log data field keys. */ protected $elementMap; /** * @var string Encoding to use in XML */ protected $encoding; /** * @var Escaper instance */ protected $escaper; /** * Format specifier for DateTime objects in event data (default: ISO 8601) * * @see http://php.net/manual/en/function.date.php * @var string */ protected $dateTimeFormat = self::DEFAULT_DATETIME_FORMAT; /** * Class constructor * (the default encoding is UTF-8) * * @param array|Traversable $options * @return Xml */ public function __construct($options = array()) { if ($options instanceof Traversable) { $options = ArrayUtils::iteratorToArray($options); } if (!is_array($options)) { $args = func_get_args(); $options = array( 'rootElement' => array_shift($args) ); if (count($args)) { $options['elementMap'] = array_shift($args); } if (count($args)) { $options['encoding'] = array_shift($args); } if (count($args)) { $options['dateTimeFormat'] = array_shift($args); } } if (!array_key_exists('rootElement', $options)) { $options['rootElement'] = 'logEntry'; } if (!array_key_exists('encoding', $options)) { $options['encoding'] = 'UTF-8'; } $this->rootElement = $options['rootElement']; $this->setEncoding($options['encoding']); if (array_key_exists('elementMap', $options)) { $this->elementMap = $options['elementMap']; } if (array_key_exists('dateTimeFormat', $options)) { $this->setDateTimeFormat($options['dateTimeFormat']); } } /** * Get encoding * * @return string */ public function getEncoding() { return $this->encoding; } /** * Set encoding * * @param string $value * @return Xml */ public function setEncoding($value) { $this->encoding = (string) $value; return $this; } /** * Set Escaper instance * * @param Escaper $escaper * @return Xml */ public function setEscaper(Escaper $escaper) { $this->escaper = $escaper; return $this; } /** * Get Escaper instance * * Lazy-loads an instance with the current encoding if none registered. * * @return Escaper */ public function getEscaper() { if (null === $this->escaper) { $this->setEscaper(new Escaper($this->getEncoding())); } return $this->escaper; } /** * Formats data into a single line to be written by the writer. * * @param array $event event data * @return string formatted line to write to the log */ public function format($event) { if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTime) { $event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat()); } if ($this->elementMap === null) { $dataToInsert = $event; } else { $dataToInsert = array(); foreach ($this->elementMap as $elementName => $fieldKey) { $dataToInsert[$elementName] = $event[$fieldKey]; } } $enc = $this->getEncoding(); $escaper = $this->getEscaper(); $dom = new DOMDocument('1.0', $enc); $elt = $dom->appendChild(new DOMElement($this->rootElement)); foreach ($dataToInsert as $key => $value) { if (empty($value) || is_scalar($value) || (is_object($value) && method_exists($value, '__toString')) ) { if ($key == "message") { $value = $escaper->escapeHtml($value); } elseif ($key == "extra" && empty($value)) { continue; } $elt->appendChild(new DOMElement($key, (string) $value)); } } $xml = $dom->saveXML(); $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml); return $xml . PHP_EOL; } /** * {@inheritDoc} */ public function getDateTimeFormat() { return $this->dateTimeFormat; } /** * {@inheritDoc} */ public function setDateTimeFormat($dateTimeFormat) { $this->dateTimeFormat = (string) $dateTimeFormat; return $this; } } |