1313use Toolkit \Cli \Traits \WriteMessageTrait ;
1414use function date ;
1515use function defined ;
16- use function fflush ;
17- use function fgets ;
1816use function function_exists ;
19- use function fwrite ;
2017use function getenv ;
2118use function implode ;
22- use function is_array ;
23- use function is_int ;
2419use function is_numeric ;
2520use function json_encode ;
2621use function preg_replace ;
27- use function sprintf ;
2822use function strpos ;
2923use function strtoupper ;
3024use function trim ;
3529use const PHP_WINDOWS_VERSION_BUILD ;
3630use const PHP_WINDOWS_VERSION_MAJOR ;
3731use const PHP_WINDOWS_VERSION_MINOR ;
38- use const STDERR ;
3932use const STDOUT ;
4033
4134/**
4538 */
4639class Cli
4740{
48- // use ReadMessageTrait, WriteMessageTrait;
41+ use ReadMessageTrait, WriteMessageTrait;
4942
5043 public const LOG_LEVEL2TAG = [
5144 'info ' => 'info ' ,
@@ -57,78 +50,17 @@ class Cli
5750 ];
5851
5952 /*******************************************************************************
60- * read message
61- ******************************************************************************/
62-
63- /*******************************************************************************
64- * write message
53+ * color render
6554 ******************************************************************************/
6655
6756 /**
68- * @param string $format
69- * @param mixed ...$args
70- */
71- public static function writef (string $ format , ...$ args ): void
72- {
73- self ::write (sprintf ($ format , ...$ args ));
74- }
75-
76- /**
77- * Write message to console
78- *
79- * @param string|array $messages
80- * @param bool $nl
81- * @param bool|int $quit
82- */
83- public static function write ($ messages , bool $ nl = true , $ quit = false ): void
84- {
85- if (is_array ($ messages )) {
86- $ messages = implode ($ nl ? PHP_EOL : '' , $ messages );
87- }
88-
89- self ::stdout (Color::parseTag ($ messages ), $ nl , $ quit );
90- }
91-
92- /**
93- * Logs data to stdout
94- *
95- * @param string $message
96- * @param bool $nl
97- * @param bool|int $quit
98- */
99- public static function stdout (string $ message , bool $ nl = true , $ quit = false ): void
100- {
101- fwrite (STDOUT , $ message . ($ nl ? PHP_EOL : '' ));
102- fflush (STDOUT );
103-
104- if (($ isTrue = true === $ quit ) || is_int ($ quit )) {
105- $ code = $ isTrue ? 0 : $ quit ;
106- exit ($ code );
107- }
108- }
109-
110- /**
111- * Logs data to stderr
112- *
113- * @param string $message
114- * @param bool $nl
115- * @param bool|int $quit
57+ * @return Style
11658 */
117- public static function stderr ( string $ message , $ nl = true , $ quit = - 1 ): void
59+ public static function style ( ): Style
11860 {
119- fwrite (STDERR , self ::color ('[ERROR] ' , 'red ' ) . $ message . ($ nl ? PHP_EOL : '' ));
120- fflush (STDOUT );
121-
122- if (($ isTrue = true === $ quit ) || is_int ($ quit )) {
123- $ code = $ isTrue ? 0 : $ quit ;
124- exit ($ code );
125- }
61+ return Style::instance ();
12662 }
12763
128- /*******************************************************************************
129- * color render
130- ******************************************************************************/
131-
13264 /**
13365 * @param string $text
13466 * @param string|int|array $style
@@ -154,7 +86,7 @@ public static function color(string $text, $style = null): string
15486 * 'coId' => 12,
15587 * ]
15688 */
157- public static function log (string $ msg , array $ data = [], string $ type = 'info ' , array $ opts = []): void
89+ public static function clog (string $ msg , array $ data = [], string $ type = 'info ' , array $ opts = []): void
15890 {
15991 if (isset (self ::LOG_LEVEL2TAG [$ type ])) {
16092 $ type = ColorTag::add (strtoupper ($ type ), self ::LOG_LEVEL2TAG [$ type ]);
0 commit comments