Source for file CLI.php
Documentation is available at CLI.php
* @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: CLI.php 313023 2011-07-06 19:17:11Z dufuz $
* @link http://pear.php.net/package/PEAR
* @since File available since Release 0.1
require_once 'PEAR/Frontend.php';
* Command-line Frontend for the PEAR Installer
* @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
* What type of user interface this frontend is for.
var $lp = ''; // line prefix
$term = getenv('TERM'); //(cox) $_ENV is empty for me in 4.1.1
// output is being redirected to a file or through a pipe
if (preg_match('/^(xterm|vt220|linux)/', $term)) {
$this->term['bold'] = sprintf("%c%c%c%c", 27 , 91 , 49 , 109 );
$this->term['normal'] = sprintf("%c%c%c", 27 , 91 , 109 );
$this->term['bold'] = sprintf("%c%c%c%c%c%c", 27 , 91 , 49 , 109 , 0 , 0 );
$this->term['normal'] = sprintf("%c%c%c%c%c", 27 , 91 , 109 , 0 , 0 );
// XXX add ANSI codes here
* @param object PEAR_Error object
return $this->_displayLine ($e->getMessage ());
* @param object PEAR_Error object
if ($config->get ('verbose') > 5 ) {
if (isset ($frame['class'])
$frame['class'] = !isset ($frame['class']) ? '' : $frame['class'];
$frame['type'] = !isset ($frame['type']) ? '' : $frame['type'];
$frame['function'] = !isset ($frame['function']) ? '' : $frame['function'];
$frame['line'] = !isset ($frame['line']) ? '' : $frame['line'];
$this->_displayLine (" #$i: $frame[class]$frame[type]$frame[function] $frame[line]" );
* Instruct the runInstallScript method to skip a paramgroup that matches the
* This method is useful for dynamically configuring which sections of a post-install script
* will be run based on the user's setup, which is very useful for making flexible
* post-install scripts without losing the cross-Frontend ability to retrieve user input
$this->_skipSections[$id] = true;
foreach ($scripts as $i => $script) {
* @param array $xml contents of postinstallscript tag
* @param object $script post-installation script
* @param string install|upgrade
$this->_skipSections = array ();
if (!is_array($xml) || !isset ($xml['paramgroup'])) {
$script->run (array (), '_default');
$completedPhases = array ();
if (!isset ($xml['paramgroup'][0 ])) {
$xml['paramgroup'] = array ($xml['paramgroup']);
foreach ($xml['paramgroup'] as $group) {
if (isset ($this->_skipSections[$group['id']])) {
// the post-install script chose to skip this section dynamically
if (isset ($group['name'])) {
$paramname = explode('::', $group['name']);
if ($lastgroup['id'] != $paramname[0 ]) {
$group['name'] = $paramname[1 ];
if (isset ($answers[$group['name']])) {
switch ($group['conditiontype']) {
if ($answers[$group['name']] != $group['value']) {
if ($answers[$group['name']] == $group['value']) {
$answers[$group['name']])) {
if (isset ($group['instructions'])) {
$this->_display ($group['instructions']);
if (!isset ($group['param'][0 ])) {
$group['param'] = array ($group['param']);
if (isset ($group['param'])) {
$prompts = $script->postProcessPrompts ($group['param'], $group['id']);
$this->outputData('postinstall', 'Error: post-install script did not ' .
'return proper post-processed prompts');
$prompts = $group['param'];
foreach ($prompts as $i => $var) {
if (!is_array($var) || !isset ($var['prompt']) ||
($var['name'] != $group['param'][$i]['name']) ||
($var['type'] != $group['param'][$i]['type'])
$this->outputData('postinstall', 'Error: post-install script ' .
'modified the variables or prompts, severe security risk. ' .
'Will instead use the defaults from the package.xml');
$prompts = $group['param'];
if ((isset ($answers) && $answers) || !isset ($group['param'])) {
if (!$script->run ($answers, $group['id'])) {
$script->run ($completedPhases, '_undoOnError');
$script->run ($completedPhases, '_undoOnError');
* Ask for user input, confirm the answers and continue until the user is satisfied
* @param array an array of arrays, format array('name' => 'paramname', 'prompt' =>
* 'text to display', 'type' => 'string'[, default => 'default value'])
$answers = $prompts = $types = array ();
foreach ($params as $param) {
$prompts[$param['name']] = $param['prompt'];
$types[$param['name']] = $param['type'];
$answers[$param['name']] = isset ($param['default']) ? $param['default'] : '';
foreach ($answers as $var => $value) {
echo $this->bold("* Enter an answer for #" . $i . " : ({$prompts[$var]})\n" );
$answers = $this->userDialog('', $prompts, $types, $answers);
function userDialog($command, $prompts, $types = array (), $defaults = array (), $screensize = 20 )
if (count($prompts) === 1 ) {
foreach ($prompts as $key => $prompt) {
$default = @$defaults[$key];
$line = fgets(STDIN , 2048 );
$result[$key] = ($default && trim($line) == '') ? $default : trim($line);
$descFormat = " %-{$descLength}s";
foreach ($prompts as $n => $var) {
$res = isset ($result[$n]) ? $result[$n] : null;
printf(" %2d. $descFormat : %s\n" , ++ $i, $prompts[$n], $res);
print " \n1-$last, 'all', 'abort', or Enter to continue: ";
if (isset ($testprompts[(int) $tmp - 1 ])) {
$var = $testprompts[(int) $tmp - 1 ];
$current = @$result[$var];
print " $desc [$current] : ";
} elseif ($tmp == 'all') {
foreach ($prompts as $var => $desc) {
$current = $result[$var];
print " $desc [$current] : ";
$result[$var] = trim($tmp);
trigger_error("PEAR_Frontend_CLI::userConfirm not yet converted", E_USER_ERROR );
static $positives = array ('y', 'yes', 'on', '1');
static $negatives = array ('n', 'no', 'off', '0');
print " $this->lp $prompt [ $default] : ";
$fp = fopen("php://stdin", "r");
$line = fgets($fp, 2048 );
foreach ($data as $type => $section) {
if (is_array($data) && isset ($data['release_warnings'])) {
$this->_startTable (array (
'caption' => 'Release Warnings'
$this->_tableRow (array ($data['release_warnings']), null , array (1 => array ('wrap' => 55 )));
$this->_displayLine (is_array($data) ? $data['data'] : $data);
$this->_startTable ($data);
if (isset ($data['headline']) && is_array($data['headline'])) {
$this->_tableRow ($data['headline'], array ('bold' => true ), array (1 => array ('wrap' => 55 )));
foreach($data['data'] as $category) {
foreach($category as $name => $pkg) {
$packages[$pkg[0 ]] = $pkg;
$this->_tableRow ($packages[$name], null , array (1 => array ('wrap' => 55 )));
if (!isset ($data['data'])) {
$this->_displayLine ('No packages in channel');
$this->_startTable ($data);
if (isset ($data['headline']) && is_array($data['headline'])) {
$this->_tableRow ($data['headline'], array ('bold' => true ), array (1 => array ('wrap' => 55 )));
foreach($data['data'] as $category) {
foreach($category as $name => $pkg) {
$packages[$pkg[0 ]] = $pkg;
$this->_tableRow ($pkg, null , array (1 => array ('wrap' => 55 )));
0 => array ('wrap' => 30 ),
1 => array ('wrap' => 20 ),
$this->_startTable ($data);
if (isset ($data['headline']) && is_array($data['headline'])) {
$this->_tableRow ($data['headline'], array ('bold' => true ), $opts);
foreach ($data['data'] as $group) {
foreach ($group as $value) {
$this->_tableRow ($value, null , $opts);
'caption' => 'Package details:',
array ("Latest", $data['stable']),
array ("Installed", $data['installed']),
array ("Package", $data['name']),
array ("License", $data['license']),
array ("Category", $data['category']),
array ("Summary", $data['summary']),
array ("Description", $data['description']),
if (isset ($d['deprecated']) && $d['deprecated']) {
$reg = $conf->getRegistry ();
$name = $reg->parsedPackageNameToString ($d['deprecated'], true );
$data['data'][] = array ('Deprecated! use', $name);
$this->_startTable ($data);
$count = count($data['data'][0 ]);
$opts = array (0 => array ('wrap' => 25 ),
$opts = array (0 => array ('wrap' => 30 ),
1 => array ('wrap' => 20 ),
if (isset ($data['headline']) && is_array($data['headline'])) {
$this->_tableRow ($data['headline'],
foreach($data['data'] as $row) {
$this->_tableRow ($row, null , $opts);
$this->_tableRow (array ($data['data']), null , $opts);
$this->_displayLine ($data);
function log($text, $append_crlf = true )
return $this->_displayLine ($text);
return $this->_display ($text);
if (empty ($this->term['bold'])) {
return $this->term['bold'] . $text . $this->term['normal'];
function _displayHeading ($title)
print $this->lp. $this->bold($title). "\n";
function _startTable ($params = array ())
$params['table_data'] = array ();
$params['widest'] = array (); // indexed by column
$params['highest'] = array (); // indexed by row
function _tableRow ($columns, $rowparams = array (), $colparams = array ())
for ($i = 0; $i < count($columns); $i++ ) {
if (isset ($colparams[$i]) && !empty ($colparams[$i]['wrap'])) {
$col = wordwrap($col, $colparams[$i]['wrap']);
if (strpos($col, "\n") !== false ) {
foreach ($multiline as $n => $line) {
$lines = count($multiline);
if (isset ($this->params['widest'][$i])) {
if ($w > $this->params['widest'][$i]) {
$this->params['widest'][$i] = $w;
$this->params['widest'][$i] = $w;
// handle unix, mac and windows formats
$lines = (isset ($tmp[10 ]) ? $tmp[10 ] : (isset ($tmp[13 ]) ? $tmp[13 ] : 0 )) + 1;
'rowparams' => $rowparams,
'colparams' => $colparams,
$this->params['table_data'][] = $new_row;
$this->_displayHeading ($caption);
if (count($table_data) === 0 ) {
for ($i = 0; $i < $ncols; $i++ ) {
if (!isset ($width[$i])) {
$width[$i] = $widest[$i];
$this->_displayLine ($borderline);
for ($i = 0; $i < count($table_data); $i++ ) {
for ($c = 0; $c < count($data); $c++ ) {
$rowlines[$c] = preg_split('/(\r?\n|\r)/', $data[$c]);
if (count($rowlines[$c]) < $height) {
$rowlines[$c] = array_pad($rowlines[$c], $height, '');
for ($c = 0; $c < count($data); $c++ ) {
$rowlines[$c] = array ($data[$c]);
for ($r = 0; $r < $height; $r++ ) {
for ($c = 0; $c < count($data); $c++ ) {
if (isset ($colparams[$c])) {
$w = isset ($width[$c]) ? $width[$c] : 0;
$cell = $rowlines[$c][$r];
if (isset ($attribs['bold'])) {
$cell = $this->bold($cell);
// not using str_pad here because we may
// add bold escape characters to $cell
$rowtext .= $cellstart . $cell . $cellend;
$rowtext = rtrim($rowtext);
$this->_displayLine ($rowtext);
$this->_displayLine ($borderline);
function _displayLine ($text)
print " $this->lp$text\n";
Documentation generated on Wed, 06 Jul 2011 23:30:32 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.
|