8000 Add logic to new stream functions on Windows by johnstevenson · Pull Request #105 · symfony/polyfill · GitHub
[go: up one dir, main page]

Skip to content

Add logic to new stream functions on Windows #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Php72/Php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,34 @@ public static function spl_object_id($object)
return self::$hashMask ^ hexdec(substr($hash, 16 - \PHP_INT_SIZE, \PHP_INT_SIZE));
}

public static function sapi_windows_vt100_support($stream, $enable = null)
{
// We cannot actually disable vt100 support if it is set
if (false === $enable || !self::stream_isatty($stream)) {
return false;
}

// The native function does not apply to stdin
$meta = array_map('strtolower', stream_get_meta_data($stream));
$stdin = 'php://stdin' === $meta['uri'] || 'php://fd/0' === $meta['uri'];

return !$stdin
&& (false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
|| 'xterm' === getenv('TERM'));
}

public static function stream_isatty($stream)
{
if ('\\' === DIRECTORY_SEPARATOR) {
$stat = @fstat($stream);
// Check if formatted mode is S_IFCHR
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
}

return function_exists('posix_isatty') && @posix_isatty($stream);
}

private static function initHashMask()
{
$obj = (object) array();
Expand Down
4 changes: 2 additions & 2 deletions src/Php72/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

if (PHP_VERSION_ID < 70200) {
if ('\\' === DIRECTORY_SEPARATOR && !function_exists('sapi_windows_vt100_support')) {
function sapi_windows_vt100_support() { return false; }
function sapi_windows_vt100_support($stream, $enable = null) { return p\Php72::sapi_windows_vt100_support($stream, $enable); }
}
if (!function_exists('stream_isatty')) {
function stream_isatty($stream) { return function_exists('posix_isatty') && @posix_isatty($stream); }
function stream_isatty($stream) { return p\Php72::stream_isatty($stream); }
}
if (!function_exists('utf8_encode')) {
function utf8_encode($s) { return p\Php72::utf8_encode($s); }
Expand Down
22 changes: 22 additions & 0 deletions tests/Php72/Php72Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,26 @@ public function testSplObjectId()

$this->assertNull(@spl_object_id(123));
}

/**
* @covers Symfony\Polyfill\Php72\Php72::sapi_windows_vt100_support
*/
public function testSapiWindowsVt100Support()
{
if ('\\' !== DIRECTORY_SEPARATOR) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@requires OSFAMILY ... instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code matches the convention we're following, LGTM as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's behind that convention? looks pretty weird to me to not use solution provided by test runner but do it manually

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno exactly, maybe just discoverability is better, so more commonly used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it whatever way you chaps want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep it this way, that's consistent with the code base

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reason is support for older PHPUnit versions (IIRC, OSFAMILY was added later)

$this->markTestSkipped('Windows only test');
}

$this->assertFalse(sapi_windows_vt100_support(STDIN, true));
}

/**
* @covers Symfony\Polyfill\Php72\Php72::stream_isatty
*/
public function testStreamIsatty()
{
$fp = fopen('php://temp', 'r+');
$this->assertFalse(stream_isatty($fp));
fclose($fp);
}
}
0