8000 feature #25142 [Process] Create a "isTtySupported" static method (nesk) · symfony/symfony@04c3712 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04c3712

Browse files
committed
feature #25142 [Process] Create a "isTtySupported" static method (nesk)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Process] Create a "isTtySupported" static method | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | _none_ | License | MIT | Doc PR | _none_ Currently, there is no way to enable the TTY mode without risking an exception. This PR extracts the code checking for TTY support and provides it in a `isTtySupported` static method. Now we can enable the TTY mode everywhere it's available without risking an exception: ```php $process = (new Process)->setTty(Process::isTtySupported()); ``` _Old comment_: > I'm targeting the 2.7 branch since this is not really a new feature, just a little refactoring of the existent code, and it is fully backward compatible. Commits ------- a1398f6 Create a "isTtySupported" static method
2 parents 7c0b1cd + a1398f6 commit 04c3712

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -976,16 +976,9 @@ public function setTty($tty)
976976
if ('\\' === DIRECTORY_SEPARATOR && $tty) {
977977
throw new RuntimeException('TTY mode is not supported on Windows platform.');
978978
}
979-
if ($tty) {
980-
static $isTtySupported;
981979

982-
if (null === $isTtySupported) {
983-
$isTtySupported = (bool) @proc_open('echo 1 >/dev/null', array(array('file', '/dev/tty', 'r'), array('file', '/dev/tty', 'w'), array('file', '/dev/tty', 'w')), $pipes);
984-
}
985-
986-
if (!$isTtySupported) {
987-
throw new RuntimeException('TTY mode requires /dev/tty to be read/writable.');
988-
}
980+
if ($tty && !self::isTtySupported()) {
981+
throw new RuntimeException('TTY mode requires /dev/tty to be read/writable.');
989982
}
990983

991984
$this->tty = (bool) $tty;
@@ -1169,6 +1162,22 @@ public function checkTimeout()
11691162
}
11701163
}
11711164

1165+
/**
1166+
* Returns whether TTY is supported on the current operating system.
1167+
*
1168+
* @return bool
1169+
*/
1170+
public static function isTtySupported()
1171+
{
1172+
static $isTtySupported;
1173+
1174+
if (null === $isTtySupported) {
1175+
$isTtySupported = (bool) @proc_open('echo 1 >/dev/null', array(array('file', '/dev/tty', 'r'), array('file', '/dev/tty', 'w'), array('file', '/dev/tty', 'w')), $pipes);
1176+
}
1177+
1178+
return $isTtySupported;
1179+
}
1180+
11721181
/**
11731182
* Returns whether PTY is supported on the current operating system.
11741183
*

0 commit comments

Comments
 (0)
0