Skip to content

Commit 86119ca

Browse files
bug symfony#50338 [Console] Remove exec and replace it by shell_exec (maxbeckers)
This PR was merged into the 5.4 branch. Discussion ---------- [Console] Remove ``exec`` and replace it by ``shell_exec`` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#49646 | License | MIT | Doc PR | n/a It could happen an error like `Error: Call to undefined function Symfony\Component\Console\shell_exec() in /{path_to_project}/vendor/symfony/console/Application.php:989 ` when `exec` is available but `shell_exec` not. There is very often used the check `Terminal::hasSttyAvailable()` in the symfony codebase and then `shell_exec` used. More Details see in the issue symfony#49646. Commits ------- bf1ae1a [Console] Remove exec and replace it by shell_exec
2 parents 3856956 + bf1ae1a commit 86119ca

File tree

4 files changed

+9
-13
lines changed

4 files changed

+9
-13
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,11 @@ private function isInteractiveInput($inputStream): bool
500500
return self::$stdinIsInteractive = @posix_isatty(fopen('php://stdin', 'r'));
501501
}
502502

503-
if (!\function_exists('exec')) {
503+
if (!\function_exists('shell_exec')) {
504504
return self::$stdinIsInteractive = true;
505505
}
506506

507-
exec('stty 2> /dev/null', $output, $status);
508-
509-
return self::$stdinIsInteractive = 1 !== $status;
507+
return self::$stdinIsInteractive = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
510508
}
511509

512510
/**

src/Symfony/Component/Console/Terminal.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,12 @@ public static function hasSttyAvailable(): bool
6464
return self::$stty;
6565
}
6666

67-
// skip check if exec function is disabled
68-
if (!\function_exists('exec')) {
67+
// skip check if shell_exec function is disabled
68+
if (!\function_exists('shell_exec')) {
6969
return false;
7070
}
7171

72-
exec('stty 2>&1', $output, $exitcode);
73-
74-
return self::$stty = 0 === $exitcode;
72+
return self::$stty = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
7573
}
7674

7775
private static function initDimensions()

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ public function testAskHiddenResponse()
430430
$this->assertEquals('8AM', $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("8AM\n")), $this->createOutputInterface(), $question));
431431
}
432432

433-
public function testAskHiddenResponseTrimmed()
433+
public function testAskHiddenResponseNotTrimmed()
434434
{
435435
if ('\\' === \DIRECTORY_SEPARATOR) {
436436
$this->markTestSkipped('This test is not supported on Windows');
@@ -442,7 +442,7 @@ public function testAskHiddenResponseTrimmed()
442442
$question->setHidden(true);
443443
$question->setTrimmable(false);
444444

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

448448
public function testAskMultilineResponseWithEOF()

src/Symfony/Component/Console/Tests/TerminalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public function testSttyOnWindows()
7777
$this->markTestSkipped('Must be on windows');
7878
}
7979

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

0 commit comments

Comments
 (0)