Browse Source
* Refactoring of "pear" command internals. Highlights:
* Refactoring of "pear" command internals. Highlights:
- user interface abstraction, making a Gtk installer should only be a matter of implementing PEAR_CommandUI_Gtk plus a "pear-gtk" executable - separated code into command classes, able to specify one or more commands - no more "pear-get" :-) * fixed use of PEAR_Config::singleton to avoid object copyingexperimental/new_ui_api
12 changed files with 660 additions and 83 deletions
-
16pear/DB.php
-
24pear/Makefile.frag
-
45pear/PEAR/Command.php
-
19pear/PEAR/Command/Common.php
-
127pear/PEAR/Command/Config.php
-
42pear/PEAR/Command/Install.php
-
107pear/PEAR/Command/Login.php
-
94pear/PEAR/CommandUI/CLI.php
-
32pear/PEAR/Config.php
-
41pear/PEAR/Remote.php
-
186pear/scripts/pear.in
-
10pear/tests/pear_config.phpt
@ -0,0 +1,127 @@ |
|||
<?php |
|||
//
|
|||
// +----------------------------------------------------------------------+
|
|||
// | PHP Version 4 |
|
|||
// +----------------------------------------------------------------------+
|
|||
// | Copyright (c) 1997-2002 The PHP Group |
|
|||
// +----------------------------------------------------------------------+
|
|||
// | This source file is subject to version 2.02 of the PHP license, |
|
|||
// | that is bundled with this package in the file LICENSE, and is |
|
|||
// | available at through the world-wide-web at |
|
|||
// | http://www.php.net/license/2_02.txt. |
|
|||
// | If you did not receive a copy of the PHP license and are unable to |
|
|||
// | obtain it through the world-wide-web, please send a note to |
|
|||
// | license@php.net so we can mail you a copy immediately. |
|
|||
// +----------------------------------------------------------------------+
|
|||
// | Author: Stig Bakken <ssb@fast.no> |
|
|||
// +----------------------------------------------------------------------+
|
|||
//
|
|||
// $Id$
|
|||
|
|||
require_once "PEAR/Command/Common.php"; |
|||
require_once "PEAR/Config.php"; |
|||
|
|||
/** |
|||
* PEAR commands for managing configuration data. |
|||
* |
|||
*/ |
|||
class PEAR_Command_Config extends PEAR_Command_Common |
|||
{ |
|||
// {{{ properties
|
|||
// }}}
|
|||
|
|||
// {{{ constructor
|
|||
|
|||
/** |
|||
* PEAR_Command_Config constructor. |
|||
* |
|||
* @access public |
|||
*/ |
|||
function PEAR_Command_Config($ui) |
|||
{ |
|||
parent::PEAR_Command_Common($ui); |
|||
} |
|||
|
|||
// }}}
|
|||
|
|||
// {{{ getCommands()
|
|||
|
|||
/** |
|||
* Return a list of all the commands defined by this class. |
|||
* @return array list of commands |
|||
* @access public |
|||
*/ |
|||
function getCommands() |
|||
{ |
|||
return array('config-show', 'config-get', 'config-set'); |
|||
} |
|||
|
|||
// }}}
|
|||
// {{{ run()
|
|||
|
|||
function run($command, $options, $params) |
|||
{ |
|||
$cf = $this->config; |
|||
$failmsg = ''; |
|||
switch ($command) { |
|||
case 'config-show': { |
|||
$keys = $cf->getKeys(); |
|||
if (isset($params[0]) && $cf->isDefined($params[0])) { |
|||
foreach ($keys as $key) { |
|||
$type = $cf->getType($key); |
|||
if ($type == 'password') { |
|||
$this->ui->displayLine("$key = ********"); |
|||
} else { |
|||
$this->ui->displayLine("$key = " . $cf->get($key, $params[0])); |
|||
} |
|||
} |
|||
} else { |
|||
foreach ($keys as $key) { |
|||
$type = $cf->getType($key); |
|||
if ($type == 'password') { |
|||
$this->ui->displayLine("$key = ********"); |
|||
} else { |
|||
$this->ui->displayLine("$key = " . $cf->get($key)); |
|||
} |
|||
} |
|||
} |
|||
break; |
|||
} |
|||
case 'config-get': { |
|||
if (sizeof($params) < 1 || sizeof($params) > 2) { |
|||
$failmsg .= "config-get expects 1 or 2 parameters"; |
|||
} elseif (sizeof($params) == 1) { |
|||
$this->ui->displayLine("$params[0] = " . $cf->get($params[0])); |
|||
} else { |
|||
$this->ui->displayLine("($params[1])$params[0] = " . |
|||
$cf->get($params[0], $params[1])); |
|||
} |
|||
break; |
|||
} |
|||
case 'config-set': { |
|||
if (sizeof($params) < 2 || sizeof($params) > 3) { |
|||
$failmsg .= "config-set expects 2 or 3 parameters"; |
|||
break; |
|||
} else { |
|||
if (!call_user_func_array(array($cf, 'set'), $params)) |
|||
{ |
|||
$failmsg = "config-set (" . |
|||
implode(", ", $params) . ") failed"; |
|||
} |
|||
} |
|||
break; |
|||
} |
|||
default: { |
|||
return false; |
|||
} |
|||
} |
|||
if ($failmsg) { |
|||
return $this->raiseError($failmsg); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
// }}}
|
|||
} |
|||
|
|||
?>
|
|||
@ -0,0 +1,107 @@ |
|||
<?php |
|||
//
|
|||
// +----------------------------------------------------------------------+
|
|||
// | PHP Version 4 |
|
|||
// +----------------------------------------------------------------------+
|
|||
// | Copyright (c) 1997-2002 The PHP Group |
|
|||
// +----------------------------------------------------------------------+
|
|||
// | This source file is subject to version 2.02 of the PHP license, |
|
|||
// | that is bundled with this package in the file LICENSE, and is |
|
|||
// | available at through the world-wide-web at |
|
|||
// | http://www.php.net/license/2_02.txt. |
|
|||
// | If you did not receive a copy of the PHP license and are unable to |
|
|||
// | obtain it through the world-wide-web, please send a note to |
|
|||
// | license@php.net so we can mail you a copy immediately. |
|
|||
// +----------------------------------------------------------------------+
|
|||
// | Author: Stig Bakken <ssb@fast.no> |
|
|||
// +----------------------------------------------------------------------+
|
|||
//
|
|||
// $Id$
|
|||
|
|||
require_once "PEAR/Command/Common.php"; |
|||
require_once "PEAR/Remote.php"; |
|||
require_once "PEAR/Config.php"; |
|||
|
|||
/** |
|||
* PEAR commands for managing configuration data. |
|||
* |
|||
*/ |
|||
class PEAR_Command_Login extends PEAR_Command_Common |
|||
{ |
|||
// {{{ properties
|
|||
// }}}
|
|||
|
|||
// {{{ constructor
|
|||
|
|||
/** |
|||
* PEAR_Command_Login constructor. |
|||
* |
|||
* @access public |
|||
*/ |
|||
function PEAR_Command_Login($ui) |
|||
{ |
|||
parent::PEAR_Command_Common($ui); |
|||
} |
|||
|
|||
// }}}
|
|||
|
|||
// {{{ getCommands()
|
|||
|
|||
/** |
|||
* Return a list of all the commands defined by this class. |
|||
* @return array list of commands |
|||
* @access public |
|||
*/ |
|||
function getCommands() |
|||
{ |
|||
return array('login', 'logout'); |
|||
} |
|||
|
|||
// }}}
|
|||
// {{{ run()
|
|||
|
|||
function run($command, $options, $params) |
|||
{ |
|||
$cf = $this->config; |
|||
$failmsg = ''; |
|||
$server = $cf->get('master_server'); |
|||
switch ($command) { |
|||
case 'login': { |
|||
$username = $cf->get('username'); |
|||
if (empty($username)) { |
|||
$this->ui->displayLine("Logging in to $server."); |
|||
$username = trim($this->ui->userDialog('Username')); |
|||
$cf->set('username', $username); |
|||
} else { |
|||
$this->ui->displayLine("Logging in as `$username' to $server."); |
|||
} |
|||
$password = trim($this->ui->userDialog('Password', 'password')); |
|||
$cf->set('password', $password); |
|||
$cf->store(); |
|||
$remote = new PEAR_Remote; |
|||
$ok = $remote->call('logintest'); |
|||
print "logintest=";var_dump($ok); |
|||
break; |
|||
} |
|||
case 'logout': { |
|||
$this->ui->displayLine("Logging out from $server."); |
|||
$cf->remove('username'); |
|||
$cf->remove('password'); |
|||
$cf->store(); |
|||
break; |
|||
} |
|||
default: { |
|||
$failmsg = "unknown command: $command"; |
|||
break; |
|||
} |
|||
} |
|||
if ($failmsg) { |
|||
return $this->raiseError($failmsg); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
// }}}
|
|||
} |
|||
|
|||
?>
|
|||
@ -0,0 +1,94 @@ |
|||
<?php |
|||
|
|||
require_once "PEAR.php"; |
|||
|
|||
class PEAR_CommandUI_CLI extends PEAR |
|||
{ |
|||
var $output_mode = 'plain'; |
|||
var $output_mode_params = array(); |
|||
|
|||
function PEAR_CommandUI_CLI() |
|||
{ |
|||
parent::PEAR(); |
|||
} |
|||
|
|||
function _PEAR_CommandUI_CLI() |
|||
{ |
|||
parent::_PEAR(); |
|||
if ($this->output_mode) { |
|||
$this->endOutput(); |
|||
} |
|||
} |
|||
|
|||
function displayLine($text) |
|||
{ |
|||
print "$text\n"; |
|||
} |
|||
|
|||
function userDialog($prompt, $type = 'text') |
|||
{ |
|||
if ($type == 'password') { |
|||
system('stty -echo'); |
|||
} |
|||
print "$prompt : "; |
|||
$fp = fopen("php://stdin", "r"); |
|||
$line = fgets($fp, 2048); |
|||
fclose($fp); |
|||
if ($type == 'password') { |
|||
system('stty echo'); |
|||
print "\n"; |
|||
} |
|||
return $line; |
|||
} |
|||
|
|||
function userConfirm($prompt, $default = 'yes') |
|||
{ |
|||
static $positives = array('y', 'yes', 'on', '1'); |
|||
static $negatives = array('n', 'no', 'off', '0'); |
|||
print "$prompt [$default] : "; |
|||
$fp = fopen("php://stdin", "r"); |
|||
$line = fgets($fp, 2048); |
|||
fclose($fp); |
|||
$answer = strtolower(trim($line)); |
|||
if (empty($answer)) { |
|||
$answer = $default; |
|||
} |
|||
if (in_array($answer, $positives)) { |
|||
return true; |
|||
} |
|||
if (in_array($answer, $negatives)) { |
|||
return false; |
|||
} |
|||
if (in_array($default, $positives)) { |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
function setOutputMode($mode, $params = array()) |
|||
{ |
|||
$this->output_mode = $mode; |
|||
$this->output_mode_params = $params; |
|||
} |
|||
|
|||
function startOutput($mode) |
|||
{ |
|||
if ($this->output_mode) { |
|||
$this->endOutput(); |
|||
} |
|||
switch ($mode) { |
|||
} |
|||
} |
|||
|
|||
function endOutput($mode = null) |
|||
{ |
|||
if ($mode === null) { |
|||
$mode = $this->output_mode; |
|||
} |
|||
$this->output_mode = ''; |
|||
switch ($mode) { |
|||
} |
|||
} |
|||
} |
|||
|
|||
?>
|
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue