10
10
* @license MIT
11
11
* @copyright Copyright (C) JBZoo.com, All rights reserved.
12
12
* @link https://github.com/JBZoo/Utils
13
+ * @author Sebastian Bergmann <[email protected] >
13
14
* @author Denis Smetannikov <[email protected] >
14
15
*/
15
16
24
25
*/
25
26
class Cli
26
27
{
28
+ const STDIN = 0 ;
29
+ const STDOUT = 1 ;
30
+ const STDERR = 2 ;
31
+
27
32
/**
28
33
* Is command line
29
34
*
@@ -88,7 +93,7 @@ public static function err($message, $addEol = true)
88
93
public static function exec ($ command , $ args = array (), $ cwd = null , $ verbose = false )
89
94
{
90
95
if (!class_exists ('\Symfony\Component\Process\Process ' )) {
91
- throw new \Exception ("Symfony/Process package required for Cli::exec() method " );
96
+ throw new \Exception ("Symfony/Process package required for Cli::exec() method " ); // @codeCoverageIgnore
92
97
}
93
98
94
99
$ cmd = self ::build ($ command , $ args );
@@ -147,7 +152,7 @@ public static function build($command, $args = array())
147
152
}
148
153
}
149
154
150
- if ($ value ) {
155
+ if (strlen ( $ value) > 0 ) {
151
156
$ stringArgs [] = $ key . '=" ' . addcslashes ($ value , '" ' ) . '" ' ;
152
157
} else {
153
158
$ stringArgs [] = $ key ;
@@ -161,4 +166,102 @@ public static function build($command, $args = array())
161
166
162
167
return $ realCommand ;
163
168
}
169
+
170
+ /**
171
+ * Returns true if STDOUT supports colorization.
172
+ *
173
+ * This code has been copied and adapted from
174
+ * Symfony\Component\Console\Output\OutputStream.
175
+ *
176
+ * @return bool
177
+ * @codeCoverageIgnore
178
+ */
179
+ public static function hasColorSupport ()
180
+ {
181
+ if (DIRECTORY_SEPARATOR == '\\' ) {
182
+
183
+ $ winColor = Env::get ('ANSICON ' , Env::VAR_BOOL )
184
+ || 'ON ' === Env::get ('ConEmuANSI ' )
185
+ || 'xterm ' === Env::get ('TERM ' );
186
+
187
+ return $ winColor ;
188
+ }
189
+
190
+ if (!defined ('STDOUT ' )) {
191
+ return false ;
192
+ }
193
+
194
+ return self ::isInteractive (STDOUT );
195
+ }
196
+
197
+ /**
198
+ * Returns the number of columns of the terminal.
199
+ *
200
+ * @return int
201
+ * @codeCoverageIgnore
202
+ */
203
+ public static function getNumberOfColumns ()
204
+ {
205
+ if (DIRECTORY_SEPARATOR == '\\' ) {
206
+ $ columns = 80 ;
207
+
208
+ if (preg_match ('/^(\d+)x\d+ \(\d+x(\d+)\)$/ ' , trim (getenv ('ANSICON ' )), $ matches )) {
209
+ $ columns = $ matches [1 ];
210
+
211
+ } elseif (function_exists ('proc_open ' )) {
212
+ $ process = proc_open (
213
+ 'mode CON ' ,
214
+ array (
215
+ 1 => array ('pipe ' , 'w ' ),
216
+ 2 => array ('pipe ' , 'w ' ),
217
+ ),
218
+ $ pipes ,
219
+ null ,
220
+ null ,
221
+ array ('suppress_errors ' => true )
222
+ );
223
+
224
+ if (is_resource ($ process )) {
225
+ $ info = stream_get_contents ($ pipes [1 ]);
226
+ fclose ($ pipes [1 ]);
227
+ fclose ($ pipes [2 ]);
228
+ proc_close ($ process );
229
+ if (preg_match ('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/ ' , $ info , $ matches )) {
230
+ $ columns = $ matches [2 ];
231
+ }
232
+ }
233
+ }
234
+
235
+ return $ columns - 1 ;
236
+ }
237
+
238
+ if (!self ::isInteractive (self ::STDIN )) {
239
+ return 80 ;
240
+ }
241
+
242
+ if (preg_match ('#\d+ (\d+)# ' , shell_exec ('stty size ' ), $ match ) === 1 ) {
243
+ if ((int )$ match [1 ] > 0 ) {
244
+ return (int )$ match [1 ];
245
+ }
246
+ }
247
+
248
+ if (preg_match ('#columns = (\d+);# ' , shell_exec ('stty ' ), $ match ) === 1 ) {
249
+ if ((int )$ match [1 ] > 0 ) {
250
+ return (int )$ match [1 ];
251
+ }
252
+ }
253
+
254
+ return 80 ;
255
+ }
256
+
257
+ /**
258
+ * Returns if the file descriptor is an interactive terminal or not.
259
+ *
260
+ * @param int|resource $fileDescriptor
261
+ * @return bool
262
+ */
263
+ public static function isInteractive ($ fileDescriptor = self ::STDOUT )
264
+ {
265
+ return function_exists ('posix_isatty ' ) && @posix_isatty ($ fileDescriptor );
266
+ }
164
267
}
0 commit comments