Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezcli.php
Go to the documentation of this file.
1 <?php
30 class eZCLI
31 {
32 public $EndlineString;
33 public $WebOutput;
34 public $TerminalStyles;
35 public $WebStyles;
36 public $EmptyStyles;
37 public $UseStyles;
38 public $IsQuiet;
39 const TERMINAL_ENDOFLINE_STRING = "\n";
40
44 public function __construct()
45 {
46 $endl = "<br/>";
47 $webOutput = true;
48 if ( isset( $_SERVER['argv'] ) )
49 {
50 $endl = self::TERMINAL_ENDOFLINE_STRING;
51 $webOutput = false;
52 }
53 $this->EndlineString = $endl;
54 $this->WebOutput = $webOutput;
55 $this->TerminalStyles = array( 'warning' => "\033[1;33m",
56 'warning-end' => "\033[0;39m",
57 'error' => "\033[1;31m",
58 'error-end' => "\033[0;39m",
59 'failure' => "\033[1;31m",
60 'failure-end' => "\033[0;39m",
61 'notice' => "\033[0;32m",
62 'notice-end' => "\033[0;39m",
63 'strict' => "\033[0;35m",
64 'strict-end' => "\033[0;39m",
65 'debug' => "\033[0;30m",
66 'debug-end' => "\033[0;39m",
67 'timing' => "\033[1;34m",
68 'timing-end' => "\033[0;39m",
69 'success' => "\033[1;32m",
70 'success-end' => "\033[0;39m",
71 'file' => "\033[1;38m",
72 'file-end' => "\033[0;39m",
73 'dir' => "\033[1;34m",
74 'dir-end' => "\033[0;39m",
75 'link' => "\033[0;36m",
76 'link-end' => "\033[0;39m",
77 'exe' => "\033[1;32m",
78 'exe-end' => "\033[0;39m",
79 'archive' => "\033[1;31m",
80 'archive-end' => "\033[0;39m",
81 'image' => "\033[1;35m",
82 'image-end' => "\033[0;39m",
83
84 'red' => "\033[1;31m",
85 'red-end' => "\033[0;39m",
86 'green' => "\033[1;32m",
87 'green-end' => "\033[0;39m",
88 'yellow' => "\033[1;33m",
89 'yellow-end' => "\033[0;39m",
90 'blue' => "\033[1;34m",
91 'blue-end' => "\033[0;39m",
92 'magenta' => "\033[1;35m",
93 'magenta-end' => "\033[0;39m",
94 'cyan' => "\033[1;36m",
95 'cyan-end' => "\033[0;39m",
96 'white' => "\033[1;37m",
97 'white-end' => "\033[0;39m",
98 'gray' => "\033[1;30m",
99 'gray-end' => "\033[0;39m",
100
101 'dark-red' => "\033[0;31m",
102 'dark-red-end' => "\033[0;39m",
103 'dark-green' => "\033[0;32m",
104 'dark-green-end' => "\033[0;39m",
105 'dark-yellow' => "\033[0;33m",
106 'dark-yellow-end' => "\033[0;39m",
107 'dark-blue' => "\033[0;34m",
108 'dark-blue-end' => "\033[0;39m",
109 'dark-magenta' => "\033[0;35m",
110 'dark-magenta-end' => "\033[0;39m",
111 'dark-cyan' => "\033[0;36m",
112 'dark-cyan-end' => "\033[0;39m",
113 'dark-white' => "\033[0;37m",
114 'dark-white-end' => "\033[0;39m",
115 'dark-gray' => "\033[0;30m",
116 'dark-gray-end' => "\033[0;39m",
117
118 'red-bg' => "\033[1;41m",
119 'red-bg-end' => "\033[0;39m",
120 'green-bg' => "\033[1;42m",
121 'green-bg-end' => "\033[0;39m",
122 'yellow-bg' => "\033[1;43m",
123 'yellow-bg-end' => "\033[0;39m",
124 'blue-bg' => "\033[1;44m",
125 'blue-bg-end' => "\033[0;39m",
126 'magenta-bg' => "\033[1;45m",
127 'magenta-bg-end' => "\033[0;39m",
128 'cyan-bg' => "\033[1;46m",
129 'cyan-bg-end' => "\033[0;39m",
130 'white-bg' => "\033[1;47m",
131 'white-bg-end' => "\033[0;39m",
132
133 'text' => "\033[0;39m",
134 'text-end' => "\033[0;39m",
135 'variable' => "\033[1;34m",
136 'variable-end' => "\033[0;39m",
137 'symbol' => "\033[0;37m",
138 'symbol-end' => "\033[0;39m",
139 'emphasize' => "\033[1;38m",
140 'emphasize-end' => "\033[0;39m",
141 'header' => "\033[1;38m",
142 'header-end' => "\033[0;39m",
143 'strong' => "\033[1;39m",
144 'strong-end' => "\033[0;39m",
145 'mark' => "\033[1;30m",
146 'mark-end' => "\033[0;39m",
147 'bold' => "\033[1;38m",
148 'bold-end' => "\033[0;39m",
149 'italic' => "\033[0;39m",
150 'italic-end' => "\033[0;39m",
151 'underline' => "\033[0;39m",
152 'underline-end' => "\033[0;39m",
153 'paragraph' => "\033[0;39m",
154 'paragraph-end' => "\033[0;39m",
155 'normal' => "\033[0;39m",
156 'normal-end' => "\033[0;39m",
157
158 );
159
160 $this->WebStyles = array( 'warning' => '<span class="cli warning" style="color: orange;">',
161 'warning-end' => '</span>',
162 'error' => '<span class="cli error" style="color: red;">',
163 'error-end' => '</span>',
164 'failure' => '<span class="cli failure" style="color: red;">',
165 'failure-end' => '</span>',
166 'notice' => '<span class="cli notice" style="color: green;">',
167 'notice-end' => '</span>',
168 'debug' => '<span class="cli debug" style="color: brown;">',
169 'debug-end' => '</span>',
170 'timing' => '<span class="cli timing" style="color: blue;">',
171 'timing-end' => '</span>',
172 'success' => '<span class="cli success" style="color: green;">',
173 'success-end' => '</span>',
174 'file' => '<span class="cli file" style="font-style: italic;">',
175 'file-end' => '</span>',
176 'dir' => '<span class="cli dir" style="color: blue;">',
177 'dir-end' => '</span>',
178 'link' => '<span class="cli link" style="color: cyan;">',
179 'link-end' => '</span>',
180 'symbol' => '<span class="cli symbol" style="font-style: italic;">',
181 'symbol-end' => '</span>',
182 'emphasize' => "<em>",
183 'emphasize-end' => "</em>",
184 'header' => "<h1>",
185 'header-end' => "</h1>",
186 'strong' => '<strong>',
187 'strong-end' => '</strong>',
188 'mark' => '',
189 'mark-end' => '',
190 'bold' => '<span style="font-weight: bold;">',
191 'bold-end' => '</span>',
192 'italic' => '<span style="font-style: italic;">',
193 'italic-end' => '</span>',
194 'underline' => '<span style="text-decoration: underline">',
195 'underline-end' => '</span>',
196 'paragraph' => '<p>',
197 'paragraph-end' => '</p>',
198 'normal' => '',
199 'normal-end' => '' );
200
201 $this->EmptyStyles = array();
202 foreach ( $this->TerminalStyles as $styleName => $styleValue )
203 {
204 $this->EmptyStyles[$styleName] = false;
205 }
206 $this->UseStyles = false;
207 $this->IsQuiet = false;
208 }
209
214 function endlineString()
215 {
216 return $this->EndlineString;
217 }
218
222 function isWebOutput()
223 {
224 return $this->WebOutput;
225 }
226
230 function terminalStyle( $name )
231 {
232 if ( isset( $this->TerminalStyles[$name] ) )
233 {
234 return $this->TerminalStyles[$name];
235 }
236 return false;
237 }
238
242 function webStyle( $name )
243 {
244 return $this->WebStyles[$name];
245 }
246
250 function terminalStyles()
251 {
252 return $this->TerminalStyles;
253 }
254
258 function webStyles()
259 {
260 return $this->WebStyles;
261 }
262
266 function emptyStyles()
267 {
268 return $this->EmptyStyles;
269 }
270
274 function style( $name )
275 {
276 if ( $this->UseStyles )
277 {
278 if ( $this->isWebOutput() )
279 return $this->webStyle( $name );
280 else
281 return $this->terminalStyle( $name );
282 }
283 return false;
284 }
285
289 function stylize( $styleName, $text )
290 {
291 $preStyle = $this->style( $styleName );
292 $postStyle = $this->style( $styleName . '-end' );
293 return $preStyle . $text . $postStyle;
294 }
295
300 static function storePosition()
301 {
302 return "\033[s";
303 }
304
309 static function restorePosition()
310 {
311 return "\033[u";
312 }
313
317 function gotoColumn( $column )
318 {
319 if ( !$this->UseStyles )
320 {
321 return "\t\t";
322 }
323 else
324 {
325 return "\033[" . $column . "G";
326 }
327 }
328
334 function setUseStyles( $useStyles )
335 {
336 if ( getenv('TERM') != '' )
337 {
338 $this->UseStyles = $useStyles;
339 }
340 }
341
345 function useStyles()
346 {
347 return $this->UseStyles;
348 }
349
354 function output( $string = false, $addEOL = true )
355 {
356 if ( $this->isQuiet() )
357 return;
358 print( $string );
359 if ( $addEOL )
360 print( $this->endlineString() );
361 }
362
367 function notice( $string = false, $addEOL = true )
368 {
369 fputs( STDERR, $string );
370 if ( $addEOL )
371 fputs( STDERR, $this->endlineString() );
372 }
373
378 function warning( $string = false, $addEOL = true )
379 {
380 $string = $this->stylize( 'warning', $string );
381 fputs( STDERR, $string );
382 if ( $addEOL )
383 fputs( STDERR, $this->endlineString() );
384 }
385
390 function error( $string = false, $addEOL = true )
391 {
392 $string = $this->stylize( 'error', $string );
393 fputs( STDERR, $string );
394 if ( $addEOL )
395 fputs( STDERR, $this->endlineString() );
396 }
397
405 function setIsQuiet( $isQuiet )
406 {
407 $this->IsQuiet = $isQuiet;
408 }
409
414 function isQuiet()
415 {
416 return $this->IsQuiet;
417 }
418
423 function isLoud()
424 {
425 return !$this->IsQuiet;
426 }
427
446 static function parseOptionString( $configString, &$optionConfig )
447 {
448 $len = strlen( $configString );
449 $i = 0;
450 if ( !is_array( $optionConfig ) )
451 {
452 $optionConfig = array( 'list' => array(),
453 'short' => array(),
454 'long' => array() );
455 }
456 while ( $i < $len )
457 {
458 $option = $configString[$i];
459 if ( $option == '[' )
460 {
461 $end = strpos( $configString, ']', $i + 1 );
462 if ( $end === false )
463 {
464 eZDebug::writeError( "Missing end marker ] in option string at position $i", __METHOD__ );
465 return $optionConfig;
466 }
467 $optionList = substr( $configString, $i + 1, $end - $i - 1 );
468 $i += 1 + ( $end - $i );
469 $startMarkerPos = strpos( $optionList, '[' );
470 if ( $startMarkerPos !== false )
471 {
472 eZDebug::writeError( "Start marker [ found in option string at position, it should not be present. Skipping current option" . ( $i + 1 + $startMarkerPos ),
473 'eZCLI::parseOptionString' );
474 continue;
475 }
476 $optionList = explode( '|', $optionList );
477 }
478 else
479 {
480 $text = $option;
481 ++$i;
482 if ( $i < $len and
483 in_array( $configString[$i], array( ':', ';' ) ) )
484 {
485 $text .= $configString[$i];
486 ++$i;
487 }
488 if ( $i < $len and
489 in_array( $configString[$i], array( '?', '*', '+' ) ) )
490 {
491 $text .= $configString[$i];
492 ++$i;
493 }
494 $optionList = array( $text );
495 }
496 $optionStoreName = false;
497 unset( $optionConfigList );
498 $optionConfigList = array();
499 foreach ( $optionList as $optionItem )
500 {
501 $optionLen = strlen( $optionItem );
502 $hasValue = false;
503 $optionName = $optionItem;
504 $quantifierText = false;
505 $quantifier = array( 'min' => 0,
506 'max' => 0 );
507 if ( $optionLen > 0 and in_array( $optionName[$optionLen - 1], array( '?', '*', '+' ) ) )
508 {
509 $quantifierText = $optionName[$optionLen - 1];
510 $optionName = substr( $optionName, 0, $optionLen - 1 );
511 --$optionLen;
512 if ( $quantifierText == '?' )
513 $quantifier = array( 'min' => 0,
514 'max' => 1 );
515 else if ( $quantifierText == '*' )
516 $quantifier = array( 'min' => 0,
517 'max' => false );
518 else if ( $quantifierText == '+' )
519 $quantifier = array( 'min' => 1,
520 'max' => false );
521 }
522 if ( $optionLen > 0 and in_array( $optionName[$optionLen - 1], array( ':', ';' ) ) )
523 {
524 $valueText = $optionName[$optionLen - 1];
525 $optionName = substr( $optionName, 0, $optionLen - 1 );
526 --$optionLen;
527 if ( $valueText == ':' )
528 $hasValue = true;
529 else if ( $valueText == ';' )
530 $hasValue = 'optional';
531 }
532 $optionLen = strlen( $optionName );
533 if ( $optionLen == 0 )
534 continue;
535 $optionStoreName = $optionName;
536 $optionConfigItem = array( 'name' => $optionName,
537 'has-value' => $hasValue,
538 'quantifier' => $quantifier,
539 'store-name' => false,
540 'is-long-option' => strlen( $optionName ) > 1 );
541 $optionConfigList[] = $optionConfigItem;
542 }
543 foreach ( array_keys( $optionConfigList ) as $optionConfigItemKey )
544 {
545 $optionConfigItem =& $optionConfigList[$optionConfigItemKey];
546 $optionName = $optionConfigItem['name'];
547 $optionConfigItem['store-name'] = $optionStoreName;
548 $optionConfig['list'][] = $optionConfigItem;
549 if ( $optionConfigItem['is-long-option'] )
550 $optionConfig['long'][$optionName] = $optionConfigItem;
551 else
552 $optionConfig['short'][$optionName] = $optionConfigItem;
553 }
554 }
555 return $optionConfig;
556 }
557
575 function getOptions( $config, $argumentConfig, $arguments = false )
576 {
577 $program = false;
578 if ( $arguments === false )
579 {
580 $arguments = $GLOBALS['argv'];
581 $program = $arguments[0];
582 array_shift( $arguments );
583 }
584
585 if ( is_string( $config ) )
586 $config = eZCLI::parseOptionString( $config, $tmpConfig );
587 if ( is_string( $argumentConfig ) )
588 $argumentConfig = eZCLI::parseOptionString( $argumentConfig, $tmpArgumentConfig );
589
590 $options = array();
591
592 $helpOption = false;
593 $helpText = false;
594 if ( isset( $config['short']['h'] ) )
595 $helpOption = '-h';
596 else if ( isset( $config['short']['help'] ) )
597 $helpOption = '--help';
598 if ( $helpOption )
599 $helpText = "\n" . "Try `$program $helpOption' for more information.";
600
601 $options['arguments'] = array();
602
603 $arguments = array_values( $arguments );
604 $argumentCount = count( $arguments );
605 for ( $i = 0; $i < $argumentCount; ++$i )
606 {
607 $argument = $arguments[$i];
608 $argumentLen = strlen( $argument );
609 if ( $argumentLen > 1 and
610 $argument[0] == '-' )
611 {
612 $argumentValue = false;
613 if ( $argumentLen > 2 and
614 $argument[1] == '-' )
615 {
616 $optionName = substr( $argument, 2 );
617 $assignPosition = strpos( $optionName, '=' );
618 if ( $assignPosition !== false )
619 {
620 $argumentValue = substr( $optionName, $assignPosition + 1 );
621 $optionName = substr( $optionName, 0, $assignPosition );
622 }
623 $optionType = 'long';
624 $optionPrefix = '--';
625 $checkNext = false;
626 }
627 else
628 {
629 $optionName = $argument[1];
630 if ( $argumentLen > 2 )
631 {
632 $argumentValue = substr( $argument, 2 );
633 }
634 $optionType = 'short';
635 $optionPrefix = '-';
636 $checkNext = true;
637 }
638 $configItem =& $config[$optionType][$optionName];
639 if ( isset( $configItem ) )
640 {
641 $value = true;
642 $hasValue = $configItem['has-value'];
643 $hasMultipleValues = ( $configItem['quantifier']['min'] > 1 or
644 $configItem['quantifier']['max'] === false or
645 $configItem['quantifier']['max'] > 1 );
646 if ( $hasValue )
647 {
648 $hasArgumentValue = false;
649 if ( $argumentValue !== false )
650 {
651 $value = $argumentValue;
652 }
653 else if ( $checkNext and $configItem['has-value'] !== 'optional' )
654 {
655 ++$i;
656 if ( $i < $argumentCount )
657 {
658 $hasArgumentValue = true;
659 }
660 else
661 {
662 --$i;
663 $this->error( "$program: option `$optionPrefix$optionName' requires an argument" . $helpText );
664 return false;
665 }
666 if ( $hasArgumentValue )
667 $value = $arguments[$i];
668 }
669 else
670 {
671 if ( $configItem['has-value'] !== 'optional' )
672 {
673 $this->error( "$program: option `$optionPrefix$optionName' requires an argument" . $helpText );
674 return false;
675 }
676 }
677 }
678 $optionStoreName = $configItem['store-name'];
679 if ( !$optionStoreName )
680 $optionStoreName = $optionName;
681 if ( $hasMultipleValues )
682 {
683 if ( !isset( $options[$optionStoreName] ) )
684 $options[$optionStoreName] = array();
685 $options[$optionStoreName][] = $value;
686 }
687 else
688 {
689 $options[$optionStoreName] = $value;
690 }
691 }
692 else
693 {
694 $this->error( "$program: invalid option `$optionPrefix$optionName'" . $helpText );
695 return false;
696 }
697 }
698 else
699 {
700 $options['arguments'][] = $argument;
701 }
702 }
703 foreach ( $config['list'] as $configItem )
704 {
705 $optionStoreName = $configItem['store-name'];
706 if ( $optionStoreName and !isset( $options[$optionStoreName] ) )
707 $options[$optionStoreName] = null;
708 }
709 return $options;
710 }
711
717 static function instance()
718 {
719 if ( !isset( $GLOBALS['eZCLIInstance'] ) ||
720 !( $GLOBALS['eZCLIInstance'] instanceof eZCLI ) )
721 {
722 $GLOBALS['eZCLIInstance'] = new eZCLI();
723 }
724
725 return $GLOBALS['eZCLIInstance'];
726 }
727
731 static function hasInstance()
732 {
733 return isset( $GLOBALS['eZCLIInstance'] ) && $GLOBALS['eZCLIInstance'] instanceof eZCLI;
734 }
735 }
736
737 ?>
eZCLI
CLI handling.
Definition: ezcli.php:31
eZCLI\isLoud
isLoud()
Definition: ezcli.php:423
eZCLI\$WebStyles
$WebStyles
Definition: ezcli.php:35
eZCLI\isQuiet
isQuiet()
Definition: ezcli.php:414
eZCLI\$TerminalStyles
$TerminalStyles
Definition: ezcli.php:34
eZCLI\__construct
__construct()
Definition: ezcli.php:44
eZCLI\$EndlineString
$EndlineString
Definition: ezcli.php:32
eZCLI\TERMINAL_ENDOFLINE_STRING
const TERMINAL_ENDOFLINE_STRING
Definition: ezcli.php:39
eZCLI\$EmptyStyles
$EmptyStyles
Definition: ezcli.php:36
eZCLI\instance
static instance()
Definition: ezcli.php:717
eZCLI\warning
warning( $string=false, $addEOL=true)
Definition: ezcli.php:378
eZCLI\error
error( $string=false, $addEOL=true)
Definition: ezcli.php:390
eZCLI\$IsQuiet
$IsQuiet
Definition: ezcli.php:38
eZCLI\getOptions
getOptions( $config, $argumentConfig, $arguments=false)
Definition: ezcli.php:575
eZCLI\terminalStyle
terminalStyle( $name)
Definition: ezcli.php:230
eZCLI\style
style( $name)
Definition: ezcli.php:274
eZCLI\webStyles
webStyles()
Definition: ezcli.php:258
eZCLI\setIsQuiet
setIsQuiet( $isQuiet)
Definition: ezcli.php:405
eZCLI\output
output( $string=false, $addEOL=true)
Definition: ezcli.php:354
eZCLI\endlineString
endlineString()
Definition: ezcli.php:214
eZCLI\$WebOutput
$WebOutput
Definition: ezcli.php:33
eZCLI\emptyStyles
emptyStyles()
Definition: ezcli.php:266
eZCLI\stylize
stylize( $styleName, $text)
Definition: ezcli.php:289
eZCLI\webStyle
webStyle( $name)
Definition: ezcli.php:242
eZCLI\parseOptionString
static parseOptionString( $configString, &$optionConfig)
Definition: ezcli.php:446
eZCLI\useStyles
useStyles()
Definition: ezcli.php:345
eZCLI\storePosition
static storePosition()
Definition: ezcli.php:300
eZCLI\notice
notice( $string=false, $addEOL=true)
Definition: ezcli.php:367
eZCLI\terminalStyles
terminalStyles()
Definition: ezcli.php:250
eZCLI\hasInstance
static hasInstance()
Definition: ezcli.php:731
eZCLI\restorePosition
static restorePosition()
Definition: ezcli.php:309
eZCLI\setUseStyles
setUseStyles( $useStyles)
Definition: ezcli.php:334
eZCLI\gotoColumn
gotoColumn( $column)
Definition: ezcli.php:317
eZCLI\isWebOutput
isWebOutput()
Definition: ezcli.php:222
eZCLI\$UseStyles
$UseStyles
Definition: ezcli.php:37
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.