PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

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

https://bitbucket.org/hieronim1981/tunethemusic
PHP | 165 lines | 77 code | 20 blank | 68 comment | 14 complexity | 971d4aef310a3bafe86b9ea23294262f 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 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 24593 2012-01-05 20:35:02Z matthew $
 21 */
 22
 23/** Zend_Log_Formatter_Abstract */
 24require_once 'Zend/Log/Formatter/Abstract.php';
 25
 26/**
 27 * @category   Zend
 28 * @package    Zend_Log
 29 * @subpackage Formatter
 30 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 31 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 32 * @version    $Id: Xml.php 24593 2012-01-05 20:35:02Z matthew $
 33 */
 34class Zend_Log_Formatter_Xml extends Zend_Log_Formatter_Abstract
 35{
 36    /**
 37     * @var string Name of root element
 38     */
 39    protected $_rootElement;
 40
 41    /**
 42     * @var array Relates XML elements to log data field keys.
 43     */
 44    protected $_elementMap;
 45
 46    /**
 47     * @var string Encoding to use in XML
 48     */
 49    protected $_encoding;
 50
 51    /**
 52     * Class constructor
 53     * (the default encoding is UTF-8)
 54     *
 55     * @param array|Zend_Config $options
 56     * @return void
 57     */
 58    public function __construct($options = array())
 59    {
 60        if ($options instanceof Zend_Config) {
 61            $options = $options->toArray();
 62        } elseif (!is_array($options)) {
 63            $args = func_get_args();
 64
 65            $options = array(
 66            	'rootElement' => array_shift($args)
 67            );
 68
 69            if (count($args)) {
 70                $options['elementMap'] = array_shift($args);
 71            }
 72
 73            if (count($args)) {
 74                $options['encoding'] = array_shift($args);
 75            }
 76        }
 77
 78        if (!array_key_exists('rootElement', $options)) {
 79            $options['rootElement'] = 'logEntry';
 80        }
 81
 82        if (!array_key_exists('encoding', $options)) {
 83            $options['encoding'] = 'UTF-8';
 84        }
 85
 86        $this->_rootElement = $options['rootElement'];
 87        $this->setEncoding($options['encoding']);
 88
 89        if (array_key_exists('elementMap', $options)) {
 90            $this->_elementMap  = $options['elementMap'];
 91        }
 92    }
 93
 94    /**
 95	 * Factory for Zend_Log_Formatter_Xml classe
 96	 *
 97	 * @param array|Zend_Config $options
 98	 * @return Zend_Log_Formatter_Xml
 99     */
100    public static function factory($options)
101    {
102        return new self($options);
103    }
104
105    /**
106     * Get encoding
107     *
108     * @return string
109     */
110    public function getEncoding()
111    {
112        return $this->_encoding;
113    }
114
115    /**
116     * Set encoding
117     *
118     * @param  string $value
119     * @return Zend_Log_Formatter_Xml
120     */
121    public function setEncoding($value)
122    {
123        $this->_encoding = (string) $value;
124        return $this;
125    }
126
127    /**
128     * Formats data into a single line to be written by the writer.
129     *
130     * @param  array    $event    event data
131     * @return string             formatted line to write to the log
132     */
133    public function format($event)
134    {
135        if ($this->_elementMap === null) {
136            $dataToInsert = $event;
137        } else {
138            $dataToInsert = array();
139            foreach ($this->_elementMap as $elementName => $fieldKey) {
140                $dataToInsert[$elementName] = $event[$fieldKey];
141            }
142        }
143
144        $enc = $this->getEncoding();
145        $dom = new DOMDocument('1.0', $enc);
146        $elt = $dom->appendChild(new DOMElement($this->_rootElement));
147
148        foreach ($dataToInsert as $key => $value) {
149            if (empty($value) 
150                || is_scalar($value) 
151                || (is_object($value) && method_exists($value,'__toString'))
152            ) {
153                if($key == "message") {
154                    $value = htmlspecialchars($value, ENT_COMPAT, $enc);
155                }
156                $elt->appendChild(new DOMElement($key, (string)$value));
157            }
158        }
159
160        $xml = $dom->saveXML();
161        $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
162
163        return $xml . PHP_EOL;
164    }
165}