[ Index ] |
|
Code source de GeekLog 1.4.1 |
1 <?php 2 /** 3 * PEAR_Frontend_CLI 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: CLI.php,v 1.59 2006/03/02 13:16:19 pajoye Exp $ 20 * @link http://pear.php.net/package/PEAR 21 * @since File available since Release 0.1 22 */ 23 /** 24 * base class 25 */ 26 require_once 'PEAR/Frontend.php'; 27 28 /** 29 * Command-line Frontend for the PEAR Installer 30 * @category pear 31 * @package PEAR 32 * @author Stig Bakken <ssb@php.net> 33 * @author Greg Beaver <cellog@php.net> 34 * @copyright 1997-2006 The PHP Group 35 * @license http://www.php.net/license/3_0.txt PHP License 3.0 36 * @version Release: 1.4.11 37 * @link http://pear.php.net/package/PEAR 38 * @since Class available since Release 0.1 39 */ 40 class PEAR_Frontend_CLI extends PEAR_Frontend 41 { 42 // {{{ properties 43 44 /** 45 * What type of user interface this frontend is for. 46 * @var string 47 * @access public 48 */ 49 var $type = 'CLI'; 50 var $lp = ''; // line prefix 51 52 var $params = array(); 53 var $term = array( 54 'bold' => '', 55 'normal' => '', 56 ); 57 58 // }}} 59 60 // {{{ constructor 61 62 function PEAR_Frontend_CLI() 63 { 64 parent::PEAR(); 65 $term = getenv('TERM'); //(cox) $_ENV is empty for me in 4.1.1 66 if (function_exists('posix_isatty') && !posix_isatty(1)) { 67 // output is being redirected to a file or through a pipe 68 } elseif ($term) { 69 // XXX can use ncurses extension here, if available 70 if (preg_match('/^(xterm|vt220|linux)/', $term)) { 71 $this->term['bold'] = sprintf("%c%c%c%c", 27, 91, 49, 109); 72 $this->term['normal']=sprintf("%c%c%c", 27, 91, 109); 73 } elseif (preg_match('/^vt100/', $term)) { 74 $this->term['bold'] = sprintf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); 75 $this->term['normal']=sprintf("%c%c%c%c%c", 27, 91, 109, 0, 0); 76 } 77 } elseif (OS_WINDOWS) { 78 // XXX add ANSI codes here 79 } 80 } 81 82 // }}} 83 84 // {{{ displayLine(text) 85 86 function displayLine($text) 87 { 88 trigger_error("PEAR_Frontend_CLI::displayLine deprecated", E_USER_ERROR); 89 } 90 91 function _displayLine($text) 92 { 93 print "$this->lp$text\n"; 94 } 95 96 // }}} 97 // {{{ display(text) 98 99 function display($text) 100 { 101 trigger_error("PEAR_Frontend_CLI::display deprecated", E_USER_ERROR); 102 } 103 104 function _display($text) 105 { 106 print $text; 107 } 108 109 // }}} 110 // {{{ displayError(eobj) 111 112 /** 113 * @param object PEAR_Error object 114 */ 115 function displayError($eobj) 116 { 117 return $this->_displayLine($eobj->getMessage()); 118 } 119 120 // }}} 121 // {{{ displayFatalError(eobj) 122 123 /** 124 * @param object PEAR_Error object 125 */ 126 function displayFatalError($eobj) 127 { 128 $this->displayError($eobj); 129 if (class_exists('PEAR_Config')) { 130 $config = &PEAR_Config::singleton(); 131 if ($config->get('verbose') > 5) { 132 if (function_exists('debug_print_backtrace')) { 133 debug_print_backtrace(); 134 } elseif (function_exists('debug_backtrace')) { 135 $trace = debug_backtrace(); 136 $raised = false; 137 foreach ($trace as $i => $frame) { 138 if (!$raised) { 139 if (isset($frame['class']) && strtolower($frame['class']) == 140 'pear' && strtolower($frame['function']) == 'raiseerror') { 141 $raised = true; 142 } else { 143 continue; 144 } 145 } 146 @$this->_displayLine("#$i: $frame[class]$frame[type]$frame[function] $frame[line]"); 147 } 148 } 149 } 150 } 151 exit(1); 152 } 153 154 // }}} 155 // {{{ displayHeading(title) 156 157 function displayHeading($title) 158 { 159 trigger_error("PEAR_Frontend_CLI::displayHeading deprecated", E_USER_ERROR); 160 } 161 162 function _displayHeading($title) 163 { 164 print $this->lp.$this->bold($title)."\n"; 165 print $this->lp.str_repeat("=", strlen($title))."\n"; 166 } 167 168 // }}} 169 170 /** 171 * Instruct the runInstallScript method to skip a paramgroup that matches the 172 * id value passed in. 173 * 174 * This method is useful for dynamically configuring which sections of a post-install script 175 * will be run based on the user's setup, which is very useful for making flexible 176 * post-install scripts without losing the cross-Frontend ability to retrieve user input 177 * @param string 178 */ 179 function skipParamgroup($id) 180 { 181 $this->_skipSections[$id] = true; 182 } 183 184 function runPostinstallScripts(&$scripts) 185 { 186 foreach ($scripts as $i => $script) { 187 $this->runInstallScript($scripts[$i]->_params, $scripts[$i]->_obj); 188 } 189 } 190 191 /** 192 * @param array $xml contents of postinstallscript tag 193 * @param object $script post-installation script 194 * @param string install|upgrade 195 */ 196 function runInstallScript($xml, &$script) 197 { 198 $this->_skipSections = array(); 199 if (!is_array($xml) || !isset($xml['paramgroup'])) { 200 $script->run(array(), '_default'); 201 } else { 202 $completedPhases = array(); 203 if (!isset($xml['paramgroup'][0])) { 204 $xml['paramgroup'] = array($xml['paramgroup']); 205 } 206 foreach ($xml['paramgroup'] as $group) { 207 if (isset($this->_skipSections[$group['id']])) { 208 // the post-install script chose to skip this section dynamically 209 continue; 210 } 211 if (isset($group['name'])) { 212 $paramname = explode('::', $group['name']); 213 if ($lastgroup['id'] != $paramname[0]) { 214 continue; 215 } 216 $group['name'] = $paramname[1]; 217 if (isset($answers)) { 218 if (isset($answers[$group['name']])) { 219 switch ($group['conditiontype']) { 220 case '=' : 221 if ($answers[$group['name']] != $group['value']) { 222 continue 2; 223 } 224 break; 225 case '!=' : 226 if ($answers[$group['name']] == $group['value']) { 227 continue 2; 228 } 229 break; 230 case 'preg_match' : 231 if (!@preg_match('/' . $group['value'] . '/', 232 $answers[$group['name']])) { 233 continue 2; 234 } 235 break; 236 default : 237 return; 238 } 239 } 240 } else { 241 return; 242 } 243 } 244 $lastgroup = $group; 245 if (isset($group['instructions'])) { 246 $this->_display($group['instructions']); 247 } 248 if (!isset($group['param'][0])) { 249 $group['param'] = array($group['param']); 250 } 251 if (isset($group['param'])) { 252 if (method_exists($script, 'postProcessPrompts')) { 253 $prompts = $script->postProcessPrompts($group['param'], $group['id']); 254 if (!is_array($prompts) || count($prompts) != count($group['param'])) { 255 $this->outputData('postinstall', 'Error: post-install script did not ' . 256 'return proper post-processed prompts'); 257 $prompts = $group['param']; 258 } else { 259 foreach ($prompts as $i => $var) { 260 if (!is_array($var) || !isset($var['prompt']) || 261 !isset($var['name']) || 262 ($var['name'] != $group['param'][$i]['name']) || 263 ($var['type'] != $group['param'][$i]['type'])) { 264 $this->outputData('postinstall', 'Error: post-install script ' . 265 'modified the variables or prompts, severe security risk. ' . 266 'Will instead use the defaults from the package.xml'); 267 $prompts = $group['param']; 268 } 269 } 270 } 271 $answers = $this->confirmDialog($prompts); 272 } else { 273 $answers = $this->confirmDialog($group['param']); 274 } 275 } 276 if ((isset($answers) && $answers) || !isset($group['param'])) { 277 if (!isset($answers)) { 278 $answers = array(); 279 } 280 array_unshift($completedPhases, $group['id']); 281 if (!$script->run($answers, $group['id'])) { 282 $script->run($completedPhases, '_undoOnError'); 283 return; 284 } 285 } else { 286 $script->run($completedPhases, '_undoOnError'); 287 return; 288 } 289 } 290 } 291 } 292 293 /** 294 * Ask for user input, confirm the answers and continue until the user is satisfied 295 * @param array an array of arrays, format array('name' => 'paramname', 'prompt' => 296 * 'text to display', 'type' => 'string'[, default => 'default value']) 297 * @return array 298 */ 299 function confirmDialog($params) 300 { 301 $answers = array(); 302 $prompts = $types = array(); 303 foreach ($params as $param) { 304 $prompts[$param['name']] = $param['prompt']; 305 $types[$param['name']] = $param['type']; 306 if (isset($param['default'])) { 307 $answers[$param['name']] = $param['default']; 308 } else { 309 $answers[$param['name']] = ''; 310 } 311 } 312 do { 313 $ok = array('yesno' => 'no'); 314 do { 315 $answers = $this->userDialog('', $prompts, $types, $answers); 316 } while (count(array_filter($answers)) != count($prompts)); 317 $this->outputData('Your choices:'); 318 foreach ($prompts as $name => $prompt) { 319 $this->outputData($prompt . ': ' . $answers[$name]); 320 } 321 $ok = $this->userDialog('', 322 array( 323 'yesno' => 'These Choices OK? (use "abort" to halt)' 324 ), 325 array( 326 'yesno' => 'string', 327 ), 328 array( 329 'yesno' => 'yes' 330 ) 331 ); 332 if ($ok['yesno'] == 'abort') { 333 return false; 334 } 335 } while ($ok['yesno'] != 'yes'); 336 return $answers; 337 } 338 // {{{ userDialog(prompt, [type], [default]) 339 340 function userDialog($command, $prompts, $types = array(), $defaults = array()) 341 { 342 $result = array(); 343 if (is_array($prompts)) { 344 // php 5.0.0 inexplicably breaks BC with this behavior 345 // now reading from STDIN is the intended syntax 346 if (version_compare(phpversion(), '5.0.0', '<')) { 347 $fp = fopen("php://stdin", "r"); 348 } 349 foreach ($prompts as $key => $prompt) { 350 $type = $types[$key]; 351 $default = @$defaults[$key]; 352 if ($type == 'password') { 353 system('stty -echo'); 354 } 355 print "$this->lp$prompt "; 356 if ($default) { 357 print "[$default] "; 358 } 359 print ": "; 360 if (version_compare(phpversion(), '5.0.0', '<')) { 361 $line = fgets($fp, 2048); 362 } else { 363 if (!defined('STDIN')) { 364 define('STDIN', fopen('php://stdin', 'r')); 365 } 366 $line = fgets(STDIN, 2048); 367 } 368 if ($type == 'password') { 369 system('stty echo'); 370 print "\n"; 371 } 372 if ($default && trim($line) == "") { 373 $result[$key] = $default; 374 } else { 375 $result[$key] = trim($line); 376 } 377 } 378 if (version_compare(phpversion(), '5.0.0', '<')) { 379 fclose($fp); 380 } 381 } 382 return $result; 383 } 384 385 // }}} 386 // {{{ userConfirm(prompt, [default]) 387 388 function userConfirm($prompt, $default = 'yes') 389 { 390 trigger_error("PEAR_Frontend_CLI::userConfirm not yet converted", E_USER_ERROR); 391 static $positives = array('y', 'yes', 'on', '1'); 392 static $negatives = array('n', 'no', 'off', '0'); 393 print "$this->lp$prompt [$default] : "; 394 $fp = fopen("php://stdin", "r"); 395 $line = fgets($fp, 2048); 396 fclose($fp); 397 $answer = strtolower(trim($line)); 398 if (empty($answer)) { 399 $answer = $default; 400 } 401 if (in_array($answer, $positives)) { 402 return true; 403 } 404 if (in_array($answer, $negatives)) { 405 return false; 406 } 407 if (in_array($default, $positives)) { 408 return true; 409 } 410 return false; 411 } 412 413 // }}} 414 // {{{ startTable([params]) 415 416 function startTable($params = array()) 417 { 418 trigger_error("PEAR_Frontend_CLI::startTable deprecated", E_USER_ERROR); 419 } 420 421 function _startTable($params = array()) 422 { 423 $params['table_data'] = array(); 424 $params['widest'] = array(); // indexed by column 425 $params['highest'] = array(); // indexed by row 426 $params['ncols'] = 0; 427 $this->params = $params; 428 } 429 430 // }}} 431 // {{{ tableRow(columns, [rowparams], [colparams]) 432 433 function tableRow($columns, $rowparams = array(), $colparams = array()) 434 { 435 trigger_error("PEAR_Frontend_CLI::tableRow deprecated", E_USER_ERROR); 436 } 437 438 function _tableRow($columns, $rowparams = array(), $colparams = array()) 439 { 440 $highest = 1; 441 for ($i = 0; $i < sizeof($columns); $i++) { 442 $col = &$columns[$i]; 443 if (isset($colparams[$i]) && !empty($colparams[$i]['wrap'])) { 444 $col = wordwrap($col, $colparams[$i]['wrap'], "\n", 0); 445 } 446 if (strpos($col, "\n") !== false) { 447 $multiline = explode("\n", $col); 448 $w = 0; 449 foreach ($multiline as $n => $line) { 450 if (strlen($line) > $w) { 451 $w = strlen($line); 452 } 453 } 454 $lines = sizeof($multiline); 455 } else { 456 $w = strlen($col); 457 } 458 459 if (isset($this->params['widest'][$i])) { 460 if ($w > $this->params['widest'][$i]) { 461 $this->params['widest'][$i] = $w; 462 } 463 } else { 464 $this->params['widest'][$i] = $w; 465 } 466 $tmp = count_chars($columns[$i], 1); 467 // handle unix, mac and windows formats 468 $lines = (isset($tmp[10]) ? $tmp[10] : (isset($tmp[13]) ? $tmp[13] : 0)) + 1; 469 if ($lines > $highest) { 470 $highest = $lines; 471 } 472 } 473 if (sizeof($columns) > $this->params['ncols']) { 474 $this->params['ncols'] = sizeof($columns); 475 } 476 $new_row = array( 477 'data' => $columns, 478 'height' => $highest, 479 'rowparams' => $rowparams, 480 'colparams' => $colparams, 481 ); 482 $this->params['table_data'][] = $new_row; 483 } 484 485 // }}} 486 // {{{ endTable() 487 488 function endTable() 489 { 490 trigger_error("PEAR_Frontend_CLI::endTable deprecated", E_USER_ERROR); 491 } 492 493 function _endTable() 494 { 495 extract($this->params); 496 if (!empty($caption)) { 497 $this->_displayHeading($caption); 498 } 499 if (count($table_data) == 0) { 500 return; 501 } 502 if (!isset($width)) { 503 $width = $widest; 504 } else { 505 for ($i = 0; $i < $ncols; $i++) { 506 if (!isset($width[$i])) { 507 $width[$i] = $widest[$i]; 508 } 509 } 510 } 511 $border = false; 512 if (empty($border)) { 513 $cellstart = ''; 514 $cellend = ' '; 515 $rowend = ''; 516 $padrowend = false; 517 $borderline = ''; 518 } else { 519 $cellstart = '| '; 520 $cellend = ' '; 521 $rowend = '|'; 522 $padrowend = true; 523 $borderline = '+'; 524 foreach ($width as $w) { 525 $borderline .= str_repeat('-', $w + strlen($cellstart) + strlen($cellend) - 1); 526 $borderline .= '+'; 527 } 528 } 529 if ($borderline) { 530 $this->_displayLine($borderline); 531 } 532 for ($i = 0; $i < sizeof($table_data); $i++) { 533 extract($table_data[$i]); 534 if (!is_array($rowparams)) { 535 $rowparams = array(); 536 } 537 if (!is_array($colparams)) { 538 $colparams = array(); 539 } 540 $rowlines = array(); 541 if ($height > 1) { 542 for ($c = 0; $c < sizeof($data); $c++) { 543 $rowlines[$c] = preg_split('/(\r?\n|\r)/', $data[$c]); 544 if (sizeof($rowlines[$c]) < $height) { 545 $rowlines[$c] = array_pad($rowlines[$c], $height, ''); 546 } 547 } 548 } else { 549 for ($c = 0; $c < sizeof($data); $c++) { 550 $rowlines[$c] = array($data[$c]); 551 } 552 } 553 for ($r = 0; $r < $height; $r++) { 554 $rowtext = ''; 555 for ($c = 0; $c < sizeof($data); $c++) { 556 if (isset($colparams[$c])) { 557 $attribs = array_merge($rowparams, $colparams); 558 } else { 559 $attribs = $rowparams; 560 } 561 $w = isset($width[$c]) ? $width[$c] : 0; 562 //$cell = $data[$c]; 563 $cell = $rowlines[$c][$r]; 564 $l = strlen($cell); 565 if ($l > $w) { 566 $cell = substr($cell, 0, $w); 567 } 568 if (isset($attribs['bold'])) { 569 $cell = $this->bold($cell); 570 } 571 if ($l < $w) { 572 // not using str_pad here because we may 573 // add bold escape characters to $cell 574 $cell .= str_repeat(' ', $w - $l); 575 } 576 577 $rowtext .= $cellstart . $cell . $cellend; 578 } 579 if (!$border) { 580 $rowtext = rtrim($rowtext); 581 } 582 $rowtext .= $rowend; 583 $this->_displayLine($rowtext); 584 } 585 } 586 if ($borderline) { 587 $this->_displayLine($borderline); 588 } 589 } 590 591 // }}} 592 // {{{ outputData() 593 594 function outputData($data, $command = '_default') 595 { 596 switch ($command) { 597 case 'channel-info': 598 foreach ($data as $type => $section) { 599 if ($type == 'main') { 600 $section['data'] = array_values($section['data']); 601 } 602 $this->outputData($section); 603 } 604 break; 605 case 'install': 606 case 'upgrade': 607 case 'upgrade-all': 608 if (isset($data['release_warnings'])) { 609 $this->_displayLine(''); 610 $this->_startTable(array( 611 'border' => false, 612 'caption' => 'Release Warnings' 613 )); 614 $this->_tableRow(array($data['release_warnings']), null, array(1 => array('wrap' => 55))); 615 $this->_endTable(); 616 $this->_displayLine(''); 617 } 618 $this->_displayLine($data['data']); 619 break; 620 case 'search': 621 $this->_startTable($data); 622 if (isset($data['headline']) && is_array($data['headline'])) { 623 $this->_tableRow($data['headline'], array('bold' => true), array(1 => array('wrap' => 55))); 624 } 625 626 foreach($data['data'] as $category) { 627 foreach($category as $pkg) { 628 $this->_tableRow($pkg, null, array(1 => array('wrap' => 55))); 629 } 630 }; 631 $this->_endTable(); 632 break; 633 case 'list-all': 634 $this->_startTable($data); 635 if (isset($data['headline']) && is_array($data['headline'])) { 636 $this->_tableRow($data['headline'], array('bold' => true), array(1 => array('wrap' => 55))); 637 } 638 639 foreach($data['data'] as $category) { 640 foreach($category as $pkg) { 641 unset($pkg[4]); 642 unset($pkg[5]); 643 $this->_tableRow($pkg, null, array(1 => array('wrap' => 55))); 644 } 645 }; 646 $this->_endTable(); 647 break; 648 case 'config-show': 649 $data['border'] = false; 650 $opts = array(0 => array('wrap' => 30), 651 1 => array('wrap' => 20), 652 2 => array('wrap' => 35)); 653 $this->_startTable($data); 654 if (isset($data['headline']) && is_array($data['headline'])) { 655 $this->_tableRow($data['headline'], 656 array('bold' => true), 657 $opts); 658 } 659 foreach($data['data'] as $group) { 660 foreach($group as $value) { 661 if ($value[2] == '') { 662 $value[2] = "<not set>"; 663 } 664 $this->_tableRow($value, null, $opts); 665 } 666 } 667 $this->_endTable(); 668 break; 669 case 'remote-info': 670 $data = array( 671 'caption' => 'Package details:', 672 'border' => false, 673 'data' => array( 674 array("Latest", $data['stable']), 675 array("Installed", $data['installed']), 676 array("Package", $data['name']), 677 array("License", $data['license']), 678 array("Category", $data['category']), 679 array("Summary", $data['summary']), 680 array("Description", $data['description']), 681 ), 682 ); 683 default: { 684 if (is_array($data)) { 685 $this->_startTable($data); 686 $count = count($data['data'][0]); 687 if ($count == 2) { 688 $opts = array(0 => array('wrap' => 25), 689 1 => array('wrap' => 48) 690 ); 691 } elseif ($count == 3) { 692 $opts = array(0 => array('wrap' => 30), 693 1 => array('wrap' => 20), 694 2 => array('wrap' => 35) 695 ); 696 } else { 697 $opts = null; 698 } 699 if (isset($data['headline']) && is_array($data['headline'])) { 700 $this->_tableRow($data['headline'], 701 array('bold' => true), 702 $opts); 703 } 704 foreach($data['data'] as $row) { 705 $this->_tableRow($row, null, $opts); 706 } 707 $this->_endTable(); 708 } else { 709 $this->_displayLine($data); 710 } 711 } 712 } 713 } 714 715 // }}} 716 // {{{ log(text) 717 718 719 function log($text, $append_crlf = true) 720 { 721 if ($append_crlf) { 722 return $this->_displayLine($text); 723 } 724 return $this->_display($text); 725 } 726 727 728 // }}} 729 // {{{ bold($text) 730 731 function bold($text) 732 { 733 if (empty($this->term['bold'])) { 734 return strtoupper($text); 735 } 736 return $this->term['bold'] . $text . $this->term['normal']; 737 } 738 739 // }}} 740 } 741 742 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Wed Nov 21 12:27:40 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |