From baf4eb2f03558de2fcf820b51a6b0340c0f96ed9 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 14 Nov 2023 12:14:59 +0000 Subject: [PATCH] Add WP_CLI\Utils\has_stdin() function --- php/utils.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/php/utils.php b/php/utils.php index 90c940e8f..6daaeb171 100644 --- a/php/utils.php +++ b/php/utils.php @@ -1845,3 +1845,19 @@ function get_cache_dir() { $home = get_home_dir(); return getenv( 'WP_CLI_CACHE_DIR' ) ? : "$home/.wp-cli/cache"; } + +/** + * Check whether any input is passed to STDIN. + * + * @return bool + */ +function has_stdin() { + $handle = fopen( 'php://stdin', 'r' ); + $read = array( $handle ); + $write = null; + $except = null; + $streams = stream_select( $read, $write, $except, 0 ); + fclose( $handle ); + + return 1 === $streams; +}