[ Index ]
 

Code source de GeekLog 1.4.1

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/system/pear/PEAR/Command/ -> Common.php (source)

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


Généré le : Wed Nov 21 12:27:40 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics