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

Source for file Text.php

Documentation is available at Text.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: Text.php,v 1.12 2008/11/26 13:29:02 farell Exp $
  39.  * @link     http://pear.php.net/package/PHP_CompatInfo
  40.  * @since    File available since Release 1.8.0b3
  41.  */
  42.  
  43. require_once 'Console/Table.php';
  44.  
  45. /**
  46.  * Text renderer for PHP_CompatInfo component.
  47.  *
  48.  * The PHP_CompatInfo_Renderer_Text class is a concrete implementation
  49.  * of PHP_CompatInfo_Renderer abstract class. It simply display results
  50.  * for the command line interface with help of PEAR::Console_Table
  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.9.0b1
  57.  * @link     http://pear.php.net/package/PHP_CompatInfo
  58.  * @since    Class available since Release 1.8.0b3
  59.  */
  60. {
  61.     /**
  62.      * Text 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.0b3 (2008-06-07)
  69.      */
  70.     function PHP_CompatInfo_Renderer_Text(&$parser$conf)
  71.     {
  72.         $this->__construct($parser$conf);
  73.     }
  74.  
  75.     /**
  76.      * Text 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.0b3 (2008-06-07)
  83.      */
  84.     function __construct(&$parser$conf)
  85.     {
  86.         $defaults = array('colwidth' => array('f' => 29'e' => 12'c' => 20));
  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.0b3 (2008-06-07)
  100.      */
  101.     function display()
  102.     {
  103.         $o    $this->args['output-level'];
  104.         $info $this->parseData;
  105.         $hdr  = array();
  106.         $src  $this->_parser->dataSource;
  107.  
  108.         if (isset($this->args['dir'])) {
  109.             $dir   $this->args['dir'];
  110.             $hdr['Files';
  111.         elseif (isset($this->args['file'])) {
  112.             $file  $this->args['file'];
  113.             $hdr['File';
  114.         elseif (isset($this->args['string'])) {
  115.             $string $this->args['string'];
  116.             $hdr[]  'Source code';
  117.         elseif ($src['dataType'== 'directory'{
  118.             $dir   $src['dataSource'];
  119.             $hdr['Files';
  120.         elseif ($src['dataType'== 'file'{
  121.             $file  $src['dataSource'];
  122.             $hdr['File';
  123.         else {
  124.             $string $src['dataSource'];
  125.             $hdr[]  'Source code';
  126.         }
  127.  
  128.         $table = new Console_Table();
  129.         $f     = 0;
  130.         if ($o 16{
  131.             $hdr['Version';
  132.             $f++;
  133.         }
  134.         if ($o 1{
  135.             $hdr['C';
  136.             $f++;
  137.         }
  138.         if ($o 2{
  139.             $hdr[]   'Extensions';
  140.             $filter2 = array(&$this'_splitExtname');
  141.             $table->addFilter($f+1$filter2);
  142.             $f++;
  143.         }
  144.         if ($o 4{
  145.             if ($o 8{
  146.                 $hdr['Constants/Tokens';
  147.             else {
  148.                 $hdr['Constants';
  149.             }
  150.             $f++;
  151.         else {
  152.             if ($o 8{
  153.                 $hdr['Tokens';
  154.                 $f++;
  155.             }
  156.         }
  157.         $table->setHeaders($hdr);
  158.         $filter0 = array(&$this'_splitFilename');
  159.         $table->addFilter(0$filter0);
  160.         if ($o > 3 && $o < 16 || $o > 19{
  161.             $filter3 = array(&$this'_splitConstant');
  162.             $table->addFilter($f$filter3);
  163.         }
  164.  
  165.         $ext   implode("\r\n"$info['extensions']);
  166.         $const implode("\r\n"array_merge($info['constants']$info['tokens']));
  167.         if (isset($dir)) {
  168.             $ds   = DIRECTORY_SEPARATOR;
  169.             $dir  str_replace(array('\\''/')$ds$dir);
  170.             $data = array($dir $ds '*');
  171.         elseif (isset($file)) {
  172.             $data = array($file);
  173.         else {
  174.             $data = array('<?php ... ?>');
  175.         }
  176.  
  177.         if ($o 16{
  178.             if (empty($info['max_version'])) {
  179.                 $data[$info['version'];
  180.             else {
  181.                 $data[implode("\r\n"array($info['version'],
  182.                                                 $info['max_version']));
  183.             }
  184.         }
  185.         if ($o 1{
  186.             $data[$info['cond_code'][0];
  187.         }
  188.         if ($o 2{
  189.             $data[$ext;
  190.         }
  191.         if ($o 4{
  192.             if ($o 8{
  193.                 $data[$const;
  194.             else {
  195.                 $data[implode("\r\n"$info['constants']);
  196.             }
  197.         else {
  198.             if ($o 8{
  199.                 $data[implode("\r\n"$info['tokens']);
  200.             }
  201.         }
  202.  
  203.         $table->addRow($data);
  204.  
  205.         unset($info['max_version']);
  206.         unset($info['version']);
  207.         unset($info['classes']);
  208.         unset($info['functions']);
  209.         unset($info['extensions']);
  210.         unset($info['constants']);
  211.         unset($info['tokens']);
  212.         unset($info['cond_code']);
  213.  
  214.         $ignored       $info['ignored_files'];
  215.         $all_functions = array();
  216.  
  217.         unset($info['ignored_files']);
  218.         unset($info['ignored_functions']);
  219.         unset($info['ignored_extensions']);
  220.         unset($info['ignored_constants']);
  221.  
  222.         // summarize : print only summary for directory without files details
  223.         if ($this->args['summarize'=== false && isset($dir)) {
  224.  
  225.             foreach ($info as $file => $info{
  226.                 if (is_numeric($file{0})) {
  227.                     // extra information available only when debug mode is on
  228.                     $all_functions[$file$info;
  229.                     continue;
  230.                 }
  231.                 if ($info === false{
  232.                     continue;  // skip this (invalid) file
  233.                 }
  234.                 $ext   implode("\r\n"$info['extensions']);
  235.                 $const implode("\r\n"array_merge($info['constants'],
  236.                                                      $info['tokens']));
  237.  
  238.                 $file str_replace(array('\\''/')$ds$file);
  239.                 $table->addSeparator();
  240.  
  241.                 $data = array($file);
  242.                 if ($o 16{
  243.                     if (empty($info['max_version'])) {
  244.                         $data[$info['version'];
  245.                     else {
  246.                         $data[implode("\r\n"array($info['version'],
  247.                                                         $info['max_version']));
  248.                     }
  249.                 }
  250.                 if ($o 1{
  251.                     $data[$info['cond_code'][0];
  252.                 }
  253.                 if ($o 2{
  254.                     $data[$ext;
  255.                 }
  256.                 if ($o 4{
  257.                     if ($o 8{
  258.                         $data[$const;
  259.                     else {
  260.                         $data[implode("\r\n"$info['constants']);
  261.                     }
  262.                 else {
  263.                     if ($o 8{
  264.                         $data[implode("\r\n"$info['tokens']);
  265.                     }
  266.                 }
  267.  
  268.                 $table->addRow($data);
  269.             }
  270.         else {
  271.             foreach ($info as $file => $info{
  272.                 if (is_numeric($file{0})) {
  273.                     // extra information available only when debug mode is on
  274.                     $all_functions[$file$info;
  275.                 }
  276.             }
  277.         }
  278.  
  279.         $output $table->getTable();
  280.  
  281.         // verbose level
  282.         $v = isset($this->args['verbose']$this->args['verbose': 0;
  283.  
  284.         // command line resume
  285.         if ($v 1{
  286.             $output .= "\nCommand Line resume :\n\n";
  287.  
  288.             $table = new Console_Table();
  289.             $table->setHeaders(array('Option''Value'));
  290.  
  291.             $filter0 = array(&$this'_splitOption');
  292.             $table->addFilter(0$filter0);
  293.             $filter1 = array(&$this'_splitValue');
  294.             $table->addFilter(1$filter1);
  295.  
  296.             if (is_array($this->args)) {
  297.                 foreach ($this->args as $key => $raw{
  298.                     if ($key == 'summarize'{
  299.                         $raw ($raw === true'TRUE' 'FALSE';
  300.                     }
  301.                     if (is_array($raw)) {
  302.                         $raw implode(', '$raw);
  303.                     }
  304.                     $contents = array($key$raw);
  305.                     $table->addRow($contents);
  306.                 }
  307.             }
  308.  
  309.             $output .= $table->getTable();
  310.         }
  311.  
  312.         // parser options resume
  313.         if ($v 2{
  314.             $output .= "\nParser options :\n\n";
  315.  
  316.             $table = new Console_Table();
  317.             $table->setHeaders(array('Option''Value'));
  318.  
  319.             $filter0 = array(&$this'_splitOption');
  320.             $table->addFilter(0$filter0);
  321.             $filter1 = array(&$this'_splitValue');
  322.             $table->addFilter(1$filter1);
  323.  
  324.             $opts $this->_parser->options;
  325.             if (is_array($opts)) {
  326.                 foreach ($opts as $key => $raw{
  327.                     if ($key == 'debug'
  328.                         || $key == 'recurse_dir'
  329.                         || $key == 'is_string'{
  330.                         $raw ($raw === true'TRUE' 'FALSE';
  331.                     }
  332.                     if (substr($key-6== '_match'{
  333.                         $val array_values($raw[1]);
  334.                         array_unshift($val$raw[0]);
  335.                         $raw implode("\r\n"$val);
  336.                     else {
  337.                         if (is_array($raw)) {
  338.                             $raw implode("\r\n"$raw);
  339.                         }
  340.                     }
  341.                     $contents = array($key$raw);
  342.                     $table->addRow($contents);
  343.                 }
  344.             }
  345.  
  346.             $output .= $table->getTable();
  347.         }
  348.  
  349.         // extra information
  350.         if ($v 4{
  351.             $output .= "\nDebug:\n\n";
  352.  
  353.             $table = new Console_Table();
  354.             $table->setHeaders(array('Version''Function''Extension''PECL'));
  355.  
  356.             foreach ($all_functions as $version => $functions{
  357.                 foreach ($functions as $func{
  358.                     $table->addRow(array($version,
  359.                         $func['function']$func['extension'],
  360.                         (isset($func['pecl']?
  361.                         (($func['pecl'=== true'yes' 'no''')));
  362.                 }
  363.             }
  364.  
  365.             $output .= $table->getTable();
  366.         }
  367.         echo $output;
  368.     }
  369.  
  370.     /**
  371.      * The Console_Table filter callback limits table output to 80 columns,
  372.      * and Path column to 29 characters
  373.      * (27 + 1 blank margin left + 1 blank margin right).
  374.      *
  375.      * @param string $data Content of filename column (0)
  376.      *
  377.      * @return string 
  378.      * @access private
  379.      * @since  1.3.0
  380.      */
  381.     function _splitFilename($data)
  382.     {
  383.         $w $this->conf['colwidth']['f'- 2;
  384.  
  385.         if (strlen($data<= $w{
  386.             $str str_pad($data$w);
  387.         else {
  388.             $str '...' substr($data(strlen($data($w - 3)));
  389.         }
  390.         return $str;
  391.     }
  392.  
  393.     /**
  394.      * The Console_Table filter callback limits table output to 80 columns,
  395.      * and Extensions column to 12 characters
  396.      * (10 + 1 blank margin left + 1 blank margin right).
  397.      *
  398.      * @param string $data Content of extensions column
  399.      *
  400.      * @return string 
  401.      * @access private
  402.      * @since  1.7.0
  403.      */
  404.     function _splitExtname($data)
  405.     {
  406.         $szlim  $this->conf['colwidth']['e'- 2;
  407.         $extArr explode("\r\n"$data);
  408.         $str    '';
  409.         foreach ($extArr as $ext{
  410.             if (strlen($ext<= $szlim{
  411.                 $str .= str_pad($ext$szlim);
  412.             else {
  413.                 $str .= '...' substr($ext(strlen($ext($szlim - 3)));
  414.             }
  415.             $str .= "\r\n";
  416.         }
  417.         $str rtrim($str"\r\n");
  418.         return $str;
  419.     }
  420.  
  421.     /**
  422.      * The Console_Table filter callback limits table output to 80 columns,
  423.      * and Constants/Tokens column to 20 characters
  424.      * (18 + 1 blank margin left + 1 blank margin right)
  425.      *
  426.      * @param string $data Content of constants/tokens column
  427.      *
  428.      * @return string 
  429.      * @access private
  430.      * @since  1.7.0
  431.      */
  432.     function _splitConstant($data)
  433.     {
  434.         $szlim  $this->conf['colwidth']['c'- 2;
  435.         $cstArr explode("\r\n"$data);
  436.         $str    '';
  437.         foreach ($cstArr as $cst{
  438.             if (strlen($cst<= $szlim{
  439.                 $str .= str_pad($cst$szlim);
  440.             else {
  441.                 $str .= '...' substr($cst(strlen($cst($szlim - 3)));
  442.             }
  443.             $str .= "\r\n";
  444.         }
  445.         $str rtrim($str"\r\n");
  446.         return $str;
  447.     }
  448.  
  449.     /**
  450.      * The Console_Table filter callback limits table output to 80 columns,
  451.      * and Command line Option column to 25 characters
  452.      * (23 + 1 blank margin left + 1 blank margin right).
  453.      *
  454.      * @param string $data Content of option column (0)
  455.      *
  456.      * @return string 
  457.      * @access private
  458.      * @since  1.7.0
  459.      */
  460.     function _splitOption($data)
  461.     {
  462.         if (strlen($data<= 23{
  463.             $str str_pad($data23);
  464.         else {
  465.             $str '...' substr($data(strlen($data- 20));
  466.         }
  467.         return $str;
  468.     }
  469.  
  470.     /**
  471.      * The Console_Table filter callback limits table output to 80 columns,
  472.      * and Command line Value column to 51 characters
  473.      * (49 + 1 blank margin left + 1 blank margin right)
  474.      *
  475.      * @param string $data Content of value column (1)
  476.      *
  477.      * @return string 
  478.      * @access private
  479.      * @since  1.7.0
  480.      */
  481.     function _splitValue($data)
  482.     {
  483.         $cstArr explode("\r\n"$data);
  484.         $str    '';
  485.         foreach ($cstArr as $cst{
  486.             if (strlen($cst<= 49{
  487.                 $str .= str_pad($cst49);
  488.             else {
  489.                 $str .= '...' substr($cst(strlen($cst- 46));
  490.             }
  491.             $str .= "\r\n";
  492.         }
  493.         $str rtrim($str"\r\n");
  494.         return $str;
  495.     }
  496. }
  497. ?>

Documentation generated on Sun, 30 Nov 2008 16:30:35 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.