PHP_CompatInfo
[ class tree: PHP_CompatInfo ] [ index: PHP_CompatInfo ] [ all elements ]

Source for file Xml.php

Documentation is available at Xml.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2008, Laurent Laville <pear@laurent-laville.org>
  4.  *
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  *     * Redistributions of source code must retain the above copyright
  12.  *       notice, this list of conditions and the following disclaimer.
  13.  *     * Redistributions in binary form must reproduce the above copyright
  14.  *       notice, this list of conditions and the following disclaimer in the
  15.  *       documentation and/or other materials provided with the distribution.
  16.  *     * Neither the name of the authors nor the names of its contributors
  17.  *       may be used to endorse or promote products derived from this software
  18.  *       without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  24.  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * PHP versions 4 and 5
  33.  *
  34.  * @category PHP
  35.  * @package  PHP_CompatInfo
  36.  * @author   Laurent Laville <pear@laurent-laville.org>
  37.  * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
  38.  * @version  CVS: $Id: Xml.php,v 1.11 2008/07/22 20:26:19 farell Exp $
  39.  * @link     http://pear.php.net/package/PHP_CompatInfo
  40.  * @since    File available since Release 1.8.0b2
  41.  */
  42.  
  43. require_once 'XML/Util.php';
  44.  
  45. /**
  46.  * Array renderer for PHP_CompatInfo component.
  47.  *
  48.  * The PHP_CompatInfo_Renderer_Xml class is a concrete implementation
  49.  * of PHP_CompatInfo_Renderer abstract class. It simply display results as
  50.  * an XML stream.
  51.  *
  52.  * @category PHP
  53.  * @package  PHP_CompatInfo
  54.  * @author   Laurent Laville <pear@laurent-laville.org>
  55.  * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
  56.  * @version  Release: 1.8.0
  57.  * @link     http://pear.php.net/package/PHP_CompatInfo
  58.  * @since    Class available since Release 1.8.0b2
  59.  */
  60. {
  61.     /**
  62.      * Xml Renderer Class constructor (ZE1) for PHP4
  63.      *
  64.      * @param object &$parser Instance of the parser (model of MVC pattern)
  65.      * @param array  $conf    A hash containing any additional configuration
  66.      *
  67.      * @access public
  68.      * @since  version 1.8.0b2 (2008-06-03)
  69.      */
  70.     function PHP_CompatInfo_Renderer_Xml(&$parser$conf)
  71.     {
  72.         $this->__construct($parser$conf);
  73.     }
  74.  
  75.     /**
  76.      * Xml Renderer Class constructor (ZE2) for PHP5+
  77.      *
  78.      * @param object &$parser Instance of the parser (model of MVC pattern)
  79.      * @param array  $conf    A hash containing any additional configuration
  80.      *
  81.      * @access public
  82.      * @since  version 1.8.0b2 (2008-06-03)
  83.      */
  84.     function __construct(&$parser$conf)
  85.     {
  86.         $defaults = array('use-beautifier' => 'auto');
  87.         $conf     array_merge($defaults$conf);
  88.  
  89.         parent::PHP_CompatInfo_Renderer($parser$conf);
  90.     }
  91.  
  92.     /**
  93.      * Display final results
  94.      *
  95.      * Display final results, when data source parsing is over.
  96.      *
  97.      * @access public
  98.      * @return void 
  99.      * @since  version 1.8.0b2 (2008-06-03)
  100.      */
  101.     function display()
  102.     {
  103.         if ($this->parseData === false{
  104.             // invalid data source
  105.             return;
  106.         }
  107.  
  108.         $version    = isset($this->conf['xml']['version'])
  109.                     ? $this->conf['xml']['version''1.0';
  110.         $encoding   = isset($this->conf['xml']['encoding'])
  111.                     ? $this->conf['xml']['encoding''UTF-8';
  112.         $standalone = isset($this->conf['xml']['standalone'])
  113.                     ? $this->conf['xml']['standalone': null;
  114.  
  115.         $msg  = XML_Util::getXMLDeclaration($version$encoding$standalone);
  116.         $msg .= PHP_EOL;
  117.         $msg .= XML_Util::createStartElement('pci',
  118.                                        array('version' => '1.8.0'));
  119.  
  120.         $o $this->args['output-level'];
  121.         $v $this->args['verbose'];
  122.  
  123.         $dataSource $this->_parser->dataSource['dataSource'];
  124.         $dataType   $this->_parser->dataSource['dataType'];
  125.         $options    $this->_parser->options;
  126.  
  127.         if ($dataType == 'directory'
  128.             || $dataType == 'array'
  129.             || $dataType == 'file'{
  130.             // parsing a directory or a list of files, chunks of code
  131.  
  132.             if ($options['is_string'== false{
  133.                 if ($dataType == 'directory'{
  134.                     // print <dir> tag
  135.                     $tag = array('qname' => 'dir',
  136.                                  'content' => dirname($dataSource[0]));
  137.                 else {
  138.                     // print <file> tag
  139.                     $tag = array('qname' => 'file',
  140.                                  'content' => $dataSource[0]);
  141.                 }
  142.                 $msg .= XML_Util::createTagFromArray($tag);
  143.                 $msg .= PHP_EOL;
  144.             }
  145.  
  146.             // print global <version> tag
  147.             if ($o 16{
  148.                 if (empty($this->parseData['max_version'])) {
  149.                     $attr = array();
  150.                 else {
  151.                     $attr = array('max' => $this->parseData['max_version']);
  152.                 }
  153.                 $tag  = array('qname' => 'version',
  154.                               'attributes' => $attr,
  155.                               'content' => $this->parseData['version']);
  156.                 $msg .= XML_Util::createTagFromArray($tag);
  157.                 $msg .= PHP_EOL;
  158.             }
  159.  
  160.             // print global <conditions> tag group
  161.             if ($o 1{
  162.                 $msg .= $this->_printTagList($this->parseData['cond_code'],
  163.                                              'condition');
  164.             }
  165.             // print global <extensions> tag group
  166.             if ($o 2{
  167.                 $msg .= $this->_printTagList($this->parseData['extensions'],
  168.                                              'extension');
  169.             }
  170.             // print global <constants> tag group
  171.             if ($o 4{
  172.                 $msg .= $this->_printTagList($this->parseData['constants'],
  173.                                              'constant');
  174.             }
  175.             // print global <tokens> tag group
  176.             if ($o 8{
  177.                 $msg .= $this->_printTagList($this->parseData['tokens'],
  178.                                              'token');
  179.             }
  180.  
  181.             // print global <ignored> tag group
  182.             $msg .= XML_Util::createStartElement('ignored');
  183.             $msg .= PHP_EOL;
  184.             // with children groups <files>, <functions>, <extensions>, <constants>
  185.             $ignored = array('file' => $this->parseData['ignored_files'],
  186.                              'function' => $this->parseData['ignored_functions'],
  187.                              'extension' => $this->parseData['ignored_extensions'],
  188.                              'constant' => $this->parseData['ignored_constants']);
  189.             foreach ($ignored as $tag => $data{
  190.                 $msg .= $this->_printTagList($data$tag);
  191.             }
  192.             $msg .= XML_Util::createEndElement('ignored');
  193.             $msg .= PHP_EOL;
  194.  
  195.             // remove summary data
  196.             unset($this->parseData['ignored_files']);
  197.             unset($this->parseData['ignored_functions']);
  198.             unset($this->parseData['ignored_extensions']);
  199.             unset($this->parseData['ignored_constants']);
  200.             unset($this->parseData['max_version']);
  201.             unset($this->parseData['version']);
  202.             unset($this->parseData['functions']);
  203.             unset($this->parseData['extensions']);
  204.             unset($this->parseData['constants']);
  205.             unset($this->parseData['tokens']);
  206.             unset($this->parseData['cond_code']);
  207.  
  208.             if ($v 4 || $options['debug'== true{
  209.                 // print local <functions> tag group
  210.                 $msg .= $this->_printTagList($this->parseData'function');
  211.  
  212.                 $entries array_keys($this->parseData);
  213.                 foreach ($entries as $k{
  214.                     if (is_numeric($k{0})) {
  215.                         unset($this->parseData[$k]);
  216.                     }
  217.                 }
  218.             }
  219.  
  220.             if ($dataType == 'file'{
  221.                 // parsing a single file
  222.                 $files = array($dataSource[0=> $this->parseData);
  223.             else {
  224.                 $files $this->parseData;
  225.             }
  226.         else {
  227.             // ... or a chunk of code (string)
  228.             $files = array($this->parseData);
  229.         }
  230.  
  231.         if ($this->args['summarize'=== false
  232.             && count($files> 1{
  233.  
  234.             if ($options['is_string'== false{
  235.                 // print <files> tag group
  236.                 $msg .= XML_Util::createStartElement('files',
  237.                                                array('count' => count($files)));
  238.                 $msg .= PHP_EOL;
  239.             }
  240.  
  241.             foreach ($files as $file => $this->parseData{
  242.                 if ($options['is_string'== true{
  243.                     $msg .= XML_Util::createStartElement('string',
  244.                                                    array('name' => $file));
  245.                 else {
  246.                     // print local <file> tag
  247.                     $msg .= XML_Util::createStartElement('file',
  248.                                                    array('name' => $file));
  249.                 }
  250.                 $msg .= PHP_EOL;
  251.  
  252.                 // print local <version> tag
  253.                 if ($o 16{
  254.                     if (empty($this->parseData['max_version'])) {
  255.                         $attr = array();
  256.                     else {
  257.                         $attr = array('max' => $this->parseData['max_version']);
  258.                     }
  259.                     $tag  = array('qname' => 'version',
  260.                                   'attributes' => $attr,
  261.                                   'content' => $this->parseData['version']);
  262.                     $msg .= XML_Util::createTagFromArray($tag);
  263.                     $msg .= PHP_EOL;
  264.                 }
  265.  
  266.                 // print local <conditions> tag group
  267.                 if ($o 1{
  268.                     $msg .= $this->_printTagList($this->parseData['cond_code'],
  269.                                                  'condition');
  270.                 }
  271.                 // print local <extensions> tag group
  272.                 if ($o 2{
  273.                     $msg .= $this->_printTagList($this->parseData['extensions'],
  274.                                                  'extension');
  275.                 }
  276.                 // print local <constants> tag group
  277.                 if ($o 4{
  278.                     $msg .= $this->_printTagList($this->parseData['constants'],
  279.                                                  'constant');
  280.                 }
  281.                 // print local <tokens> tag group
  282.                 if ($o 8{
  283.                     $msg .= $this->_printTagList($this->parseData['tokens'],
  284.                                                  'token');
  285.                 }
  286.  
  287.                 // print local <ignored> tag group
  288.                 $msg .= XML_Util::createStartElement('ignored');
  289.                 $msg .= PHP_EOL;
  290.                 // with children groups <functions>, <extensions>, <constants>
  291.                 $ignored = array(
  292.                     'function' => $this->parseData['ignored_functions'],
  293.                     'extension' => $this->parseData['ignored_extensions'],
  294.                     'constant' => $this->parseData['ignored_constants']
  295.                     );
  296.                 foreach ($ignored as $tag => $data{
  297.                     $msg .= $this->_printTagList($data$tag);
  298.                 }
  299.                 $msg .= XML_Util::createEndElement('ignored');
  300.                 $msg .= PHP_EOL;
  301.  
  302.                 // extra information only if verbose mode >= 4
  303.                 if ($v 4 || $options['debug'== true{
  304.                     unset($this->parseData['ignored_files']);
  305.                     unset($this->parseData['ignored_functions']);
  306.                     unset($this->parseData['ignored_extensions']);
  307.                     unset($this->parseData['ignored_constants']);
  308.                     unset($this->parseData['max_version']);
  309.                     unset($this->parseData['version']);
  310.                     unset($this->parseData['functions']);
  311.                     unset($this->parseData['extensions']);
  312.                     unset($this->parseData['constants']);
  313.                     unset($this->parseData['tokens']);
  314.                     unset($this->parseData['cond_code']);
  315.  
  316.                     // print local <functions> tag group
  317.                     $msg .= $this->_printTagList($this->parseData'function');
  318.                 }
  319.  
  320.                 if ($options['is_string'== true{
  321.                     $msg .= XML_Util::createEndElement('string');
  322.                 else {
  323.                     $msg .= XML_Util::createEndElement('file');
  324.                 }
  325.                 $msg .= PHP_EOL;
  326.             }
  327.             if ($options['is_string'== false{
  328.                 $msg .= XML_Util::createEndElement('files');
  329.                 $msg .= PHP_EOL;
  330.             }
  331.         }
  332.         $msg .= XML_Util::createEndElement('pci');
  333.         $msg .= PHP_EOL;
  334.  
  335.         if (strtolower($this->conf['use-beautifier']!= 'no'{
  336.             // try to see if we can improve XML render
  337.             $beautifier 'XML/Beautifier.php';
  338.             if (PHP_CompatInfo_Renderer::isIncludable($beautifier)) {
  339.                 include_once $beautifier;
  340.                 $def = array();
  341.                 $opt = isset($this->conf['beautifier'])
  342.                      ? $this->conf['beautifier'$def;
  343.                 $fmt = new XML_Beautifier($opt);
  344.                 $msg $fmt->formatString($msg);
  345.             }
  346.         }
  347.  
  348.         echo $msg;
  349.     }
  350.  
  351.     /**
  352.      * Print a group of same tag in the XML report.
  353.      *
  354.      * Groups list are : extension(s), constant(s), token(s)
  355.      *
  356.      * @param array  $dataSrc Data source
  357.      * @param string $tagName Name of the XML tag
  358.      *
  359.      * @return string 
  360.      * @access private
  361.      * @since  version 1.7.0b4 (2008-04-03)
  362.      */
  363.     function _printTagList($dataSrc$tagName)
  364.     {
  365.         $msg '';
  366.  
  367.         if ($tagName == 'function'{
  368.             $c = 0;
  369.             foreach ($dataSrc as $version => $functions{
  370.                 $c += count($functions);
  371.             }
  372.             $attributes = array('count' => $c);
  373.         elseif ($tagName == 'condition'{
  374.             if ($this->_parser->options['debug'=== true{
  375.                 $c = 0;
  376.                 foreach ($dataSrc[1as $cond => $elements{
  377.                     $c += count($elements);
  378.                 }
  379.                 $attributes = array('count' => $c'level' => $dataSrc[0]);
  380.             else {
  381.                 $attributes = array('level' => $dataSrc[0]);
  382.             }
  383.         else {
  384.             $attributes = array('count' => count($dataSrc));
  385.         }
  386.  
  387.         $msg .= XML_Util::createStartElement($tagName.'s'$attributes);
  388.         $msg .= PHP_EOL;
  389.  
  390.         if ($tagName == 'function'{
  391.             foreach ($dataSrc as $version => $functions{
  392.                 foreach ($functions as $data{
  393.                     $attr = array('version' => $version);
  394.                     if (!empty($data['extension'])) {
  395.                         $attr['extension'$data['extension'];
  396.                         $attr['pecl']      $data['pecl'=== true ?
  397.                                                 'true' 'false';
  398.                     }
  399.                     $tag  = array('qname' => $tagName,
  400.                                   'attributes' => $attr,
  401.                                   'content' => $data['function']);
  402.                     $msg .= XML_Util::createTagFromArray($tag);
  403.                     $msg .= PHP_EOL;
  404.                 }
  405.             }
  406.         elseif ($tagName == 'condition'{
  407.             if ($this->_parser->options['debug'== true{
  408.                 foreach ($dataSrc[1as $cond => $elements{
  409.                     $cond ($cond == 0? 1 : ($cond * 2);
  410.                     foreach ($elements as $data{
  411.                         $tag  = array('qname' => $tagName,
  412.                                       'attributes' => array('level' => $cond),
  413.                                       'content' => $data);
  414.                         $msg .= XML_Util::createTagFromArray($tag);
  415.                         $msg .= PHP_EOL;
  416.                     }
  417.                 }
  418.             }
  419.         else {
  420.             foreach ($dataSrc as $data{
  421.                 $tag  = array('qname' => $tagName,
  422.                               'attributes' => array(),
  423.                               'content' => $data);
  424.                 $msg .= XML_Util::createTagFromArray($tag);
  425.                 $msg .= PHP_EOL;
  426.             }
  427.         }
  428.  
  429.         $msg .= XML_Util::createEndElement($tagName.'s');
  430.         $msg .= PHP_EOL;
  431.  
  432.         return $msg;
  433.     }
  434. }
  435. ?>

Documentation generated on Fri, 01 Aug 2008 11:30:35 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.