Skip to content

Commit 36989c5

Browse files
committed
Update TTY checks
The function `stream_isatty()` ([PHP manual](https://www.php.net/manual/en/function.stream-isatty.php)) is available since PHP 7.2.0/8.0. > Determines if stream stream refers to a valid terminal type device. This is a more portable version of posix_isatty(), since it works on Windows systems too. This update also enables the use of an Output stream as an argument without triggering a warning, as in the folowing code. ``` define( 'STDOUT', fopen( 'php://output', 'w' ) ); var_dump(posix_isatty(STDOUT)); var_dump(stream_isatty(STDOUT)); => <b>Warning</b>: posix_isatty(): could not use stream of type 'Output' in <b>/usr/local/var/www/wp-cli/php/boot-fpm.php</b> on line <b>22</b><br /> bool(false) bool(false) ``` It is necessary to define `STDOUT` in this way while using the `fpm-fcgi` SAPI. This is useful when running lots of WP-CLI commands in a high-volume task scheduler where up to 95% of CPU time is wasted on the redundant work of loading PHP code. We found that php-fpm's opcode caching reduces resource usage significantly, idling hundreds of CPUs that are otherwise occupied loading the same code over and over again. The `fpm-fcgi` SAPI runs WP CLI through a modified boot script. A command line program passes commands via an FCGI client and returns results on standard streams. This work will also be contributed to the `wp-cli` project.
1 parent e472e08 commit 36989c5

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/cli/Shell.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ static public function isPiped() {
9191
if ($shellPipe !== false) {
9292
return filter_var($shellPipe, FILTER_VALIDATE_BOOLEAN);
9393
} else {
94-
return (function_exists('posix_isatty') && !posix_isatty(STDOUT));
94+
if ( function_exists('stream_isatty') ) {
95+
return !stream_isatty(STDOUT);
96+
} else {
97+
return (function_exists('posix_isatty') && !posix_isatty(STDOUT));
98+
}
9599
}
96100
}
97101

lib/cli/Streams.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ static function _call( $func, $args ) {
1414
}
1515

1616
static public function isTty() {
17-
return (function_exists('posix_isatty') && posix_isatty(static::$out));
17+
if ( function_exists('stream_isatty') ) {
18+
return !stream_isatty(static::$out);
19+
} else {
20+
return (function_exists('posix_isatty') && !posix_isatty(static::$out));
21+
}
1822
}
1923

2024
/**

0 commit comments

Comments
 (0)