8000 feature #105 Add logic to new stream functions on Windows (johnsteven… · symfony/polyfill@ce7d99d · GitHub
[go: up one dir, main page]

Skip to content

Commit ce7d99d

Browse files
feature #105 Add logic to new stream functions on Windows (johnstevenson)
This PR was squashed before being merged into the 1.7-dev branch (closes #105). Discussion ---------- Add logic to new stream functions on Windows The tests are pretty useless, but it is impossible to guarantee what the descriptors are pointing to in all test environments. Commits ------- cc2bf55 Add logic to new stream functions on Windows
2 parents fd32cf1 + cc2bf55 commit ce7d99d

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/Php72/Php72.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,34 @@ public static function spl_object_id($object)
9595
return self::$hashMask ^ hexdec(substr($hash, 16 - \PHP_INT_SIZE, \PHP_INT_SIZE));
9696
}
9797

98+
public static function sapi_windows_vt100_support($stream, $enable = null)
99+
{
100+
// We cannot actually disable vt100 support if it is set
101+
if (false === $enable || !self::stream_isatty($stream)) {
102+
return false;
103+
}
104+
105+
// The native function does not apply to stdin
106+
$meta = array_map('strtolower', stream_get_meta_data($stream));
107+
$stdin = 'php://stdin' === $meta['uri'] || 'php://fd/0' === $meta['uri'];
108+
109+
return !$stdin
110+
&& (false !== getenv('ANSICON')
111+
|| 'ON' === getenv('ConEmuANSI')
112+
|| 'xterm' === getenv('TERM'));
113+
}
114+
115+
public static function stream_isatty($stream)
116+
{
117+
if ('\\' === DIRECTORY_SEPARATOR) {
118+
$stat = @fstat($stream);
119+
// Check if formatted mode is S_IFCHR
120+
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
121+
}
122+
123+
return function_exists('posix_isatty') && @posix_isatty($stream);
124+
}
125+
98126
private static function initHashMask()
99127
{
100128
$obj = (object) array();

src/Php72/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
if (PHP_VERSION_ID < 70200) {
1515
if ('\\' === DIRECTORY_SEPARATOR && !function_exists('sapi_windows_vt100_support')) {
16-
function sapi_windows_vt100_support() { return false; }
16+
function sapi_windows_vt100_support($stream, $enable = null) { return p\Php72::sapi_windows_vt100_support($stream, $enable); }
1717
}
1818
if (!function_exists('stream_isatty')) {
19-
function stream_isatty($stream) { return function_exists('posix_isatty') && @posix_isatty($stream); }
19+
function stream_isatty($stream) { return p\Php72::stream_isatty($stream); }
2020
}
2121
if (!function_exists('utf8_encode')) {
2222
function utf8_encode($s) { return p\Php72::utf8_encode($s); }

tests/Php72/Php72Test.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,26 @@ public function testSplObjectId()
6464

6565
$this->assertNull(@spl_object_id(123));
6666
}
67+
68+
/**
69+
* @covers Symfony\Polyfill\Php72\Php72::sapi_windows_vt100_support
70+
*/
71+
public function testSapiWindowsVt100Support()
72+
{
73+
if ('\\' !== DIRECTORY_SEPA 8D8C RATOR) {
74+
$this->markTestSkipped('Windows only test');
75+
}
76+
77+
$this->assertFalse(sapi_windows_vt100_support(STDIN, true));
78+
}
79+
80+
/**
81+
* @covers Symfony\Polyfill\Php72\Php72::stream_isatty
82+
*/
83+
public function testStreamIsatty()
84+
{
85+
$fp = fopen('php://temp', 'r+');
86+
$this->assertFalse(stream_isatty($fp));
87+
fclose($fp);
88+
}
6789
}

0 commit comments

Comments
 (0)
0