From c5fa366fc6b9c231ba5095fb9bfbd499e7b85af3 Mon Sep 17 00:00:00 2001 From: johnstevenson Date: Tue, 21 Nov 2017 20:01:29 +0000 Subject: [PATCH 1/4] Add logic to new stream functions on Windows --- src/Php72/Php72.php | 29 +++++++++++++++++++++++++++++ src/Php72/bootstrap.php | 4 ++-- tests/Php72/Php72Test.php | 22 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/Php72/Php72.php b/src/Php72/Php72.php index 5925c1b9a..25143d932 100644 --- a/src/Php72/Php72.php +++ b/src/Php72/Php72.php @@ -95,6 +95,35 @@ 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 enable vt100 support + if (true === $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') + || 'cygwin' === 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(); diff --git a/src/Php72/bootstrap.php b/src/Php72/bootstrap.php index b6522e84f..9ccea829d 100644 --- a/src/Php72/bootstrap.php +++ b/src/Php72/bootstrap.php @@ -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); } diff --git a/tests/Php72/Php72Test.php b/tests/Php72/Php72Test.php index 349eef6a4..5e3e5ad30 100644 --- a/tests/Php72/Php72Test.php +++ b/tests/Php72/Php72Test.php @@ -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) { + $this->markTestSkipped('Windows only test'); + } + + $this->assertFalse(p::sapi_windows_vt100_support(STDOUT, true)); + } + + /** + * @covers Symfony\Polyfill\Php72\Php72::stream_isatty + */ + public function testStreamIsatty() + { + $fp = fopen('php://temp', 'r+'); + $this->assertFalse(p::stream_isatty($fp)); + fclose($fp); + } } From 85ab2ddf386f09e7ae0e45f041e816fbe51c1b1d Mon Sep 17 00:00:00 2001 From: johnstevenson Date: Thu, 23 Nov 2017 11:06:37 +0000 Subject: [PATCH 2/4] Fix sapi_windows_vt100_support --- src/Php72/Php72.php | 4 ++-- tests/Php72/Php72Test.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Php72/Php72.php b/src/Php72/Php72.php index 25143d932..221edab70 100644 --- a/src/Php72/Php72.php +++ b/src/Php72/Php72.php @@ -97,8 +97,8 @@ public static function spl_object_id($object) public static function sapi_windows_vt100_support($stream, $enable = null) { - // We cannot actually enable vt100 support - if (true === $enable || !self::stream_isatty($stream)) { + // We cannot actually disable vt100 support if it is set + if (false === $enable || !self::stream_isatty($stream)) { return false; } diff --git a/tests/Php72/Php72Test.php b/tests/Php72/Php72Test.php index 5e3e5ad30..9b2a1ca93 100644 --- a/tests/Php72/Php72Test.php +++ b/tests/Php72/Php72Test.php @@ -74,7 +74,7 @@ public function testSapiWindowsVt100Support() $this->markTestSkipped('Windows only test'); } - $this->assertFalse(p::sapi_windows_vt100_support(STDOUT, true)); + $this->assertFalse(p::sapi_windows_vt100_support(STDOUT, false)); } /** From d2581769bbc6ed60e17ad09978558f9175cd0f67 Mon Sep 17 00:00:00 2001 From: johnstevenson Date: Fri, 1 Dec 2017 17:28:30 +0000 Subject: [PATCH 3/4] Remove check for TERM=cygwin --- src/Php72/Php72.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Php72/Php72.php b/src/Php72/Php72.php index 221edab70..f4201ffd3 100644 --- a/src/Php72/Php72.php +++ b/src/Php72/Php72.php @@ -109,8 +109,7 @@ public static function sapi_windows_vt100_support($stream, $enable = null) return !$stdin && (false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') - || 'xterm' === getenv('TERM') - || 'cygwin' === getenv('TERM')); + || 'xterm' === getenv('TERM')); } public static function stream_isatty($stream) From 4763d266b74883640445ee5c4f94df6418295ebb Mon Sep 17 00:00:00 2001 From: johnstevenson Date: Tue, 30 Jan 2018 18:43:05 +0000 Subject: [PATCH 4/4] Fix tests --- tests/Php72/Php72Test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Php72/Php72Test.php b/tests/Php72/Php72Test.php index 9b2a1ca93..6f79d9a7d 100644 --- a/tests/Php72/Php72Test.php +++ b/tests/Php72/Php72Test.php @@ -74,7 +74,7 @@ public function testSapiWindowsVt100Support() $this->markTestSkipped('Windows only test'); } - $this->assertFalse(p::sapi_windows_vt100_support(STDOUT, false)); + $this->assertFalse(sapi_windows_vt100_support(STDIN, true)); } /** @@ -83,7 +83,7 @@ public function testSapiWindowsVt100Support() public function testStreamIsatty() { $fp = fopen('php://temp', 'r+'); - $this->assertFalse(p::stream_isatty($fp)); + $this->assertFalse(stream_isatty($fp)); fclose($fp); } }