Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
}
10 changes: 9 additions & 1 deletion tests/test-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
}
}