cvs: pear-core /PEAR/Frontend CLI.php

From: Date: Sat, 04 Apr 2009 00:09:14 +0000
Subject: cvs: pear-core /PEAR/Frontend CLI.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-core+get-8301@lists.php.net to get a copy of this message
dufuz Sat Apr 4 00:09:14 2009 UTC Modified files: /pear-core/PEAR/Frontend CLI.php Log: rejuggle the code a little bit When there is only one question, no matter the type, don't go ahead with the whole 1-1, all, abort magic - It's distracting and we have had multiple complaints about the UX

http://cvs.php.net/viewvc.cgi/pear-core/PEAR/Frontend/CLI.php?r1=1.75&r2=1.76&diff_format=u Index: pear-core/PEAR/Frontend/CLI.php diff -u pear-core/PEAR/Frontend/CLI.php:1.75 pear-core/PEAR/Frontend/CLI.php:1.76 --- pear-core/PEAR/Frontend/CLI.php:1.75 Tue Feb 24 23:45:35 2009 +++ pear-core/PEAR/Frontend/CLI.php Sat Apr 4 00:09:14 2009 @@ -10,7 +10,7 @@ * @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,v 1.75 2009/02/24 23:45:35 dufuz Exp $ + * @version CVS: $Id: CLI.php,v 1.76 2009/04/04 00:09:14 dufuz Exp $ * @link http://pear.php.net/package/PEAR * @since File available since Release 0.1 */ @@ -66,22 +66,12 @@ } } - function _displayLine($text) - { - print "$this->lp$text\n"; - } - - function _display($text) - { - print $text; - } - /** * @param object PEAR_Error object */ - function displayError($eobj) + function displayError($e) { - return $this->_displayLine($eobj->getMessage()); + return $this->_displayLine($e->getMessage()); } /** @@ -98,40 +88,29 @@ exit(1); } - $trace = debug_backtrace(); $raised = false; - foreach ($trace as $i => $frame) { + foreach (debug_backtrace() as $i => $frame) { if (!$raised) { - if (isset($frame['class']) && strtolower($frame['class']) == - 'pear' && strtolower($frame['function']) == 'raiseerror') { + if (isset($frame['class']) + && strtolower($frame['class']) == 'pear' + && strtolower($frame['function']) == 'raiseerror' + ) { $raised = true; } else { continue; } } - if (!isset($frame['class'])) { - $frame['class'] = ''; - } - if (!isset($frame['type'])) { - $frame['type'] = ''; - } - if (!isset($frame['function'])) { - $frame['function'] = ''; - } - if (!isset($frame['line'])) { - $frame['line'] = ''; - } + + $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]"); } } } - exit(1); - } - function _displayHeading($title) - { - print $this->lp.$this->bold($title)."\n"; - print $this->lp.str_repeat("=", strlen($title))."\n"; + exit(1); } /** @@ -235,7 +214,8 @@ if (!is_array($var) || !isset($var['prompt']) || !isset($var['name']) || ($var['name'] != $group['param'][$i]['name']) || - ($var['type'] != $group['param'][$i]['type'])) { + ($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'); @@ -254,6 +234,7 @@ if (!isset($answers)) { $answers = array(); } + array_unshift($completedPhases, $group['id']); if (!$script->run($answers, $group['id'])) { $script->run($completedPhases, '_undoOnError'); @@ -274,8 +255,7 @@ */ function confirmDialog($params) { - $answers = array(); - $prompts = $types = array(); + $answers = $prompts = $types = array(); foreach ($params as $param) { $prompts[$param['name']] = $param['prompt']; $types[$param['name']] = $param['type']; @@ -295,7 +275,7 @@ } $answers = $this->userDialog('', $prompts, $types, $answers); - $tried = true; + $tried = true; } while (is_array($answers) && count(array_filter($answers)) != count($prompts)); return $answers; @@ -308,16 +288,12 @@ } $testprompts = array_keys($prompts); - $result = $defaults; - - if (!defined('STDIN')) { - define('STDIN', fopen('php://stdin', 'r')); - } + $result = $defaults; reset($prompts); - if (count($prompts) === 1 && $types[key($prompts)] == 'yesno') { + if (count($prompts) === 1) { foreach ($prompts as $key => $prompt) { - $type = $types[$key]; + $type = $types[$key]; $default = @$defaults[$key]; print "$prompt "; if ($default) { @@ -325,13 +301,10 @@ } print ": "; - $line = fgets(STDIN, 2048); - if ($default && trim($line) == "") { - $result[$key] = $default; - } else { - $result[$key] = trim($line); - } + $line = fgets(STDIN, 2048); + $result[$key] = ($default && trim($line) == '') ? $default : trim($line); } + return $result; } @@ -339,7 +312,7 @@ while (true) { $descLength = max(array_map('strlen', $prompts)); $descFormat = "%-{$descLength}s"; - $last = count($prompts); + $last = count($prompts); $i = 0; foreach ($prompts as $n => $var) { @@ -358,8 +331,8 @@ } if (isset($testprompts[(int)$tmp - 1])) { - $var = $testprompts[(int)$tmp - 1]; - $desc = $prompts[$var]; + $var = $testprompts[(int)$tmp - 1]; + $desc = $prompts[$var]; $current = @$result[$var]; print "$desc [$current] : "; $tmp = trim(fgets(STDIN, 1024)); @@ -380,10 +353,6 @@ $first_run = false; } - if (!defined('STDOUT')) { - fclose(STDIN); - } - return $result; } @@ -412,6 +381,172 @@ return false; } + function outputData($data, $command = '_default') + { + switch ($command) { + case 'channel-info': + foreach ($data as $type => $section) { + if ($type == 'main') { + $section['data'] = array_values($section['data']); + } + + $this->outputData($section); + } + break; + case 'install': + case 'upgrade': + case 'upgrade-all': + if (isset($data['release_warnings'])) { + $this->_displayLine(''); + $this->_startTable(array( + 'border' => false, + 'caption' => 'Release Warnings' + )); + $this->_tableRow(array($data['release_warnings']), null, array(1 => array('wrap' => 55))); + $this->_endTable(); + $this->_displayLine(''); + } + + $this->_displayLine($data['data']); + break; + case 'search': + $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 $pkg) { + $this->_tableRow($pkg, null, array(1 => array('wrap' => 55))); + } + } + + $this->_endTable(); + break; + case 'list-all': + if (!isset($data['data'])) { + $this->_displayLine('No packages in channel'); + break; + } + + $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 $pkg) { + unset($pkg[4], $pkg[5]); + $this->_tableRow($pkg, null, array(1 => array('wrap' => 55))); + } + } + + $this->_endTable(); + break; + case 'config-show': + $data['border'] = false; + $opts = array( + 0 => array('wrap' => 30), + 1 => array('wrap' => 20), + 2 => array('wrap' => 35) + ); + + $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) { + if ($value[2] == '') { + $value[2] = "<not set>"; + } + + $this->_tableRow($value, null, $opts); + } + } + + $this->_endTable(); + break; + case 'remote-info': + $d = $data; + $data = array( + 'caption' => 'Package details:', + 'border' => false, + 'data' => array( + 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']) { + $conf = &PEAR_Config::singleton(); + $reg = $conf->getRegistry(); + $name = $reg->parsedPackageNameToString($d['deprecated'], true); + $data['data'][] = array('Deprecated! use', $name); + } + default: { + if (is_array($data)) { + $this->_startTable($data); + $count = count($data['data'][0]); + if ($count == 2) { + $opts = array(0 => array('wrap' => 25), + 1 => array('wrap' => 48) + ); + } elseif ($count == 3) { + $opts = array(0 => array('wrap' => 30), + 1 => array('wrap' => 20), + 2 => array('wrap' => 35) + ); + } else { + $opts = null; + } + if (isset($data['headline']) && is_array($data['headline'])) { + $this->_tableRow($data['headline'], + array('bold' => true), + $opts); + } + + foreach($data['data'] as $row) { + $this->_tableRow($row, null, $opts); + } + $this->_endTable(); + } else { + $this->_displayLine($data); + } + } + } + } + + function log($text, $append_crlf = true) + { + if ($append_crlf) { + return $this->_displayLine($text); + } + + return $this->_display($text); + } + + function bold($text) + { + if (empty($this->term['bold'])) { + return strtoupper($text); + } + + return $this->term['bold'] . $text . $this->term['normal']; + } + + function _displayHeading($title) + { + print $this->lp.$this->bold($title)."\n"; + print $this->lp.str_repeat("=", strlen($title))."\n"; + } + function _startTable($params = array()) { $params['table_data'] = array(); @@ -427,7 +562,7 @@ for ($i = 0; $i < count($columns); $i++) { $col = &$columns[$i]; if (isset($colparams[$i]) && !empty($colparams[$i]['wrap'])) { - $col = wordwrap($col, $colparams[$i]['wrap'], "\n", 0); + $col = wordwrap($col, $colparams[$i]['wrap']); } if (strpos($col, "\n") !== false) { @@ -460,8 +595,8 @@ } } - if (sizeof($columns) > $this->params['ncols']) { - $this->params['ncols'] = sizeof($columns); + if (count($columns) > $this->params['ncols']) { + $this->params['ncols'] = count($columns); } $new_row = array( @@ -585,152 +720,13 @@ } } - function outputData($data, $command = '_default') - { - switch ($command) { - case 'channel-info': - foreach ($data as $type => $section) { - if ($type == 'main') { - $section['data'] = array_values($section['data']); - } - $this->outputData($section); - } - break; - case 'install': - case 'upgrade': - case 'upgrade-all': - if (isset($data['release_warnings'])) { - $this->_displayLine(''); - $this->_startTable(array( - 'border' => false, - 'caption' => 'Release Warnings' - )); - $this->_tableRow(array($data['release_warnings']), null, array(1 => array('wrap' => 55))); - $this->_endTable(); - $this->_displayLine(''); - } - $this->_displayLine($data['data']); - break; - case 'search': - $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 $pkg) { - $this->_tableRow($pkg, null, array(1 => array('wrap' => 55))); - } - }; - $this->_endTable(); - break; - case 'list-all': - if (!isset($data['data'])) { - $this->_displayLine('No packages in channel'); - break; - } - $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 $pkg) { - unset($pkg[4]); - unset($pkg[5]); - $this->_tableRow($pkg, null, array(1 => array('wrap' => 55))); - } - }; - $this->_endTable(); - break; - case 'config-show': - $data['border'] = false; - $opts = array(0 => array('wrap' => 30), - 1 => array('wrap' => 20), - 2 => array('wrap' => 35)); - $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) { - if ($value[2] == '') { - $value[2] = "<not set>"; - } - $this->_tableRow($value, null, $opts); - } - } - $this->_endTable(); - break; - case 'remote-info': - $d = $data; - $data = array( - 'caption' => 'Package details:', - 'border' => false, - 'data' => array( - 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']) { - $conf = &PEAR_Config::singleton(); - $reg = $conf->getRegistry(); - $name = $reg->parsedPackageNameToString($d['deprecated'], true); - $data['data'][] = array('Deprecated! use', $name); - } - default: { - if (is_array($data)) { - $this->_startTable($data); - $count = count($data['data'][0]); - if ($count == 2) { - $opts = array(0 => array('wrap' => 25), - 1 => array('wrap' => 48) - ); - } elseif ($count == 3) { - $opts = array(0 => array('wrap' => 30), - 1 => array('wrap' => 20), - 2 => array('wrap' => 35) - ); - } else { - $opts = null; - } - if (isset($data['headline']) && is_array($data['headline'])) { - $this->_tableRow($data['headline'], - array('bold' => true), - $opts); - } - - foreach($data['data'] as $row) { - $this->_tableRow($row, null, $opts); - } - $this->_endTable(); - } else { - $this->_displayLine($data); - } - } - } - } - - function log($text, $append_crlf = true) + function _displayLine($text) { - if ($append_crlf) { - return $this->_displayLine($text); - } - return $this->_display($text); + print "$this->lp$text\n"; } - function bold($text) + function _display($text) { - if (empty($this->term['bold'])) { - return strtoupper($text); - } - return $this->term['bold'] . $text . $this->term['normal']; + print $text; } } \ No newline at end of file
« previous php.pear.core (#8301) next »