2010-03-10 15:03:40 +03:00
|
|
|
<?php
|
2012-05-11 19:57:55 +04:00
|
|
|
/**
|
|
|
|
* PEAR_Command_Common base class
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* @category pear
|
|
|
|
* @package PEAR
|
|
|
|
* @author Stig Bakken <ssb@php.net>
|
|
|
|
* @author Greg Beaver <cellog@php.net>
|
|
|
|
* @copyright 1997-2009 The Authors
|
|
|
|
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|
|
|
* @version CVS: $Id: Common.php 313023 2011-07-06 19:17:11Z dufuz $
|
|
|
|
* @link http://pear.php.net/package/PEAR
|
|
|
|
* @since File available since Release 0.1
|
|
|
|
*/
|
2010-03-10 15:03:40 +03:00
|
|
|
|
2012-05-11 19:57:55 +04:00
|
|
|
/**
|
|
|
|
* base class
|
|
|
|
*/
|
|
|
|
require_once 'PEAR.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PEAR commands base class
|
|
|
|
*
|
|
|
|
* @category pear
|
|
|
|
* @package PEAR
|
|
|
|
* @author Stig Bakken <ssb@php.net>
|
|
|
|
* @author Greg Beaver <cellog@php.net>
|
|
|
|
* @copyright 1997-2009 The Authors
|
|
|
|
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|
|
|
* @version Release: 1.9.4
|
|
|
|
* @link http://pear.php.net/package/PEAR
|
|
|
|
* @since Class available since Release 0.1
|
|
|
|
*/
|
2010-03-10 15:03:40 +03:00
|
|
|
class PEAR_Command_Common extends PEAR
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* PEAR_Config object used to pass user system and configuration
|
|
|
|
* on when executing commands
|
|
|
|
*
|
2012-05-11 19:57:55 +04:00
|
|
|
* @var PEAR_Config
|
2010-03-10 15:03:40 +03:00
|
|
|
*/
|
|
|
|
var $config;
|
2012-05-11 19:57:55 +04:00
|
|
|
/**
|
|
|
|
* @var PEAR_Registry
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
var $_registry;
|
2010-03-10 15:03:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User Interface object, for all interaction with the user.
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
var $ui;
|
|
|
|
|
|
|
|
var $_deps_rel_trans = array(
|
|
|
|
'lt' => '<',
|
|
|
|
'le' => '<=',
|
|
|
|
'eq' => '=',
|
|
|
|
'ne' => '!=',
|
|
|
|
'gt' => '>',
|
|
|
|
'ge' => '>=',
|
|
|
|
'has' => '=='
|
|
|
|
);
|
|
|
|
|
|
|
|
var $_deps_type_trans = array(
|
|
|
|
'pkg' => 'package',
|
2012-05-11 19:57:55 +04:00
|
|
|
'ext' => 'extension',
|
2010-03-10 15:03:40 +03:00
|
|
|
'php' => 'PHP',
|
|
|
|
'prog' => 'external program',
|
|
|
|
'ldlib' => 'external library for linking',
|
|
|
|
'rtlib' => 'external runtime library',
|
|
|
|
'os' => 'operating system',
|
|
|
|
'websrv' => 'web server',
|
|
|
|
'sapi' => 'SAPI backend'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PEAR_Command_Common constructor.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function PEAR_Command_Common(&$ui, &$config)
|
|
|
|
{
|
|
|
|
parent::PEAR();
|
|
|
|
$this->config = &$config;
|
|
|
|
$this->ui = &$ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of all the commands defined by this class.
|
|
|
|
* @return array list of commands
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getCommands()
|
|
|
|
{
|
|
|
|
$ret = array();
|
|
|
|
foreach (array_keys($this->commands) as $command) {
|
|
|
|
$ret[$command] = $this->commands[$command]['summary'];
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of all the command shortcuts defined by this class.
|
|
|
|
* @return array shortcut => command
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getShortcuts()
|
|
|
|
{
|
|
|
|
$ret = array();
|
|
|
|
foreach (array_keys($this->commands) as $command) {
|
|
|
|
if (isset($this->commands[$command]['shortcut'])) {
|
|
|
|
$ret[$this->commands[$command]['shortcut']] = $command;
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOptions($command)
|
|
|
|
{
|
2012-05-11 19:57:55 +04:00
|
|
|
$shortcuts = $this->getShortcuts();
|
|
|
|
if (isset($shortcuts[$command])) {
|
|
|
|
$command = $shortcuts[$command];
|
|
|
|
}
|
2010-03-10 15:03:40 +03:00
|
|
|
|
2012-05-11 19:57:55 +04:00
|
|
|
if (isset($this->commands[$command]) &&
|
|
|
|
isset($this->commands[$command]['options'])) {
|
|
|
|
return $this->commands[$command]['options'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2010-03-10 15:03:40 +03:00
|
|
|
|
|
|
|
function getGetoptArgs($command, &$short_args, &$long_args)
|
|
|
|
{
|
2012-05-11 19:57:55 +04:00
|
|
|
$short_args = '';
|
2010-03-10 15:03:40 +03:00
|
|
|
$long_args = array();
|
2012-05-11 19:57:55 +04:00
|
|
|
if (empty($this->commands[$command]) || empty($this->commands[$command]['options'])) {
|
2010-03-10 15:03:40 +03:00
|
|
|
return;
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
|
|
|
reset($this->commands[$command]['options']);
|
2010-03-10 15:03:40 +03:00
|
|
|
while (list($option, $info) = each($this->commands[$command]['options'])) {
|
|
|
|
$larg = $sarg = '';
|
|
|
|
if (isset($info['arg'])) {
|
|
|
|
if ($info['arg']{0} == '(') {
|
|
|
|
$larg = '==';
|
|
|
|
$sarg = '::';
|
|
|
|
$arg = substr($info['arg'], 1, -1);
|
|
|
|
} else {
|
|
|
|
$larg = '=';
|
|
|
|
$sarg = ':';
|
|
|
|
$arg = $info['arg'];
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
if (isset($info['shortopt'])) {
|
|
|
|
$short_args .= $info['shortopt'] . $sarg;
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
$long_args[] = $option . $larg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the help message for the given command
|
|
|
|
*
|
|
|
|
* @param string $command The command
|
|
|
|
* @return mixed A fail string if the command does not have help or
|
|
|
|
* a two elements array containing [0]=>help string,
|
|
|
|
* [1]=> help string for the accepted cmd args
|
|
|
|
*/
|
|
|
|
function getHelp($command)
|
|
|
|
{
|
|
|
|
$config = &PEAR_Config::singleton();
|
2012-05-11 19:57:55 +04:00
|
|
|
if (!isset($this->commands[$command])) {
|
|
|
|
return "No such command \"$command\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
$help = null;
|
|
|
|
if (isset($this->commands[$command]['doc'])) {
|
|
|
|
$help = $this->commands[$command]['doc'];
|
|
|
|
}
|
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
if (empty($help)) {
|
|
|
|
// XXX (cox) Fallback to summary if there is no doc (show both?)
|
2012-05-11 19:57:55 +04:00
|
|
|
if (!isset($this->commands[$command]['summary'])) {
|
2010-03-10 15:03:40 +03:00
|
|
|
return "No help for command \"$command\"";
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
$help = $this->commands[$command]['summary'];
|
2010-03-10 15:03:40 +03:00
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
if (preg_match_all('/{config\s+([^\}]+)}/e', $help, $matches)) {
|
|
|
|
foreach($matches[0] as $k => $v) {
|
|
|
|
$help = preg_replace("/$v/", $config->get($matches[1][$k]), $help);
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
return array($help, $this->getHelpArgs($command));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 19:57:55 +04:00
|
|
|
* Returns the help for the accepted arguments of a command
|
|
|
|
*
|
|
|
|
* @param string $command
|
|
|
|
* @return string The help string
|
|
|
|
*/
|
2010-03-10 15:03:40 +03:00
|
|
|
function getHelpArgs($command)
|
|
|
|
{
|
|
|
|
if (isset($this->commands[$command]['options']) &&
|
|
|
|
count($this->commands[$command]['options']))
|
|
|
|
{
|
|
|
|
$help = "Options:\n";
|
|
|
|
foreach ($this->commands[$command]['options'] as $k => $v) {
|
|
|
|
if (isset($v['arg'])) {
|
2012-05-11 19:57:55 +04:00
|
|
|
if ($v['arg'][0] == '(') {
|
2010-03-10 15:03:40 +03:00
|
|
|
$arg = substr($v['arg'], 1, -1);
|
|
|
|
$sapp = " [$arg]";
|
|
|
|
$lapp = "[=$arg]";
|
|
|
|
} else {
|
|
|
|
$sapp = " $v[arg]";
|
|
|
|
$lapp = "=$v[arg]";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sapp = $lapp = "";
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
if (isset($v['shortopt'])) {
|
|
|
|
$s = $v['shortopt'];
|
2012-05-11 19:57:55 +04:00
|
|
|
$help .= " -$s$sapp, --$k$lapp\n";
|
2010-03-10 15:03:40 +03:00
|
|
|
} else {
|
2012-05-11 19:57:55 +04:00
|
|
|
$help .= " --$k$lapp\n";
|
2010-03-10 15:03:40 +03:00
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
$p = " ";
|
|
|
|
$doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
|
|
|
|
$help .= " $doc\n";
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
return $help;
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function run($command, $options, $params)
|
|
|
|
{
|
2012-05-11 19:57:55 +04:00
|
|
|
if (empty($this->commands[$command]['function'])) {
|
2010-03-10 15:03:40 +03:00
|
|
|
// look for shortcuts
|
|
|
|
foreach (array_keys($this->commands) as $cmd) {
|
2012-05-11 19:57:55 +04:00
|
|
|
if (isset($this->commands[$cmd]['shortcut']) && $this->commands[$cmd]['shortcut'] == $command) {
|
|
|
|
if (empty($this->commands[$cmd]['function'])) {
|
2010-03-10 15:03:40 +03:00
|
|
|
return $this->raiseError("unknown command `$command'");
|
2012-05-11 19:57:55 +04:00
|
|
|
} else {
|
|
|
|
$func = $this->commands[$cmd]['function'];
|
2010-03-10 15:03:40 +03:00
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
$command = $cmd;
|
|
|
|
|
|
|
|
//$command = $this->commands[$cmd]['function'];
|
2010-03-10 15:03:40 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
} else {
|
|
|
|
$func = $this->commands[$command]['function'];
|
2010-03-10 15:03:40 +03:00
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
|
2010-03-10 15:03:40 +03:00
|
|
|
return $this->$func($command, $options, $params);
|
|
|
|
}
|
2012-05-11 19:57:55 +04:00
|
|
|
}
|