Skip to content

Commit

Permalink
[Console] Remove exec and replace it by shell_exec
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbeckers committed May 19, 2023
1 parent 9a16a1c commit a349882
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
6 changes: 2 additions & 4 deletions Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,11 @@ private function isInteractiveInput($inputStream): bool
return self::$stdinIsInteractive = @posix_isatty(fopen('php://stdin', 'r'));
}

if (!\function_exists('exec')) {
if (!\function_exists('shell_exec')) {
return self::$stdinIsInteractive = true;
}

exec('stty 2> /dev/null', $output, $status);

return self::$stdinIsInteractive = 1 !== $status;
return self::$stdinIsInteractive = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
}

/**
Expand Down
8 changes: 3 additions & 5 deletions Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ public static function hasSttyAvailable(): bool
return self::$stty;
}

// skip check if exec function is disabled
if (!\function_exists('exec')) {
// skip check if shell_exec function is disabled
if (!\function_exists('shell_exec')) {
return false;
}

exec('stty 2>&1', $output, $exitcode);

return self::$stty = 0 === $exitcode;
return self::$stty = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
}

private static function initDimensions()
Expand Down
4 changes: 2 additions & 2 deletions Tests/Helper/QuestionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public function testAskHiddenResponse()
$this->assertEquals('8AM', $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("8AM\n")), $this->createOutputInterface(), $question));
}

public function testAskHiddenResponseTrimmed()
public function testAskHiddenResponseNotTrimmed()
{
if ('\\' === \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('This test is not supported on Windows');
Expand All @@ -442,7 +442,7 @@ public function testAskHiddenResponseTrimmed()
$question->setHidden(true);
$question->setTrimmable(false);

$this->assertEquals(' 8AM', $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream(' 8AM')), $this->createOutputInterface(), $question));
$this->assertEquals(' 8AM'.\PHP_EOL, $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream(' 8AM'.\PHP_EOL)), $this->createOutputInterface(), $question));
}

public function testAskMultilineResponseWithEOF()
Expand Down
4 changes: 2 additions & 2 deletions Tests/TerminalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public function testSttyOnWindows()
$this->markTestSkipped('Must be on windows');
}

$sttyString = exec('(stty -a | grep columns) 2>&1', $output, $exitcode);
if (0 !== $exitcode) {
$sttyString = shell_exec('(stty -a | grep columns) 2> NUL');
if (!$sttyString) {
$this->markTestSkipped('Must have stty support');
}

Expand Down

0 comments on commit a349882

Please sign in to comment.