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.11 2008/07/27 14:17:44 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.8.0
  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['functions']);
  208.         unset($info['extensions']);
  209.         unset($info['constants']);
  210.         unset($info['tokens']);
  211.         unset($info['cond_code']);
  212.  
  213.         $ignored       $info['ignored_files'];
  214.         $all_functions = array();
  215.  
  216.         unset($info['ignored_files']);
  217.         unset($info['ignored_functions']);
  218.         unset($info['ignored_extensions']);
  219.         unset($info['ignored_constants']);
  220.  
  221.         // summarize : print only summary for directory without files details
  222.         if ($this->args['summarize'=== false && isset($dir)) {
  223.  
  224.             foreach ($info as $file => $info{
  225.                 if (is_numeric($file{0})) {
  226.                     // extra information available only when debug mode is on
  227.                     $all_functions[$file$info;
  228.                     continue;
  229.                 }
  230.                 if ($info === false{
  231.                     continue;  // skip this (invalid) file
  232.                 }
  233.                 $ext   implode("\r\n"$info['extensions']);
  234.                 $const implode("\r\n"array_merge($info['constants'],
  235.                                                      $info['tokens']));
  236.  
  237.                 $file str_replace(array('\\''/')$ds$file);
  238.                 $table->addSeparator();
  239.  
  240.                 $data = array($file);
  241.                 if ($o 16{
  242.                     if (empty($info['max_version'])) {
  243.                         $data[$info['version'];
  244.                     else {
  245.                         $data[implode("\r\n"array($info['version'],
  246.                                                         $info['max_version']));
  247.                     }
  248.                 }
  249.                 if ($o 1{
  250.                     $data[$info['cond_code'][0];
  251.                 }
  252.                 if ($o 2{
  253.                     $data[$ext;
  254.                 }
  255.                 if ($o 4{
  256.                     if ($o 8{
  257.                         $data[$const;
  258.                     else {
  259.                         $data[implode("\r\n"$info['constants']);
  260.                     }
  261.                 else {
  262.                     if ($o 8{
  263.                         $data[implode("\r\n"$info['tokens']);
  264.                     }
  265.                 }
  266.  
  267.                 $table->addRow($data);
  268.             }
  269.         else {
  270.             foreach ($info as $file => $info{
  271.                 if (is_numeric($file{0})) {
  272.                     // extra information available only when debug mode is on
  273.                     $all_functions[$file$info;
  274.                 }
  275.             }
  276.         }
  277.  
  278.         $output $table->getTable();
  279.  
  280.         // verbose level
  281.         $v = isset($this->args['verbose']$this->args['verbose': 0;
  282.  
  283.         // command line resume
  284.         if ($v 1{
  285.             $output .= "\nCommand Line resume :\n\n";
  286.  
  287.             $table = new Console_Table();
  288.             $table->setHeaders(array('Option''Value'));
  289.  
  290.             $filter0 = array(&$this'_splitOption');
  291.             $table->addFilter(0$filter0);
  292.             $filter1 = array(&$this'_splitValue');
  293.             $table->addFilter(1$filter1);
  294.  
  295.             if (is_array($this->args)) {
  296.                 foreach ($this->args as $key => $raw{
  297.                     if ($key == 'summarize'{
  298.                         $raw ($raw === true'TRUE' 'FALSE';
  299.                     }
  300.                     if (is_array($raw)) {
  301.                         $raw implode(', '$raw);
  302.                     }
  303.                     $contents = array($key$raw);
  304.                     $table->addRow($contents);
  305.                 }
  306.             }
  307.  
  308.             $output .= $table->getTable();
  309.         }
  310.  
  311.         // parser options resume
  312.         if ($v 2{
  313.             $output .= "\nParser options :\n\n";
  314.  
  315.             $table = new Console_Table();
  316.             $table->setHeaders(array('Option''Value'));
  317.  
  318.             $filter0 = array(&$this'_splitOption');
  319.             $table->addFilter(0$filter0);
  320.             $filter1 = array(&$this'_splitValue');
  321.             $table->addFilter(1$filter1);
  322.  
  323.             $opts $this->_parser->options;
  324.             if (is_array($opts)) {
  325.                 foreach ($opts as $key => $raw{
  326.                     if ($key == 'debug'
  327.                         || $key == 'recurse_dir'
  328.                         || $key == 'is_string'{
  329.                         $raw ($raw === true'TRUE' 'FALSE';
  330.                     }
  331.                     if (substr($key-6== '_match'{
  332.                         $val array_values($raw[1]);
  333.                         array_unshift($val$raw[0]);
  334.                         $raw implode("\r\n"$val);
  335.                     else {
  336.                         if (is_array($raw)) {
  337.                             $raw implode("\r\n"$raw);
  338.                         }
  339.                     }
  340.                     $contents = array($key$raw);
  341.                     $table->addRow($contents);
  342.                 }
  343.             }
  344.  
  345.             $output .= $table->getTable();
  346.         }
  347.  
  348.         // extra information
  349.         if ($v 4{
  350.             $output .= "\nDebug:\n\n";
  351.  
  352.             $table = new Console_Table();
  353.             $table->setHeaders(array('Version''Function''Extension''PECL'));
  354.  
  355.             foreach ($all_functions as $version => $functions{
  356.                 foreach ($functions as $func{
  357.                     $table->addRow(array($version,
  358.                         $func['function']$func['extension'],
  359.                         (isset($func['pecl']?
  360.                         (($func['pecl'=== true'yes' 'no''')));
  361.                 }
  362.             }
  363.  
  364.             $output .= $table->getTable();
  365.         }
  366.         echo $output;
  367.     }
  368.  
  369.     /**
  370.      * The Console_Table filter callback limits table output to 80 columns,
  371.      * and Path column to 29 characters
  372.      * (27 + 1 blank margin left + 1 blank margin right).
  373.      *
  374.      * @param string $data Content of filename column (0)
  375.      *
  376.      * @return string 
  377.      * @access private
  378.      * @since  1.3.0
  379.      */
  380.     function _splitFilename($data)
  381.     {
  382.         $w $this->conf['colwidth']['f'- 2;
  383.  
  384.         if (strlen($data<= $w{
  385.             $str str_pad($data$w);
  386.         else {
  387.             $str '...' substr($data(strlen($data($w - 3)));
  388.         }
  389.         return $str;
  390.     }
  391.  
  392.     /**
  393.      * The Console_Table filter callback limits table output to 80 columns,
  394.      * and Extensions column to 12 characters
  395.      * (10 + 1 blank margin left + 1 blank margin right).
  396.      *
  397.      * @param string $data Content of extensions column
  398.      *
  399.      * @return string 
  400.      * @access private
  401.      * @since  1.7.0
  402.      */
  403.     function _splitExtname($data)
  404.     {
  405.         $szlim  $this->conf['colwidth']['e'- 2;
  406.         $extArr explode("\r\n"$data);
  407.         $str    '';
  408.         foreach ($extArr as $ext{
  409.             if (strlen($ext<= $szlim{
  410.                 $str .= str_pad($ext$szlim);
  411.             else {
  412.                 $str .= '...' substr($ext(strlen($ext($szlim - 3)));
  413.             }
  414.             $str .= "\r\n";
  415.         }
  416.         $str rtrim($str"\r\n");
  417.         return $str;
  418.     }
  419.  
  420.     /**
  421.      * The Console_Table filter callback limits table output to 80 columns,
  422.      * and Constants/Tokens column to 20 characters
  423.      * (18 + 1 blank margin left + 1 blank margin right)
  424.      *
  425.      * @param string $data Content of constants/tokens column
  426.      *
  427.      * @return string 
  428.      * @access private
  429.      * @since  1.7.0
  430.      */
  431.     function _splitConstant($data)
  432.     {
  433.         $szlim  $this->conf['colwidth']['c'- 2;
  434.         $cstArr explode("\r\n"$data);
  435.         $str    '';
  436.         foreach ($cstArr as $cst{
  437.             if (strlen($cst<= $szlim{
  438.                 $str .= str_pad($cst$szlim);
  439.             else {
  440.                 $str .= '...' substr($cst(strlen($cst($szlim - 3)));
  441.             }
  442.             $str .= "\r\n";
  443.         }
  444.         $str rtrim($str"\r\n");
  445.         return $str;
  446.     }
  447.  
  448.     /**
  449.      * The Console_Table filter callback limits table output to 80 columns,
  450.      * and Command line Option column to 25 characters
  451.      * (23 + 1 blank margin left + 1 blank margin right).
  452.      *
  453.      * @param string $data Content of option column (0)
  454.      *
  455.      * @return string 
  456.      * @access private
  457.      * @since  1.7.0
  458.      */
  459.     function _splitOption($data)
  460.     {
  461.         if (strlen($data<= 23{
  462.             $str str_pad($data23);
  463.         else {
  464.             $str '...' substr($data(strlen($data- 20));
  465.         }
  466.         return $str;
  467.     }
  468.  
  469.     /**
  470.      * The Console_Table filter callback limits table output to 80 columns,
  471.      * and Command line Value column to 51 characters
  472.      * (49 + 1 blank margin left + 1 blank margin right)
  473.      *
  474.      * @param string $data Content of value column (1)
  475.      *
  476.      * @return string 
  477.      * @access private
  478.      * @since  1.7.0
  479.      */
  480.     function _splitValue($data)
  481.     {
  482.         $cstArr explode("\r\n"$data);
  483.         $str    '';
  484.         foreach ($cstArr as $cst{
  485.             if (strlen($cst<= 49{
  486.                 $str .= str_pad($cst49);
  487.             else {
  488.                 $str .= '...' substr($cst(strlen($cst- 46));
  489.             }
  490.             $str .= "\r\n";
  491.         }
  492.         $str rtrim($str"\r\n");
  493.         return $str;
  494.     }
  495. }
  496. ?>

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