22
22
*/
23
23
class ProgressBar
24
24
{
25
- const FORMAT_QUIET = ' %percent%% ' ;
26
- const FORMAT_NORMAL = ' %current%/%max% [%bar%] %percent:3s%% ' ;
27
- const FORMAT_VERBOSE = ' %current%/%max% [%bar%] %percent:3s%% Elapsed: %elapsed:6s% ' ;
28
- const FORMAT_QUIET_NOMAX = ' %current% ' ;
29
- const FORMAT_NORMAL_NOMAX = ' %current% [%bar%] ' ;
30
- const FORMAT_VERBOSE_NOMAX = ' %current% [%bar%] Elapsed: %elapsed:6s% ' ;
31
-
32
25
// options
33
26
private $ barWidth = 28 ;
34
27
private $ barChar = '= ' ;
@@ -52,6 +45,7 @@ class ProgressBar
52
45
private $ messages ;
53
46
54
47
static private $ formatters ;
48
+ static private $ formats ;
55
49
56
50
/**
57
51
* Constructor.
@@ -69,6 +63,10 @@ public function __construct(OutputInterface $output, $max = 0)
69
63
if (!self ::$ formatters ) {
70
64
self ::$ formatters = self ::initPlaceholderFormatters ();
71
65
}
66
+
67
+ if (!self ::$ formats ) {
68
+ self ::$ formats = self ::initFormats ();
69
+ }
72
70
}
73
71
74
72
/**
@@ -79,7 +77,7 @@ public function __construct(OutputInterface $output, $max = 0)
79
77
* @param string $name The placeholder name (including the delimiter char like %)
80
78
* @param callable $callable A PHP callable
81
79
*/
82
- public static function setPlaceholderFormatter ($ name , $ callable )
80
+ public static function setPlaceholderFormatterDefinition ($ name , $ callable )
83
81
{
84
82
if (!self ::$ formatters ) {
85
83
self ::$ formatters = self ::initPlaceholderFormatters ();
@@ -88,6 +86,23 @@ public static function setPlaceholderFormatter($name, $callable)
88
86
self ::$ formatters [$ name ] = $ callable ;
89
87
}
90
88
89
+ /**
90
+ * Sets a format for a given name.
91
+ *
92
+ * This method also allow you to override an existing format.
93
+ *
94
+ * @param string $name The format name
95
+ * @param string $format A format string
96
+ */
97
+ public static function setFormatDefinition ($ name , $ format )
98
+ {
99
+ if (!self ::$ formats ) {
100
+ self ::$ formats = self ::initFormats ();
101
+ }
102
+
103
+ self ::$ formats [$ name ] = $ format ;
104
+ }
105
+
91
106
public function setMessage ($ message , $ name = 'message ' )
92
107
{
93
108
$ this ->messages [$ name ] = $ message ;
@@ -235,8 +250,8 @@ public function getProgressCharacter()
235
250
*/
236
251
public function setFormat ($ format )
237
252
{
238
- $ this ->format = $ format ;
239
- $ this ->formatLineCount = substr_count ($ format , "\n" );
253
+ $ this ->format = isset ( self :: $ formats [ $ format ]) ? self :: $ formats [ $ format ] : $ format ;
254
+ $ this ->formatLineCount = substr_count ($ this -> format , "\n" );
240
255
}
241
256
242
257
/**
@@ -408,28 +423,14 @@ private function determineBestFormat()
408
423
{
409
424
switch ($ this ->output ->getVerbosity ()) {
410
425
case OutputInterface::VERBOSITY_QUIET :
411
- $ format = self ::FORMAT_QUIET_NOMAX ;
412
- if ($ this ->max > 0 ) {
413
- $ format = self ::FORMAT_QUIET ;
414
- }
415
- break ;
426
+ return $ this ->max > 0 ? 'quiet ' : 'quiet_nomax ' ;
416
427
case OutputInterface::VERBOSITY_VERBOSE :
417
428
case OutputInterface::VERBOSITY_VERY_VERBOSE :
418
429
case OutputInterface::VERBOSITY_DEBUG :
419
- $ format = self ::FORMAT_VERBOSE_NOMAX ;
420
- if ($ this ->max > 0 ) {
421
- $ format = self ::FORMAT_VERBOSE ;
422
- }
423
- break ;
430
+ return $ this ->max > 0 ? 'verbose ' : 'verbose_nomax ' ;
424
431
default :
425
- $ format = self ::FORMAT_NORMAL_NOMAX ;
426
- if ($ this ->max > 0 ) {
427
- $ format = self ::FORMAT_NORMAL ;
428
- }
429
- break ;
432
+ return $ this ->max > 0 ? 'normal ' : 'normal_nomax ' ;
430
433
}
431
-
432
- return $ format ;
433
434
}
434
435
435
436
static private function initPlaceholderFormatters ()
@@ -488,4 +489,16 @@ static private function initPlaceholderFormatters()
488
489
},
489
490
);
490
491
}
492
+
493
+ static private function initFormats ()
494
+ {
495
+ return array (
496
+ 'quiet ' => ' %percent%% ' ,
497
+ 'normal ' => ' %current%/%max% [%bar%] %percent:3s%% ' ,
498
+ 'verbose ' => ' %current%/%max% [%bar%] %percent:3s%% Elapsed: %elapsed:6s% ' ,
499
+ 'quiet_nomax ' => ' %current% ' ,
500
+ 'normal_nomax ' => ' %current% [%bar%] ' ,
501
+ 'verbose_nomax ' => ' %current% [%bar%] Elapsed: %elapsed:6s% ' ,
502
+ );
503
+ }
491
504
}
0 commit comments