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

Source for file Common.php

Documentation is available at Common.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: Common.php,v 1.24 2004/01/08 17:33:13 sniper Exp $
  19.  
  20. require_once "PEAR.php";
  21.  
  22. class PEAR_Command_Common extends PEAR
  23. {
  24.     // {{{ properties
  25.  
  26.     /**
  27.      * PEAR_Config object used to pass user system and configuration
  28.      * on when executing commands
  29.      *
  30.      * @var object 
  31.      */
  32.     var $config;
  33.  
  34.     /**
  35.      * User Interface object, for all interaction with the user.
  36.      * @var object 
  37.      */
  38.     var $ui;
  39.  
  40.     var $_deps_rel_trans = array(
  41.                                  'lt' => '<',
  42.                                  'le' => '<=',
  43.                                  'eq' => '=',
  44.                                  'ne' => '!=',
  45.                                  'gt' => '>',
  46.                                  'ge' => '>=',
  47.                                  'has' => '=='
  48.                                  );
  49.  
  50.     var $_deps_type_trans = array(
  51.                                   'pkg' => 'package',
  52.                                   'extension' => 'extension',
  53.                                   'php' => 'PHP',
  54.                                   'prog' => 'external program',
  55.                                   'ldlib' => 'external library for linking',
  56.                                   'rtlib' => 'external runtime library',
  57.                                   'os' => 'operating system',
  58.                                   'websrv' => 'web server',
  59.                                   'sapi' => 'SAPI backend'
  60.                                   );
  61.  
  62.     // }}}
  63.     // {{{ constructor
  64.  
  65.     /**
  66.      * PEAR_Command_Common constructor.
  67.      *
  68.      * @access public
  69.      */
  70.     function PEAR_Command_Common(&$ui&$config)
  71.     {
  72.         parent::PEAR();
  73.         $this->config &$config;
  74.         $this->ui &$ui;
  75.     }
  76.  
  77.     // }}}
  78.  
  79.     // {{{ getCommands()
  80.  
  81.     /**
  82.      * Return a list of all the commands defined by this class.
  83.      * @return array list of commands
  84.      * @access public
  85.      */
  86.     function getCommands()
  87.     {
  88.         $ret = array();
  89.         foreach (array_keys($this->commandsas $command{
  90.             $ret[$command$this->commands[$command]['summary'];
  91.         }
  92.         return $ret;
  93.     }
  94.  
  95.     // }}}
  96.     // {{{ getShortcuts()
  97.  
  98.     /**
  99.      * Return a list of all the command shortcuts defined by this class.
  100.      * @return array shortcut => command
  101.      * @access public
  102.      */
  103.     function getShortcuts()
  104.     {
  105.         $ret = array();
  106.         foreach (array_keys($this->commandsas $command{
  107.             if (isset($this->commands[$command]['shortcut'])) {
  108.                 $ret[$this->commands[$command]['shortcut']] $command;
  109.             }
  110.         }
  111.         return $ret;
  112.     }
  113.  
  114.     // }}}
  115.     // {{{ getOptions()
  116.  
  117.     function getOptions($command)
  118.     {
  119.         return @$this->commands[$command]['options'];
  120.     }
  121.  
  122.     // }}}
  123.     // {{{ getGetoptArgs()
  124.  
  125.     function getGetoptArgs($command&$short_args&$long_args)
  126.     {
  127.         $short_args "";
  128.         $long_args = array();
  129.         if (empty($this->commands[$command])) {
  130.             return;
  131.         }
  132.         reset($this->commands[$command]);
  133.         while (list($option$infoeach($this->commands[$command]['options'])) {
  134.             $larg $sarg '';
  135.             if (isset($info['arg'])) {
  136.                 if ($info['arg']{0== '('{
  137.                     $larg '==';
  138.                     $sarg '::';
  139.                     $arg substr($info['arg']1-1);
  140.                 else {
  141.                     $larg '=';
  142.                     $sarg ':';
  143.                     $arg $info['arg'];
  144.                 }
  145.             }
  146.             if (isset($info['shortopt'])) {
  147.                 $short_args .= $info['shortopt'$sarg;
  148.             }
  149.             $long_args[$option $larg;
  150.         }
  151.     }
  152.  
  153.     // }}}
  154.     // {{{ getHelp()
  155.     /**
  156.     * Returns the help message for the given command
  157.     *
  158.     * @param string $command The command
  159.     * @return mixed A fail string if the command does not have help or
  160.     *                a two elements array containing [0]=>help string,
  161.     *                [1]=> help string for the accepted cmd args
  162.     */
  163.     function getHelp($command)
  164.     {
  165.         $config &PEAR_Config::singleton();
  166.         $help @$this->commands[$command]['doc'];
  167.         if (empty($help)) {
  168.             // XXX (cox) Fallback to summary if there is no doc (show both?)
  169.             if (!$help @$this->commands[$command]['summary']{
  170.                 return "No help for command \"$command\"";
  171.             }
  172.         }
  173.         if (preg_match_all('/{config\s+([^\}]+)}/e'$help$matches)) {
  174.             foreach($matches[0as $k => $v{
  175.                 $help preg_replace("/$v/"$config->get($matches[1][$k])$help);
  176.             }
  177.         }
  178.         return array($help$this->getHelpArgs($command));
  179.     }
  180.  
  181.     // }}}
  182.     // {{{ getHelpArgs()
  183.     /**
  184.     * Returns the help for the accepted arguments of a command
  185.     *
  186.     * @param  string $command 
  187.     * @return string The help string
  188.     */
  189.     function getHelpArgs($command)
  190.     {
  191.         if (isset($this->commands[$command]['options']&&
  192.             count($this->commands[$command]['options']))
  193.         {
  194.             $help "Options:\n";
  195.             foreach ($this->commands[$command]['options'as $k => $v{
  196.                 if (isset($v['arg'])) {
  197.                     if ($v['arg']{0== '('{
  198.                         $arg substr($v['arg']1-1);
  199.                         $sapp = " [$arg]";
  200.                         $lapp = "[=$arg]";
  201.                     else {
  202.                         $sapp = " $v[arg]";
  203.                         $lapp = "=$v[arg]";
  204.                     }
  205.                 else {
  206.                     $sapp $lapp "";
  207.                 }
  208.                 if (isset($v['shortopt'])) {
  209.                     $s $v['shortopt'];
  210.                     @$help .= "  -$s$sapp, --$k$lapp\n";
  211.                 else {
  212.                     @$help .= "  --$k$lapp\n";
  213.                 }
  214.                 $p "        ";
  215.                 $doc rtrim(str_replace("\n""\n$p"$v['doc']));
  216.                 $help .= "        $doc\n";
  217.             }
  218.             return $help;
  219.         }
  220.         return null;
  221.     }
  222.  
  223.     // }}}
  224.     // {{{ run()
  225.  
  226.     function run($command$options$params)
  227.     {
  228.         $func @$this->commands[$command]['function'];
  229.         if (empty($func)) {
  230.             // look for shortcuts
  231.             foreach (array_keys($this->commandsas $cmd{
  232.                 if (@$this->commands[$cmd]['shortcut'== $command{
  233.                     $command $cmd;
  234.                     $func @$this->commands[$command]['function'];
  235.                     if (empty($func)) {
  236.                         return $this->raiseError("unknown command `$command'");
  237.                     }
  238.                     break;
  239.                 }
  240.             }
  241.         }
  242.         return $this->$func($command$options$params);
  243.     }
  244.  
  245.     // }}}
  246. }
  247.  
  248. ?>

Documentation generated on Mon, 11 Mar 2019 14:23:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.