Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Dec 7, 2017
2 parents 7c0b1cd + a1398f6 commit 04c3712
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/Symfony/Component/Process/Process.php
Expand Up @@ -976,16 +976,9 @@ public function setTty($tty)
if ('\\' === DIRECTORY_SEPARATOR && $tty) {
throw new RuntimeException('TTY mode is not supported on Windows platform.');
}
if ($tty) {
static $isTtySupported;

if (null === $isTtySupported) {
$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);
}

if (!$isTtySupported) {
throw new RuntimeException('TTY mode requires /dev/tty to be read/writable.');
}
if ($tty && !self::isTtySupported()) {
throw new RuntimeException('TTY mode requires /dev/tty to be read/writable.');
}

$this->tty = (bool) $tty;
Expand Down Expand Up @@ -1169,6 +1162,22 @@ public function checkTimeout()
}
}

/**
* Returns whether TTY is supported on the current operating system.
*
* @return bool
*/
public static function isTtySupported()
{
static $isTtySupported;

if (null === $isTtySupported) {
$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);
}

return $isTtySupported;
}

/**
* Returns whether PTY is supported on the current operating system.
*
Expand Down

0 comments on commit 04c3712

Please sign in to comment.