[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of Command Line Interface functions 4 // 5 // Created on: <05-Aug-2003 13:00:00 amos> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! \file cli.php 30 */ 31 32 /*! 33 \class eZCLI ezcli.php 34 \brief CLI handling 35 36 Provides functionality to work with the CLI (Command Line Interface). 37 The CLI can be run from either a terminal (shell) or a web interface. 38 39 A typical usage: 40 \code 41 $cli =& eZCLI::instance(); 42 43 $cli->setUseStyles( true ); // enable colors 44 45 $cli->output( "This is a text string" ); 46 47 \endcode 48 49 */ 50 51 include_once ( 'lib/ezutils/classes/ezini.php' ); 52 include_once ( 'lib/ezutils/classes/ezdebug.php' ); 53 include_once ( 'lib/ezutils/classes/ezdebugsetting.php' ); 54 55 define( 'EZ_CLI_TERMINAL_ENDOFLINE_STRING', "\n" ); 56 57 class eZCLI 58 { 59 /*! 60 Initializes object and detects if the CLI is used. 61 */ 62 function eZCLI() 63 { 64 $endl = "<br/>"; 65 $webOutput = true; 66 if ( isset( $_SERVER['argv'] ) ) 67 { 68 $endl = EZ_CLI_TERMINAL_ENDOFLINE_STRING; 69 $webOutput = false; 70 } 71 $this->EndlineString = $endl; 72 $this->WebOutput = $webOutput; 73 $this->TerminalStyles = array( 'warning' => "\033[1;35m", 74 'warning-end' => "\033[0;39m", 75 'error' => "\033[1;31m", 76 'error-end' => "\033[0;39m", 77 'failure' => "\033[1;31m", 78 'failure-end' => "\033[0;39m", 79 'notice' => "\033[0;32m", 80 'notice-end' => "\033[0;39m", 81 'debug' => "\033[0;30m", 82 'debug-end' => "\033[0;39m", 83 'timing' => "\033[1;34m", 84 'timing-end' => "\033[0;39m", 85 'success' => "\033[1;32m", 86 'success-end' => "\033[0;39m", 87 'file' => "\033[1;38m", 88 'file-end' => "\033[0;39m", 89 'dir' => "\033[1;34m", 90 'dir-end' => "\033[0;39m", 91 'link' => "\033[0;36m", 92 'link-end' => "\033[0;39m", 93 'exe' => "\033[1;32m", 94 'exe-end' => "\033[0;39m", 95 'archive' => "\033[1;31m", 96 'archive-end' => "\033[0;39m", 97 'image' => "\033[1;35m", 98 'image-end' => "\033[0;39m", 99 100 'red' => "\033[1;31m", 101 'red-end' => "\033[0;39m", 102 'green' => "\033[1;32m", 103 'green-end' => "\033[0;39m", 104 'yellow' => "\033[1;33m", 105 'yellow-end' => "\033[0;39m", 106 'blue' => "\033[1;34m", 107 'blue-end' => "\033[0;39m", 108 'magenta' => "\033[1;35m", 109 'magenta-end' => "\033[0;39m", 110 'cyan' => "\033[1;36m", 111 'cyan-end' => "\033[0;39m", 112 'white' => "\033[1;37m", 113 'white-end' => "\033[0;39m", 114 'gray' => "\033[1;30m", 115 'gray-end' => "\033[0;39m", 116 117 'dark-red' => "\033[0;31m", 118 'dark-red-end' => "\033[0;39m", 119 'dark-green' => "\033[0;32m", 120 'dark-green-end' => "\033[0;39m", 121 'dark-yellow' => "\033[0;33m", 122 'dark-yellow-end' => "\033[0;39m", 123 'dark-blue' => "\033[0;34m", 124 'dark-blue-end' => "\033[0;39m", 125 'dark-magenta' => "\033[0;35m", 126 'dark-magenta-end' => "\033[0;39m", 127 'dark-cyan' => "\033[0;36m", 128 'dark-cyan-end' => "\033[0;39m", 129 'dark-white' => "\033[0;37m", 130 'dark-white-end' => "\033[0;39m", 131 'dark-gray' => "\033[0;30m", 132 'dark-gray-end' => "\033[0;39m", 133 134 'red-bg' => "\033[1;41m", 135 'red-bg-end' => "\033[0;39m", 136 'green-bg' => "\033[1;42m", 137 'green-bg-end' => "\033[0;39m", 138 'yellow-bg' => "\033[1;43m", 139 'yellow-bg-end' => "\033[0;39m", 140 'blue-bg' => "\033[1;44m", 141 'blue-bg-end' => "\033[0;39m", 142 'magenta-bg' => "\033[1;45m", 143 'magenta-bg-end' => "\033[0;39m", 144 'cyan-bg' => "\033[1;46m", 145 'cyan-bg-end' => "\033[0;39m", 146 'white-bg' => "\033[1;47m", 147 'white-bg-end' => "\033[0;39m", 148 149 'text' => "\033[0;39m", 150 'text-end' => "\033[0;39m", 151 'variable' => "\033[1;34m", 152 'variable-end' => "\033[0;39m", 153 'symbol' => "\033[0;37m", 154 'symbol-end' => "\033[0;39m", 155 'emphasize' => "\033[1;38m", 156 'emphasize-end' => "\033[0;39m", 157 'header' => "\033[1;38m", 158 'header-end' => "\033[0;39m", 159 'strong' => "\033[1;39m", 160 'strong-end' => "\033[0;39m", 161 'mark' => "\033[1;30m", 162 'mark-end' => "\033[0;39m", 163 'bold' => "\033[1;38m", 164 'bold-end' => "\033[0;39m", 165 'italic' => "\033[0;39m", 166 'italic-end' => "\033[0;39m", 167 'underline' => "\033[0;39m", 168 'underline-end' => "\033[0;39m", 169 'paragraph' => "\033[0;39m", 170 'paragraph-end' => "\033[0;39m", 171 'normal' => "\033[0;39m", 172 'normal-end' => "\033[0;39m", 173 174 ); 175 176 $this->WebStyles = array( 'warning' => "<font color=\"orange\">", 177 'warning-end' => "</font>", 178 'error' => "<font color=\"red\">", 179 'error-end' => "</font>", 180 'failure' => "<font color=\"red\">", 181 'failure-end' => "</font>", 182 'notice' => "<font color=\"green\">", 183 'notice-end' => "</font>", 184 'debug' => "<font color=\"brown\">", 185 'debug-end' => "</font>", 186 'timing' => "<font color=\"blue\">", 187 'timing-end' => "</font>", 188 'success' => "<font color=\"green\">", 189 'success-end' => "</font>", 190 'file' => "<i>", 191 'file-end' => "</i>", 192 'dir' => "<font color=\"blue\">", 193 'dir-end' => "</font>", 194 'link' => "<font color=\"cyan\">", 195 'link-end' => "</font>", 196 'symbol' => "<i>", 197 'symbol-end' => "</i>", 198 'emphasize' => "<i>", 199 'emphasize-end' => "</i>", 200 'header' => "<h1>", 201 'header-end' => "</h1>", 202 'strong' => "<b>", 203 'strong-end' => "</b>", 204 'mark' => "", 205 'mark-end' => "", 206 'bold' => "<b>", 207 'bold-end' => "</b>", 208 'italic' => "<i>", 209 'italic-end' => "</i>", 210 'underline' => "<u>", 211 'underline-end' => "</u>", 212 'paragraph' => "<p>", 213 'paragraph-end' => "</p>", 214 'normal' => "", 215 'normal-end' => "" ); 216 217 $this->EmptyStyles = array(); 218 foreach ( $this->TerminalStyles as $styleName => $styleValue ) 219 { 220 $this->EmptyStyles[$styleName] = false; 221 } 222 $this->UseStyles = false; 223 $this->IsQuiet = false; 224 } 225 226 /*! 227 \return the string used to end lines. This is either the \n if CLI is used 228 or <br/> if the script is run in a webinterface. 229 */ 230 function endlineString() 231 { 232 return $this->EndlineString; 233 } 234 235 /*! 236 \return \c true if the current script is run in a webinterface. 237 */ 238 function isWebOutput() 239 { 240 return $this->WebOutput; 241 } 242 243 /*! 244 \return the style for the name \a $name. The style is specific for terminals. 245 */ 246 function terminalStyle( $name ) 247 { 248 if ( isset( $this->TerminalStyles[$name] ) ) 249 { 250 return $this->TerminalStyles[$name]; 251 } 252 return false; 253 } 254 255 /*! 256 \return the style for the name \a $name. The style is specific for web. 257 */ 258 function webStyle( $name ) 259 { 260 return $this->WebStyles[$name]; 261 } 262 263 /*! 264 \return a hash with all terminal styles. 265 */ 266 function terminalStyles() 267 { 268 return $this->TerminalStyles; 269 } 270 271 /*! 272 \return a hash with all web styles. 273 */ 274 function webStyles() 275 { 276 return $this->WebStyles; 277 } 278 279 /*! 280 \return a hash with empty styles. 281 */ 282 function emptyStyles() 283 { 284 return $this->EmptyStyles; 285 } 286 287 /*! 288 \return the style for the name \a $name. The style is taken from the current interface type. 289 */ 290 function style( $name ) 291 { 292 if ( $this->UseStyles ) 293 { 294 if ( $this->isWebOutput() ) 295 return $this->webStyle( $name ); 296 else 297 return $this->terminalStyle( $name ); 298 } 299 return false; 300 } 301 302 /*! 303 \return the text \a $text wrapped in the style \a $styleName. 304 */ 305 function stylize( $styleName, $text ) 306 { 307 $preStyle = $this->style( $styleName ); 308 $postStyle = $this->style( $styleName . '-end' ); 309 return $preStyle . $text . $postStyle; 310 } 311 312 /*! 313 \static 314 \returns an ANSI sequence which will store the current position. 315 */ 316 function storePosition() 317 { 318 return "\033[s"; 319 } 320 321 /*! 322 \static 323 \returns an ANSI sequence which will restore the current position. 324 */ 325 function restorePosition() 326 { 327 return "\033[u"; 328 } 329 330 /*! 331 \static 332 \return an ANSI sequence which will tell the console go to the specified column. 333 */ 334 function gotoColumn( $column ) 335 { 336 if ( !$this->UseStyles ) 337 { 338 return "\t\t"; 339 } 340 else 341 { 342 return "\033[" . $column . "G"; 343 } 344 } 345 346 /*! 347 Controls whether styles are to be used or not. If disabled 348 empty strings are returned when asking for styles. 349 \note This only controls the style() function. 350 */ 351 function setUseStyles( $useStyles ) 352 { 353 if ( getenv('TERM') != '' ) 354 { 355 $this->UseStyles = $useStyles; 356 } 357 } 358 359 /*! 360 \return \c true if styles are enabled. 361 */ 362 function useStyles() 363 { 364 return $this->UseStyles; 365 } 366 367 /*! 368 Outputs the string \a $string to the current interface. 369 If \a $addEOL is true then the end-of-line string is added. 370 */ 371 function output( $string = false, $addEOL = true ) 372 { 373 if ( $this->isQuiet() ) 374 return; 375 print( $string ); 376 if ( $addEOL ) 377 print( $this->endlineString() ); 378 } 379 380 /*! 381 Outputs the string \a $string to the current interface as a notice. 382 If \a $addEOL is true then the end-of-line string is added. 383 */ 384 function notice( $string = false, $addEOL = true ) 385 { 386 if ( $this->isQuiet() ) 387 return; 388 fputs( STDERR, $string ); 389 if ( $addEOL ) 390 fputs( STDERR, $this->endlineString() ); 391 } 392 393 /*! 394 Outputs the string \a $string to the current interface as an warning. 395 If \a $addEOL is true then the end-of-line string is added. 396 */ 397 function warning( $string = false, $addEOL = true ) 398 { 399 if ( $this->isQuiet() ) 400 return; 401 $string = $this->stylize( 'warning', $string ); 402 fputs( STDERR, $string ); 403 if ( $addEOL ) 404 fputs( STDERR, $this->endlineString() ); 405 } 406 407 /*! 408 Outputs the string \a $string to the current interface as an error. 409 If \a $addEOL is true then the end-of-line string is added. 410 */ 411 function error( $string = false, $addEOL = true ) 412 { 413 if ( $this->isQuiet() ) 414 return; 415 $string = $this->stylize( 'error', $string ); 416 fputs( STDERR, $string ); 417 if ( $addEOL ) 418 fputs( STDERR, $this->endlineString() ); 419 } 420 421 /*! 422 Sets whether the output(), notice(), warning() and error() methods should print out anything. 423 \sa isQuiet, isLoud 424 */ 425 function setIsQuiet( $isQuiet ) 426 { 427 $this->IsQuiet = $isQuiet; 428 } 429 430 /*! 431 \return \c true if output is not allowed. 432 \sa isLoud 433 */ 434 function isQuiet() 435 { 436 return $this->IsQuiet; 437 } 438 439 /*! 440 \return \c true if output is allowed. 441 \sa isQuiet 442 */ 443 function isLoud() 444 { 445 return !$this->IsQuiet; 446 } 447 448 function parseOptionString( $configString, &$optionConfig ) 449 { 450 $len = strlen( $configString ); 451 $i = 0; 452 if ( !is_array( $optionConfig ) ) 453 { 454 $optionConfig = array( 'list' => array(), 455 'short' => array(), 456 'long' => array() ); 457 } 458 while ( $i < $len ) 459 { 460 $option = $configString[$i]; 461 if ( $option == '[' ) 462 { 463 $end = strpos( $configString, ']', $i + 1 ); 464 if ( $end === false ) 465 { 466 eZDebug::writeError( "Missing end marker ] in option string at position $i", 467 'eZCLI::parseOptionString' ); 468 return $optionConfig; 469 } 470 $optionList = substr( $configString, $i + 1, $end - $i - 1 ); 471 $i += 1 + ( $end - $i ); 472 $startMarkerPos = strpos( $optionList, '[' ); 473 if ( $startMarkerPos !== false ) 474 { 475 eZDebug::writeError( "Start marker [ found in option string at position, it should not be present. Skipping current option" . ( $i + 1 + $startMarkerPos ), 476 'eZCLI::parseOptionString' ); 477 continue; 478 } 479 $optionList = explode( '|', $optionList ); 480 } 481 else 482 { 483 $text = $option; 484 ++$i; 485 if ( $i < $len and 486 in_array( $configString[$i], array( ':', ';' ) ) ) 487 { 488 $text .= $configString[$i]; 489 ++$i; 490 } 491 if ( $i < $len and 492 in_array( $configString[$i], array( '?', '*', '+' ) ) ) 493 { 494 $text .= $configString[$i]; 495 ++$i; 496 } 497 $optionList = array( $text ); 498 } 499 $optionStoreName = false; 500 unset( $optionConfigList ); 501 $optionConfigList = array(); 502 foreach ( $optionList as $optionItem ) 503 { 504 $optionLen = strlen( $optionItem ); 505 $hasValue = false; 506 $optionName = $optionItem; 507 $quantifierText = false; 508 $quantifier = array( 'min' => 0, 509 'max' => 0 ); 510 if ( $optionLen > 0 and in_array( $optionName[$optionLen - 1], array( '?', '*', '+' ) ) ) 511 { 512 $quantifierText = $optionName[$optionLen - 1]; 513 $optionName = substr( $optionName, 0, $optionLen - 1 ); 514 --$optionLen; 515 if ( $quantifierText == '?' ) 516 $quantifier = array( 'min' => 0, 517 'max' => 1 ); 518 else if ( $quantifierText == '*' ) 519 $quantifier = array( 'min' => 0, 520 'max' => false ); 521 else if ( $quantifierText == '+' ) 522 $quantifier = array( 'min' => 1, 523 'max' => false ); 524 } 525 if ( $optionLen > 0 and in_array( $optionName[$optionLen - 1], array( ':', ';' ) ) ) 526 { 527 $valueText = $optionName[$optionLen - 1]; 528 $optionName = substr( $optionName, 0, $optionLen - 1 ); 529 --$optionLen; 530 if ( $valueText == ':' ) 531 $hasValue = true; 532 else if ( $valueText == ';' ) 533 $hasValue = 'optional'; 534 } 535 $optionLen = strlen( $optionName ); 536 if ( $optionLen == 0 ) 537 continue; 538 $optionStoreName = $optionName; 539 $optionConfigItem = array( 'name' => $optionName, 540 'has-value' => $hasValue, 541 'quantifier' => $quantifier, 542 'store-name' => false, 543 'is-long-option' => strlen( $optionName ) > 1 ); 544 $optionConfigList[] = $optionConfigItem; 545 } 546 foreach ( array_keys( $optionConfigList ) as $optionConfigItemKey ) 547 { 548 $optionConfigItem =& $optionConfigList[$optionConfigItemKey]; 549 $optionName = $optionConfigItem['name']; 550 $optionConfigItem['store-name'] = $optionStoreName; 551 $optionConfig['list'][] = $optionConfigItem; 552 if ( $optionConfigItem['is-long-option'] ) 553 $optionConfig['long'][$optionName] = $optionConfigItem; 554 else 555 $optionConfig['short'][$optionName] = $optionConfigItem; 556 } 557 } 558 return $optionConfig; 559 } 560 561 function getOptions( $config, $argumentConfig, $arguments = false ) 562 { 563 $program = false; 564 if ( $arguments === false ) 565 { 566 $arguments = $GLOBALS['argv']; 567 $program = $arguments[0]; 568 array_shift( $arguments ); 569 } 570 571 if ( isset( $this ) and get_class( $this ) == 'ezcli' ) 572 $cli =& $this; 573 else 574 $cli =& eZCLI::instance(); 575 576 if ( is_string( $config ) ) 577 $config = eZCLI::parseOptionString( $config, $optionConfig ); 578 if ( is_string( $argumentConfig ) ) 579 $argumentConfig = eZCLI::parseOptionString( $argumentConfig, $tmpArgumentConfig ); 580 581 $options = array(); 582 583 $helpOption = false; 584 $helpText = false; 585 if ( isset( $config['short']['h'] ) ) 586 $helpOption = '-h'; 587 else if ( isset( $config['short']['help'] ) ) 588 $helpOption = '--help'; 589 if ( $helpOption ) 590 $helpText = "\n" . "Try `$program $helpOption' for more information."; 591 592 $options['arguments'] = array(); 593 594 $arguments = array_values( $arguments ); 595 $argumentCount = count( $arguments ); 596 for ( $i = 0; $i < $argumentCount; ++$i ) 597 { 598 $argument = $arguments[$i]; 599 $argumentLen = strlen( $argument ); 600 if ( $argumentLen > 1 and 601 $argument[0] == '-' ) 602 { 603 $argumentValue = false; 604 if ( $argumentLen > 2 and 605 $argument[1] == '-' ) 606 { 607 $optionName = substr( $argument, 2 ); 608 $assignPosition = strpos( $optionName, '=' ); 609 if ( $assignPosition !== false ) 610 { 611 $argumentValue = substr( $optionName, $assignPosition + 1 ); 612 $optionName = substr( $optionName, 0, $assignPosition ); 613 } 614 $optionType = 'long'; 615 $optionPrefix = '--'; 616 $checkNext = false; 617 } 618 else 619 { 620 $optionName = $argument[1]; 621 if ( $argumentLen > 2 ) 622 { 623 $argumentValue = substr( $argument, 2 ); 624 } 625 $optionType = 'short'; 626 $optionPrefix = '-'; 627 $checkNext = true; 628 } 629 $configItem =& $config[$optionType][$optionName]; 630 if ( isset( $configItem ) ) 631 { 632 $value = true; 633 $hasValue = $configItem['has-value']; 634 $hasMultipleValues = ( $configItem['quantifier']['min'] > 1 or 635 $configItem['quantifier']['max'] === false or 636 $configItem['quantifier']['max'] > 1 ); 637 if ( $hasValue ) 638 { 639 $hasArgumentValue = false; 640 if ( $argumentValue !== false ) 641 { 642 $value = $argumentValue; 643 } 644 else if ( $checkNext and $configItem['has-value'] !== 'optional' ) 645 { 646 ++$i; 647 if ( $i < $argumentCount ) 648 { 649 $hasArgumentValue = true; 650 } 651 else 652 { 653 --$i; 654 $cli->error( "$program: option `$optionPrefix$optionName' requires an argument" . $helpText ); 655 return false; 656 } 657 if ( $hasArgumentValue ) 658 $value = $arguments[$i]; 659 } 660 else 661 { 662 if ( $configItem['has-value'] !== 'optional' ) 663 { 664 $cli->error( "$program: option `$optionPrefix$optionName' requires an argument" . $helpText ); 665 return false; 666 } 667 } 668 } 669 $optionStoreName = $configItem['store-name']; 670 if ( !$optionStoreName ) 671 $optionStoreName = $optionName; 672 if ( $hasMultipleValues ) 673 { 674 if ( !isset( $options[$optionStoreName] ) ) 675 $options[$optionStoreName] = array(); 676 $options[$optionStoreName][] = $value; 677 } 678 else 679 { 680 $options[$optionStoreName] = $value; 681 } 682 } 683 else 684 { 685 $cli->error( "$program: invalid option `$optionPrefix$optionName'" . $helpText ); 686 return false; 687 } 688 } 689 else 690 { 691 $options['arguments'][] = $argument; 692 } 693 } 694 foreach ( $config['list'] as $configItem ) 695 { 696 $optionStoreName = $configItem['store-name']; 697 if ( $optionStoreName and !isset( $options[$optionStoreName] ) ) 698 $options[$optionStoreName] = null; 699 } 700 return $options; 701 } 702 703 /*! 704 \return the unique instance for the cli class. 705 */ 706 function &instance() 707 { 708 $implementation =& $GLOBALS['eZCLIInstance']; 709 if ( !isset( $implementation ) or 710 get_class( $implementation ) != 'ezcli' ) 711 { 712 $implementation = new eZCLI(); 713 } 714 return $implementation; 715 } 716 717 /*! 718 \return \c true if instance of eZCLI already exists otherwise \c false. 719 */ 720 function hasInstance() 721 { 722 $implementation =& $GLOBALS['eZCLIInstance']; 723 if ( isset( $implementation ) && get_class( $implementation ) == 'ezcli' ) 724 return true; 725 726 return false; 727 } 728 } 729 730 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |