Skip to content

Commit b0c9225

Browse files
johnstevensonfabpot
authored andcommitted
Use new PHP7.2 functions in hasColorSupport
1 parent 1067468 commit b0c9225

File tree

3 files changed

+114
-38
lines changed

3 files changed

+114
-38
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

+26-5
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,38 @@ public static function register($mode = false)
178178
}
179179
}
180180

181+
/**
182+
* Returns true if STDOUT is defined and supports colorization.
183+
*
184+
* Reference: Composer\XdebugHandler\Process::supportsColor
185+
* https://github.com/composer/xdebug-handler
186+
*
187+
* @return bool
188+
*/
181189
private static function hasColorSupport()
182190
{
183-
if ('\\' === DIRECTORY_SEPARATOR) {
184-
return
185-
defined('STDOUT') && function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(STDOUT)
186-
|| '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
191+
if (!defined('STDOUT')) {
192+
return false;
193+
}
194+
195+
if (DIRECTORY_SEPARATOR === '\\') {
196+
return (function_exists('sapi_windows_vt100_support')
197+
&& sapi_windows_vt100_support(STDOUT))
187198
|| false !== getenv('ANSICON')
188199
|| 'ON' === getenv('ConEmuANSI')
189200
|| 'xterm' === getenv('TERM');
190201
}
191202

192-
return defined('STDOUT') && function_exists('posix_isatty') && @posix_isatty(STDOUT);
203+
if (function_exists('stream_isatty')) {
204+
return stream_isatty(STDOUT);
205+
}
206+
207+
if (function_exists('posix_isatty')) {
208+
return posix_isatty(STDOUT);
209+
}
210+
211+
$stat = fstat(STDOUT);
212+
// Check if formatted mode is S_IFCHR
213+
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
193214
}
194215
}

src/Symfony/Component/Console/Output/StreamOutput.php

+18-15
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,34 @@ protected function doWrite($message, $newline)
8181
*
8282
* Colorization is disabled if not supported by the stream:
8383
*
84-
* - the stream is redirected (eg php file.php >log)
85-
* - Windows without VT100 support, Ansicon, ConEmu, Mintty
86-
* - non tty consoles
84+
* This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
85+
* terminals via named pipes, so we can only check the environment.
86+
*
87+
* Reference: Composer\XdebugHandler\Process::supportsColor
88+
* https://github.com/composer/xdebug-handler
8789
*
8890
* @return bool true if the stream supports colorization, false otherwise
8991
*/
9092
protected function hasColorSupport()
9193
{
92-
if (function_exists('stream_isatty') && !@stream_isatty($this->stream)) {
93-
return false;
94-
}
9594
if (DIRECTORY_SEPARATOR === '\\') {
96-
if (function_exists('sapi_windows_vt100_support')) {
97-
$vt100Enabled = @sapi_windows_vt100_support($this->stream);
98-
} else {
99-
$vt100Enabled = '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD;
100-
}
101-
102-
return
103-
$vt100Enabled
95+
return (function_exists('sapi_windows_vt100_support')
96+
&& @sapi_windows_vt100_support($this->stream))
10497
|| false !== getenv('ANSICON')
10598
|| 'ON' === getenv('ConEmuANSI')
10699
|| 'xterm' === getenv('TERM');
107100
}
108101

109-
return function_exists('posix_isatty') && @posix_isatty($this->stream);
102+
if (function_exists('stream_isatty')) {
103+
return @stream_isatty($this->stream);
104+
}
105+
106+
if (function_exists('posix_isatty')) {
107+
return @posix_isatty($this->stream);
108+
}
109+
110+
$stat = @fstat($this->stream);
111+
// Check if formatted mode is S_IFCHR
112+
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
110113
}
111114
}

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

