web/lib/Zend/Log/Formatter/Xml.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Log
    16  * @package    Zend_Log
    17  * @subpackage Formatter
    17  * @subpackage Formatter
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    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
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Xml.php 20104 2010-01-06 21:26:01Z matthew $
    20  * @version    $Id: Xml.php 24593 2012-01-05 20:35:02Z matthew $
    21  */
    21  */
    22 
    22 
    23 /** Zend_Log_Formatter_Interface */
    23 /** Zend_Log_Formatter_Abstract */
    24 require_once 'Zend/Log/Formatter/Interface.php';
    24 require_once 'Zend/Log/Formatter/Abstract.php';
    25 
    25 
    26 /**
    26 /**
    27  * @category   Zend
    27  * @category   Zend
    28  * @package    Zend_Log
    28  * @package    Zend_Log
    29  * @subpackage Formatter
    29  * @subpackage Formatter
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    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
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    32  * @version    $Id: Xml.php 20104 2010-01-06 21:26:01Z matthew $
    32  * @version    $Id: Xml.php 24593 2012-01-05 20:35:02Z matthew $
    33  */
    33  */
    34 class Zend_Log_Formatter_Xml implements Zend_Log_Formatter_Interface
    34 class Zend_Log_Formatter_Xml extends Zend_Log_Formatter_Abstract
    35 {
    35 {
    36     /**
    36     /**
    37      * @var Relates XML elements to log data field keys.
    37      * @var string Name of root element
    38      */
    38      */
    39     protected $_rootElement;
    39     protected $_rootElement;
    40 
    40 
    41     /**
    41     /**
    42      * @var Relates XML elements to log data field keys.
    42      * @var array Relates XML elements to log data field keys.
    43      */
    43      */
    44     protected $_elementMap;
    44     protected $_elementMap;
    45 
    45 
    46     /**
    46     /**
    47      * @var string Encoding to use in XML
    47      * @var string Encoding to use in XML
    48      */
    48      */
    49     protected $_encoding;
    49     protected $_encoding;
    50 
    50 
    51     /**
    51     /**
    52      * Class constructor
    52      * Class constructor
       
    53      * (the default encoding is UTF-8)
    53      *
    54      *
    54      * @param string $rootElement Name of root element
    55      * @param array|Zend_Config $options
    55      * @param array $elementMap
    56      * @return void
    56      * @param string $encoding Encoding to use (defaults to UTF-8)
       
    57      */
    57      */
    58     public function __construct($rootElement = 'logEntry', $elementMap = null, $encoding = 'UTF-8')
    58     public function __construct($options = array())
    59     {
    59     {
    60         $this->_rootElement = $rootElement;
    60         if ($options instanceof Zend_Config) {
    61         $this->_elementMap  = $elementMap;
    61             $options = $options->toArray();
    62         $this->setEncoding($encoding);
    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);
    63     }
   103     }
    64 
   104 
    65     /**
   105     /**
    66      * Get encoding
   106      * Get encoding
    67      *
   107      *
   104         $enc = $this->getEncoding();
   144         $enc = $this->getEncoding();
   105         $dom = new DOMDocument('1.0', $enc);
   145         $dom = new DOMDocument('1.0', $enc);
   106         $elt = $dom->appendChild(new DOMElement($this->_rootElement));
   146         $elt = $dom->appendChild(new DOMElement($this->_rootElement));
   107 
   147 
   108         foreach ($dataToInsert as $key => $value) {
   148         foreach ($dataToInsert as $key => $value) {
   109             if($key == "message") {
   149             if (empty($value) 
   110                 $value = htmlspecialchars($value, ENT_COMPAT, $enc);
   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));
   111             }
   157             }
   112             $elt->appendChild(new DOMElement($key, $value));
       
   113         }
   158         }
   114 
   159 
   115         $xml = $dom->saveXML();
   160         $xml = $dom->saveXML();
   116         $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
   161         $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
   117 
   162 
   118         return $xml . PHP_EOL;
   163         return $xml . PHP_EOL;
   119     }
   164     }
   120 
       
   121 }
   165 }