diff --git a/php/utils.php b/php/utils.php index 986fd29235..e4798e5410 100644 --- a/php/utils.php +++ b/php/utils.php @@ -428,7 +428,7 @@ function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) { $old_pass = getenv( 'MYSQL_PWD' ); putenv( 'MYSQL_PWD=' . $pass ); - $final_cmd = $cmd . assoc_args_to_str( $assoc_args ); + $final_cmd = maybe_prefix_env( $cmd ) . assoc_args_to_str( $assoc_args ); $proc = proc_open( $final_cmd, $descriptors, $pipes ); if ( !$proc ) @@ -980,3 +980,26 @@ function is_bundled_command( $command ) { ? array_key_exists( $command, $classes ) : false; } + +/** + * Maybe prefix command string with "/usr/bin/env". + * Removes (if there) if Windows, adds (if not there) if not. + * + * @param string $command + * + * @return string + */ +function maybe_prefix_env( $command ) { + $env_prefix = '/usr/bin/env '; + $env_prefix_len = strlen( $env_prefix ); + if ( is_windows() ) { + if ( 0 === strncmp( $command, $env_prefix, $env_prefix_len ) ) { + $command = substr( $command, $env_prefix_len ); + } + } else { + if ( 0 !== strncmp( $command, $env_prefix, $env_prefix_len ) ) { + $command = $env_prefix . $command; + } + } + return $command; +} diff --git a/tests/test-utils.php b/tests/test-utils.php index 9363c99d17..519029fd41 100644 --- a/tests/test-utils.php +++ b/tests/test-utils.php @@ -143,5 +143,13 @@ public function testAssocArgsToString() { ) ) ); } + public function testMaybePreviewEnv() { + if ( 0 === strncasecmp( 'WIN', PHP_OS, 3 ) ) { + $this->assertSame( 'cmd', Utils\maybe_prefix_env( 'cmd' ) ); + $this->assertSame( 'cmd', Utils\maybe_prefix_env( '/usr/bin/env cmd' ) ); + } else { + $this->assertSame( '/usr/bin/env cmd', Utils\maybe_prefix_env( 'cmd' ) ); + $this->assertSame( '/usr/bin/env cmd', Utils\maybe_prefix_env( '/usr/bin/env cmd' ) ); + } + } } -