+70-18
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct($output = null, $charset = null)
5858
{
5959
parent::__construct($output, $charset);
6060

61-
if ('\\' === DIRECTORY_SEPARATOR && 'ON' !== @getenv('ConEmuANSI') && 'xterm' !== @getenv('TERM')) {
61+
if ('\\' === DIRECTORY_SEPARATOR && !$this->isWindowsTrueColor()) {
6262
// Use only the base 16 xterm colors when using ANSICON or standard Windows 10 CLI
6363
$this->setStyles(array(
6464
'default' => '31',
@@ -420,7 +420,7 @@ protected function style($style, $value, $attr = array())
420420
protected function supportsColors()
421421
{
422422
if ($this->outputStream !== static::$defaultOutput) {
423-
return @(is_resource($this->outputStream) && function_exists('posix_isatty') && posix_isatty($this->outputStream));
423+
return $this->hasColorSupport($this->outputStream);
424424
}
425425
if (null !== static::$defaultColors) {
426426
return static::$defaultColors;
@@ -448,23 +448,10 @@ protected function supportsColors()
448448
}
449449
}
450450

451-
if ('\\' === DIRECTORY_SEPARATOR) {
452-
static::$defaultColors = @(
453-
function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support($this->outputStream)
454-
|| '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
455-
|| false !== getenv('ANSICON')
456-
|| 'ON' === getenv('ConEmuANSI')
457-
|| 'xterm' === getenv('TERM')
458-
);
459-
} elseif (function_exists('posix_isatty')) {
460-
$h = stream_get_meta_data($this->outputStream) + array('wrapper_type' => null);
461-
$h = 'Output' === $h['stream_type'] && 'PHP' === $h['wrapper_type'] ? fopen('php://stdout', 'wb') : $this->outputStream;
462-
static::$defaultColors = @posix_isatty($h);
463-
} else {
464-
static::$defaultColors = false;
465-
}
451+
$h = stream_get_meta_data($this->outputStream) + array('wrapper_type' => null);
452+
$h = 'Output' === $h['stream_type'] && 'PHP' === $h['wrapper_type'] ? fopen('php://stdout', 'wb') : $this->outputStream;
466453

467-
return static::$defaultColors;
454+
return static::$defaultColors = $this->hasColorSupport($h);
468455
}
469456

470457
/**
@@ -477,4 +464,69 @@ protected function dumpLine($depth, $endOfValue = false)
477464
}
478465
parent::dumpLine($depth);
479466
}
467+
468+
/**
469+
* Returns true if the stream supports colorization.
470+
*
471+
* Reference: Composer\XdebugHandler\Process::supportsColor
472+
* https://github.com/composer/xdebug-handler
473+
*
474+
* @param mixed $stream A CLI output stream
475+
*
476+
* @return bool
477+
*/
478+
private function hasColorSupport($stream)
479+
{
480+
if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
481+
return false;
482+
}
483+
484+
if (DIRECTORY_SEPARATOR === '\\') {
485+
return (function_exists('sapi_windows_vt100_support')
486+
&& @sapi_windows_vt100_support($stream))
487+
|| false !== getenv('ANSICON')
488+
|| 'ON' === getenv('ConEmuANSI')
489+
|| 'xterm' === getenv('TERM');
490+
}
491+
492+
if (function_exists('stream_isatty')) {
493+
return @stream_isatty($stream);
494+
}
495+
496+
if (function_exists('posix_isatty')) {
497+
return @posix_isatty($stream);
498+
}
499+
500+
$stat = @fstat($stream);
501+
// Check if formatted mode is S_IFCHR
502+
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
503+
}
504+
505+
/**
506+
* Returns true if the Windows terminal supports true color.
507+
*
508+
* Note that this does not check an output stream, but relies on environment
509+
* variables from known implementations, or a PHP and Windows version that
510+
* supports true color.
511+
*
512+
* @return bool
513+
*/
514+
private function isWindowsTrueColor()
515+
{
516+
$result = strval(getenv('ANSICON_VER')) >= '183'
517+
|| 'ON' === getenv('ConEmuANSI')
518+
|| 'xterm' === getenv('TERM');
519+
520+
if (!$result && PHP_VERSION_ID >= 70200) {
521+
$version = sprintf(
522+
'%s.%s.%s',
523+
PHP_WINDOWS_VERSION_MAJOR,
524+
PHP_WINDOWS_VERSION_MINOR,
525+
PHP_WINDOWS_VERSION_BUILD
526+
);
527+
$result = $version >= '10.0.15063';
528+
}
529+
530+
return $result;
531+
}
480532
}

0 commit comments

Comments
 (0)