99
1010namespace Inhere \Console \IO \Output ;
1111
12- use Inhere \Console \Concern \AbstractOutput ;
12+ use Inhere \Console \IO \Output ;
13+ use function implode ;
14+ use function is_array ;
15+ use function sprintf ;
1316use function strlen ;
1417use const PHP_EOL ;
1518
1619/**
1720 * Class BufferedOutput
1821 * @package Inhere\Console\IO\Output
1922 */
20- class BufferedOutput extends AbstractOutput
23+ class BufferedOutput extends Output
2124{
2225 /**
2326 * @var string
@@ -40,37 +43,114 @@ public function fetch(bool $reset = true): string
4043 return $ str ;
4144 }
4245
46+ /**
47+ * @return string
48+ */
49+ public function toString (): string
50+ {
51+ return $ this ->fetch ();
52+ }
53+
54+ public function reset (): void
55+ {
56+ $ this ->buffer = '' ;
57+ }
58+
59+ /**
60+ * @return string
61+ */
4362 public function __toString (): string
4463 {
4564 return $ this ->fetch ();
4665 }
4766
4867 /**
49- * @param string $content
68+ * @param mixed $messages
69+ * @param bool $nl
70+ * @param bool $quit
71+ * @param array $opts
5072 *
5173 * @return int
5274 */
53- public function write (string $ content ): int
75+ public function write ($ messages , $ nl = true , $ quit = false , array $ opts = [] ): int
5476 {
55- $ this ->buffer .= $ content ;
56- return strlen ($ content );
77+ if (is_array ($ messages )) {
78+ $ str = implode ($ nl ? PHP_EOL : '' , $ messages );
79+ } else {
80+ $ str = (string )$ messages ;
81+ }
82+
83+ if ($ nl ) {
84+ $ str .= PHP_EOL ;
85+ }
86+
87+ $ this ->buffer .= $ str ;
88+ return strlen ($ str );
5789 }
5890
5991 /**
60- * @param string $content
92+ * @param mixed $messages
6193 * @param bool $quit
6294 * @param array $opts
6395 *
6496 * @return int
6597 */
66- public function writeln ($ content , bool $ quit = false , array $ opts = []): int
98+ public function writeln ($ messages , bool $ quit = false , array $ opts = []): int
6799 {
68- $ this ->buffer .= $ content . PHP_EOL ;
69- return strlen ($ content ) + 1 ;
100+ return $ this ->write ($ messages , true , $ quit , $ opts );
70101 }
71102
72- public function reset (): void
103+ /**
104+ * Write a message to output with format.
105+ *
106+ * @param string $format
107+ * @param mixed ...$args
108+ *
109+ * @return int
110+ */
111+ public function writef (string $ format , ...$ args ): int
112+ {
113+ return $ this ->write (sprintf ($ format , ...$ args ));
114+ }
115+
116+ /**
117+ * start buffering
118+ */
119+ public function startBuffer (): void
120+ {
121+ }
122+
123+ /**
124+ * clear buffering
125+ */
126+ public function clearBuffer (): void
127+ {
128+ $ this ->reset ();
129+ }
130+
131+ /**
132+ * stop buffering and flush buffer text
133+ *
134+ * @param bool $flush
135+ * @param bool $nl
136+ * @param bool $quit
137+ * @param array{quitCode:int} $opts
138+ *
139+ * @see Console::stopBuffer()
140+ */
141+ public function stopBuffer (bool $ flush = true , bool $ nl = false , bool $ quit = false , array $ opts = []): void
142+ {
143+
144+ }
145+
146+ /**
147+ * stop buffering and flush buffer text
148+ *
149+ * @param bool $nl
150+ * @param bool $quit
151+ * @param array $opts
152+ */
153+ public function flush (bool $ nl = false , bool $ quit = false , array $ opts = []): void
73154 {
74- $ this ->buffer = '' ;
75155 }
76156}
0 commit comments