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

Source for file Cli.php

Documentation is available at Cli.php

  1. <?php
  2. /**
  3.  * CLI Script to generate text phpinfo() style PEAR information
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.01 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @category PEAR
  14.  * @package  PEAR_Info
  15.  * @author   Laurent Laville <pear@laurent-laville.org>
  16.  * @license  http://www.php.net/license/3_01.txt  PHP License 3.01
  17.  * @version  CVS: $Id: Cli.php,v 1.2 2008/03/28 14:25:10 farell Exp $
  18.  * @link     http://pear.php.net/package/PEAR_Info
  19.  * @since    File available since Release 1.8.0
  20.  */
  21.  
  22. require_once 'PEAR/Info.php';
  23. require_once 'Console/Getargs.php';
  24.  
  25. /**
  26.  * CLI Script to display information about your PEAR installation
  27.  *
  28.  * <code>
  29.  * <?php
  30.  *     require_once 'PEAR/Info/Cli.php';
  31.  *     $cli = new PEAR_Info_Cli();
  32.  *     $cli->run();
  33.  * ?>
  34.  * </code>
  35.  *
  36.  * @category PEAR
  37.  * @package  PEAR_Info
  38.  * @author   Laurent Laville <pear@laurent-laville.org>
  39.  * @license  http://www.php.net/license/3_01.txt  PHP License 3.01
  40.  * @version  Release: 1.8.0
  41.  * @link     http://pear.php.net/package/PEAR_Info
  42.  * @since    Class available since Release 1.8.0
  43.  */
  44.  
  45. class PEAR_Info_Cli extends PEAR_Info
  46. {
  47.     /**
  48.      * @var    array    Current CLI Flags
  49.      * @since  1.8.0
  50.      */
  51.     var $opts = array();
  52.  
  53.     /**
  54.      * @var    string   error message
  55.      * @since  1.8.0
  56.      */
  57.     var $error;
  58.  
  59.     /**
  60.      * @var    object   Console_Getargs instance
  61.      * @since  1.8.0
  62.      */
  63.     var $args;
  64.  
  65.  
  66.     /**
  67.      * ZE2 Constructor
  68.      *
  69.      * @since  1.8.0
  70.      */
  71.     function __construct()
  72.     {
  73.         $this->opts = array(
  74.             'php-dir' =>
  75.                 array('short' => 'pd',
  76.                       'desc'  => 'PEAR directory',
  77.                       'default' => '',
  78.                       'min'   => 0 'max' => 1),
  79.             'usr-cfg' =>
  80.                 array('short' => 'uc',
  81.                       'desc' => 'User Configuration File',
  82.                       'default' => '',
  83.                       'min'   => 0 'max' => 1),
  84.             'sys-cfg' =>
  85.                 array('short' => 'sc',
  86.                       'desc' => 'System Configuration File',
  87.                       'default' => '',
  88.                       'min'   => 0 'max' => 1),
  89.             'http-proxy' =>
  90.                 array('short' => 'hp',
  91.                       'desc' => 'HTTP Proxy Server Address',
  92.                       'default' => '',
  93.                       'min'   => 0 'max' => 1),
  94.             'all' =>
  95.                 array('short'   => 'a',
  96.                       'desc'    => 'Display informations for installed packages',
  97.                       'default' => PEAR_INFO_ALL,
  98.                       'min'     => 0 'max' => 1),
  99.             'allchannels' =>
  100.                 array('short' => 'A',
  101.                       'desc'  => 'List packages from all channels, '
  102.                                . 'not just the default one',
  103.                       'max'   => 0),
  104.             'channel' =>
  105.                 array('short'   => 'c',
  106.                       'desc'    => 'Specify which channel',
  107.                       'default' => '',
  108.                       'min'     => 0 'max' => 1),
  109.             'version' =>
  110.                 array('short' => 'V',
  111.                       'desc'  => 'Print version information',
  112.                       'max'   => 0),
  113.             'help' =>
  114.                 array('short' => 'h',
  115.                       'desc'  => 'Show this help',
  116.                       'max'   => 0),
  117.         );
  118.         $this->args = Console_Getargs::factory($this->opts);
  119.         if (PEAR::isError($this->args)) {
  120.             if ($this->args->getCode(=== CONSOLE_GETARGS_HELP{
  121.                 $this->error = '';
  122.             else {
  123.                 $this->error = $this->args->getMessage();
  124.             }
  125.             return;
  126.         }
  127.  
  128.         $options = array('channels' => array('pear.php.net'),
  129.                          'resume' => PEAR_INFO_ALL);
  130.  
  131.         // php-dir
  132.         if ($this->args->isDefined('pd')) {
  133.             $pear_dir $this->args->getValue('pd');
  134.         else {
  135.             $pear_dir '';
  136.         }
  137.  
  138.         // usr-cfg
  139.         if ($this->args->isDefined('uc')) {
  140.             $user_file $this->args->getValue('uc');
  141.             if (!file_exists($user_file)) {
  142.                 $this->error = 'Failed opening PEAR user configuration file "'
  143.                      . $user_file
  144.                      . '". Please check your spelling and try again.';
  145.                 return;
  146.             }
  147.         else {
  148.             $user_file '';
  149.         }
  150.  
  151.         // sys-cfg
  152.         if ($this->args->isDefined('sc')) {
  153.             $system_file $this->args->getValue('sc');
  154.             if (!file_exists($system_file)) {
  155.                 $this->error = 'Failed opening PEAR system configuration file "'
  156.                      . $system_file
  157.                      . '". Please check your spelling and try again.';
  158.                 return;
  159.             }
  160.         else {
  161.             $system_file '';
  162.         }
  163.  
  164.         // http-proxy
  165.         if ($this->args->isDefined('hp')) {
  166.             $proxy $this->args->getValue('hp');
  167.             $res   PEAR_Info::setProxy($proxy);
  168.             if ($res === false{
  169.                 $this->error = 'Failed define Proxy Server Address.'
  170.                      . ' Please check your spelling and try again.';
  171.                 return;
  172.             }
  173.         }
  174.  
  175.         // all
  176.         if ($this->args->isDefined('a')) {
  177.             $a $this->args->getValue('a');
  178.             if (is_numeric($a)) {
  179.                 $options['resume'intval($a);
  180.             else {
  181.                 $this->error = "No valid 'resume' option for argument all."
  182.                      . ' Please check your spelling and try again.';
  183.                 return;
  184.             }
  185.         }
  186.  
  187.         // allchannels
  188.         $A $this->args->getValue('A');
  189.         if (isset($A)) {
  190.             $options['channels'= array();
  191.         }
  192.  
  193.         // channel
  194.         if ($this->args->isDefined('c')) {
  195.             $chan $this->args->getValue('c');
  196.             if (is_string($chan)) {
  197.                 $options['channels'explode(','$chan);
  198.             else {
  199.                 $this->error = 'No valid channel list provided.'
  200.                      . ' Please check your spelling and try again.';
  201.                 return;
  202.             }
  203.         }
  204.  
  205.         // version
  206.         $V $this->args->getValue('V');
  207.         if (isset($V)) {
  208.             $this->error = 'PEAR_Info (cli) version 1.8.0'
  209.                          . ' (http://pear.php.net/package/PEAR_Info)';
  210.             return;
  211.         }
  212.  
  213.         parent::__construct($pear_dir$user_file$system_file$options);
  214.     }
  215.  
  216.     /**
  217.      * ZE1 PHP4 Compatible Constructor
  218.      *
  219.      * @since  1.8.0
  220.      */
  221.     function PEAR_Info_Cli()
  222.     {
  223.         $this->__construct();
  224.     }
  225.  
  226.     /**
  227.      * Run the CLI Script
  228.      *
  229.      * @return void 
  230.      * @access public
  231.      * @since  1.8.0
  232.      */
  233.     function run()
  234.     {
  235.         if (isset($this->error)) {
  236.             if (strpos($this->error'PEAR_Info'=== false{
  237.                 $this->_printUsage($this->error);
  238.             else {
  239.                 // when Version asked, do not print help usage
  240.                 echo $this->error;
  241.             }
  242.             return;
  243.         }
  244.  
  245.         $table      = 0;
  246.         $skip_table = 0;
  247.         $tdeps      = false;
  248.         $td         '';
  249.         $lines      explode("\n"$this->info);
  250.  
  251.         foreach ($lines as $line{
  252.             $line trim($line);
  253.             if (strlen($line== 0 && $tdeps === false{
  254.                 // skip blank line
  255.  
  256.             elseif ($line == '<br />'{
  257.                 // skip
  258.  
  259.             elseif (substr($line06== '<a id='{
  260.                 // skip package anchor
  261.  
  262.             elseif (substr($line04== '<h1>'{
  263.                 // skip PEAR credits link
  264.  
  265.             elseif (substr($line04== '<h2>'{
  266.                 if (substr($line410== '<a id="top'{
  267.                     // skip anchors
  268.                     $skip_table = 1;
  269.                 else {
  270.                     $td strip_tags($line);
  271.                     echo PHP_EOL . $td . PHP_EOL;
  272.                 }
  273.  
  274.             elseif ($skip_table == 0 && $line == '<table>'{
  275.                 echo PHP_EOL;
  276.                 $table++;
  277.  
  278.             elseif ($line == '</table>'{
  279.                 $skip_table = 0;
  280.                 if ($tdeps{
  281.                     $td = PHP_EOL . implode(PHP_EOL$deps);
  282.                 }
  283.                 $tdeps = false;
  284.  
  285.             elseif ($skip_table == 0 && $line == '<tr>'{
  286.                 $tr_class '';
  287.  
  288.             elseif ($skip_table == 0 && substr($line03== '<tr'{
  289.                 $tr_class substr($line111);
  290.                 if ($tr_class == 'w'{
  291.                     $tdeps = true;
  292.                     $tr_w  = array();
  293.                     $w     = 0;
  294.                 elseif ($tr_class == 'h'{
  295.                     $td '';
  296.                 else {
  297.                     $td = array('''');
  298.                 }
  299.  
  300.             elseif ($skip_table == 0 && $line == '</tr>' && $tr_class != ''{
  301.                 if ($tr_class == 'h'{
  302.                     $td explode(' '$td);
  303.                 }
  304.                 if (isset($td_v)) {
  305.                     $td $td_v;
  306.                 }
  307.                 if ($tdeps{
  308.                     if ($tr_w[0!= 'Required'{
  309.                         if ($tr_w[1== 'PHP'{
  310.                             $td $tr_w[1.' '$tr_w[3.' '$tr_w[4];
  311.                         else {
  312.                             if ($tr_w[3== 'has' || $tr_w[3== 'not'{
  313.                                 $td $tr_w[3.' '$tr_w[2.' '$tr_w[1.' '.
  314.                                       $tr_w[4];
  315.                             else {
  316.                                 $td $tr_w[2.' '$tr_w[1.' '$tr_w[3.' '.
  317.                                       $tr_w[4];
  318.                             }
  319.                             if ($tr_w[0== 'No'{
  320.                                 $td .= ' (optional)';
  321.                             }
  322.                         }
  323.                         $deps[' - ' $td;
  324.                     }
  325.                     $w = 0;
  326.                 else {
  327.                     echo $td[0' => ' $td[1. PHP_EOL;
  328.                 }
  329.  
  330.             elseif ($skip_table == 0 && $line == '</td>'{
  331.                 if (isset($td_v)) {
  332.                     if (empty($td_v[0])) {
  333.                         $td_v[0$td;
  334.                         $td      '';
  335.                     else {
  336.                         $td_v[1$td;
  337.                     }
  338.                 }
  339.  
  340.             elseif ($skip_table == 0 && $line == '<td>'{
  341.                 // skip
  342.  
  343.             elseif ($skip_table == 0 && substr($line03== '<td'{
  344.                 if ($tr_class == 'h'{
  345.                     $td '';
  346.  
  347.                 elseif ($tr_class == 'v'{
  348.                     if ($line == '<td class="e">'{
  349.                         $td_v = array('''');
  350.                         $td   '';
  351.                     else {
  352.                         unset($td_v);
  353.                         preg_match('`\<td(.*)\>(.*)\</td\>`'$line$matches);
  354.                         if (empty($matches[2])) {
  355.                             $td[1strip_tags($matches[0]);
  356.                         else {
  357.                             if (empty($matches[1])) {
  358.                                 $td[1strip_tags($matches[2]);
  359.                             else {
  360.                                 $td[0$matches[2];
  361.                             }
  362.                         }
  363.                     }
  364.                 }
  365.  
  366.             else {
  367.                 if ($tdeps{
  368.                     $tr_w[$w$line;
  369.                     $w++;
  370.                 else {
  371.                     if ($line == '<table class="d">'{
  372.                         $deps = array();
  373.                     else {
  374.                         $line strip_tags($line);
  375.                         $td  .= $line;
  376.                     }
  377.                 }
  378.             }
  379.         }
  380.     }
  381.  
  382.     /**
  383.      * Show full help information
  384.      *
  385.      * @param string $footer (optional) page footer content
  386.      *
  387.      * @return void 
  388.      * @access private
  389.      */
  390.     function _printUsage($footer '')
  391.     {
  392.         $header 'Usage: '
  393.             . basename($_SERVER['SCRIPT_NAME']" [options]\n\n";
  394.         echo Console_Getargs::getHelp($this->opts$header,
  395.             "\n$footer\n"782)."\n";
  396.     }
  397. }
  398. ?>

Documentation generated on Fri, 18 Apr 2008 16:30:05 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.