13
13
use Toolkit \Cli \Traits \WriteMessageTrait ;
14
14
use function date ;
15
15
use function defined ;
16
- use function fflush ;
17
- use function fgets ;
18
16
use function function_exists ;
19
- use function fwrite ;
20
17
use function getenv ;
21
18
use function implode ;
22
- use function is_array ;
23
- use function is_int ;
24
19
use function is_numeric ;
25
20
use function json_encode ;
26
21
use function preg_replace ;
27
- use function sprintf ;
28
22
use function strpos ;
29
23
use function strtoupper ;
30
24
use function trim ;
35
29
use const PHP_WINDOWS_VERSION_BUILD ;
36
30
use const PHP_WINDOWS_VERSION_MAJOR ;
37
31
use const PHP_WINDOWS_VERSION_MINOR ;
38
- use const STDERR ;
39
32
use const STDOUT ;
40
33
41
34
/**
45
38
*/
46
39
class Cli
47
40
{
48
- // use ReadMessageTrait, WriteMessageTrait;
41
+ use ReadMessageTrait, WriteMessageTrait;
49
42
50
43
public const LOG_LEVEL2TAG = [
51
44
'info ' => 'info ' ,
@@ -57,78 +50,17 @@ class Cli
57
50
];
58
51
59
52
/*******************************************************************************
60
- * read message
61
- ******************************************************************************/
62
-
63
- /*******************************************************************************
64
- * write message
53
+ * color render
65
54
******************************************************************************/
66
55
67
56
/**
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
116
58
*/
117
- public static function stderr ( string $ message , $ nl = true , $ quit = - 1 ): void
59
+ public static function style ( ): Style
118
60
{
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 ();
126
62
}
127
63
128
- /*******************************************************************************
129
- * color render
130
- ******************************************************************************/
131
-
132
64
/**
133
65
* @param string $text
134
66
* @param string|int|array $style
@@ -154,7 +86,7 @@ public static function color(string $text, $style = null): string
154
86
* 'coId' => 12,
155
87
* ]
156
88
*/
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
158
90
{
159
91
if (isset (self ::LOG_LEVEL2TAG [$ type ])) {
160
92
$ type = ColorTag::add (strtoupper ($ type ), self ::LOG_LEVEL2TAG [$ type ]);
0 commit comments