From 36989c56f4ef6fa6f39d877c2aa6566a2e81094f Mon Sep 17 00:00:00 2001 From: Andy Skelton Date: Mon, 28 Jun 2021 10:24:50 -0500 Subject: [PATCH] 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)); => Warning: posix_isatty(): could not use stream of type 'Output' in /usr/local/var/www/wp-cli/php/boot-fpm.php on line 22
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. --- lib/cli/Shell.php | 6 +++++- lib/cli/Streams.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/cli/Shell.php b/lib/cli/Shell.php index 9479c6b..037fe77 100755 --- a/lib/cli/Shell.php +++ b/lib/cli/Shell.php @@ -91,7 +91,11 @@ static public function isPiped() { if ($shellPipe !== false) { return filter_var($shellPipe, FILTER_VALIDATE_BOOLEAN); } else { - return (function_exists('posix_isatty') && !posix_isatty(STDOUT)); + if ( function_exists('stream_isatty') ) { + return !stream_isatty(STDOUT); + } else { + return (function_exists('posix_isatty') && !posix_isatty(STDOUT)); + } } } diff --git a/lib/cli/Streams.php b/lib/cli/Streams.php index 85e2929..7322760 100755 --- a/lib/cli/Streams.php +++ b/lib/cli/Streams.php @@ -14,7 +14,11 @@ static function _call( $func, $args ) { } static public function isTty() { - return (function_exists('posix_isatty') && posix_isatty(static::$out)); + if ( function_exists('stream_isatty') ) { + return !stream_isatty(static::$out); + } else { + return (function_exists('posix_isatty') && !posix_isatty(static::$out)); + } } /**