Skip to content

Commit 141cb0b

Browse files
committed
[Console] remove shell_exec from console component
1 parent 6166fc4 commit 141cb0b

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,11 +1004,11 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
10041004
}
10051005

10061006
if (Terminal::hasSttyAvailable()) {
1007-
$sttyMode = shell_exec('stty -g');
1007+
$sttyMode = exec('stty -g');
10081008

10091009
foreach ([\SIGINT, \SIGTERM] as $signal) {
10101010
$this->signalRegistry->register($signal, static function () use ($sttyMode) {
1011-
shell_exec('stty '.$sttyMode);
1011+
exec('stty '.$sttyMode);
10121012
});
10131013
}
10141014
}

src/Symfony/Component/Console/Cursor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ public function getCurrentPosition(): array
191191
return [1, 1];
192192
}
193193

194-
$sttyMode = shell_exec('stty -g');
195-
shell_exec('stty -icanon -echo');
194+
$sttyMode = exec('stty -g');
195+
exec('stty -icanon -echo');
196196

197197
@fwrite($this->input, "\033[6n");
198198

199199
$code = trim(fread($this->input, 1024));
200200

201-
shell_exec(sprintf('stty %s', $sttyMode));
201+
exec(sprintf('stty %s', $sttyMode));
202202

203203
sscanf($code, "\033[%d;%dR", $row, $col);
204204

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
251251
$matches = $autocomplete($ret);
252252
$numMatches = \count($matches);
253253

254-
$sttyMode = shell_exec('stty -g');
254+
$sttyMode = exec('stty -g');
255255
$isStdin = 'php://stdin' === (stream_get_meta_data($inputStream)['uri'] ?? null);
256256
$r = [$inputStream];
257257
$w = [];
258258

259259
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
260-
shell_exec('stty -icanon -echo');
260+
exec('stty -icanon -echo');
261261

262262
// Add highlighted text style
263263
$output->getFormatter()->setStyle('hl', new OutputFormatterStyle('black', 'white'));
@@ -272,7 +272,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
272272

273273
// as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false.
274274
if (false === $c || ('' === $ret && '' === $c && null === $question->getDefault())) {
275-
shell_exec('stty '.$sttyMode);
275+
exec('stty '.$sttyMode);
276276
throw new MissingInputException('Aborted.');
277277
} elseif ("\177" === $c) { // Backspace Character
278278
if (0 === $numMatches && 0 !== $i) {
@@ -377,7 +377,7 @@ function ($match) use ($ret) {
377377
}
378378

379379
// Reset stty so it behaves normally again
380-
shell_exec('stty '.$sttyMode);
380+
exec('stty '.$sttyMode);
381381

382382
return $fullChoice;
383383
}
@@ -417,8 +417,8 @@ private function getHiddenResponse(OutputInterface $output, $inputStream, bool $
417417
$exe = $tmpExe;
418418
}
419419

420-
$sExec = shell_exec('"'.$exe.'"');
421-
$value = $trimmable ? rtrim($sExec) : $sExec;
420+
$sExec = exec('"'.$exe.'"');
421+
$value = $trimmable ? rtrim($sExec) : $sExec . PHP_EOL; // add PHP_EOL to keep the same result as shell_exec
422422
$output->writeln('');
423423

424424
if (isset($tmpExe)) {
@@ -429,16 +429,16 @@ private function getHiddenResponse(OutputInterface $output, $inputStream, bool $
429429
}
430430

431431
if (self::$stty && Terminal::hasSttyAvailable()) {
432-
$sttyMode = shell_exec('stty -g');
433-
shell_exec('stty -echo');
432+
$sttyMode = exec('stty -g');
433+
exec('stty -echo');
434434
} elseif ($this->isInteractiveInput($inputStream)) {
435435
throw new RuntimeException('Unable to hide the response.');
436436
}
437437

438438
$value = fgets($inputStream, 4096);
439439

440440
if (self::$stty && Terminal::hasSttyAvailable()) {
441-
shell_exec('stty '.$sttyMode);
441+
exec('stty '.$sttyMode);
442442
}
443443

444444
if (false === $value) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,22 +2040,22 @@ public function testSignalableRestoresStty()
20402040
$this->markTestSkipped('pcntl signals not available');
20412041
}
20422042

2043-
$previousSttyMode = shell_exec('stty -g');
2043+
$previousSttyMode = exec('stty -g');
20442044

20452045
$p = new Process(['php', __DIR__.'/Fixtures/application_signalable.php']);
20462046
$p->setTty(true);
20472047
$p->start();
20482048

2049-
for ($i = 0; $i < 10 && shell_exec('stty -g') === $previousSttyMode; ++$i) {
2049+
for ($i = 0; $i < 10 && exec('stty -g') === $previousSttyMode; ++$i) {
20502050
usleep(100000);
20512051
}
20522052

2053-
$this->assertNotSame($previousSttyMode, shell_exec('stty -g'));
2053+
$this->assertNotSame($previousSttyMode, exec('stty -g'));
20542054
$p->signal(\SIGINT);
20552055
$p->wait();
20562056

2057-
$sttyMode = shell_exec('stty -g');
2058-
shell_exec('stty '.$previousSttyMode);
2057+
$sttyMode = exec('stty -g');
2058+
exec('stty '.$previousSttyMode);
20592059

20602060
$this->assertSame($previousSttyMode, $sttyMode);
20612061
}

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()

0 commit comments

Comments
 (0)