| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Output; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
| 16 |
|
| 17 |
/** |
| 18 |
* StreamOutput writes the output to a given stream. |
| 19 |
* |
| 20 |
* Usage: |
| 21 |
* |
| 22 |
* $output = new StreamOutput(fopen('php://stdout', 'w')); |
| 23 |
* |
| 24 |
* As `StreamOutput` can use any stream, you can also use a file: |
| 25 |
* |
| 26 |
* $output = new StreamOutput(fopen('/path/to/output.log', 'a', false)); |
| 27 |
* |
| 28 |
* @author Fabien Potencier <fabien@symfony.com> |
| 29 |
*/ |
| 30 |
class StreamOutput extends Output |
| 31 |
{ |
| 32 |
private $stream; |
| 33 |
|
| 34 |
/** |
| 35 |
* @param resource $stream A stream resource |
| 36 |
* @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface) |
| 37 |
* @param bool|null $decorated Whether to decorate messages (null for auto-guessing) |
| 38 |
* @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) |
| 39 |
* |
| 40 |
* @throws InvalidArgumentException When first argument is not a real stream |
| 41 |
*/ |
| 42 |
public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null) |
| 43 |
{ |
| 44 |
if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) { |
| 45 |
throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.'); |
| 46 |
} |
| 47 |
|
| 48 |
$this->stream = $stream; |
| 49 |
|
| 50 |
if (null === $decorated) { |
| 51 |
$decorated = $this->hasColorSupport(); |
| 52 |
} |
| 53 |
|
| 54 |
parent::__construct($verbosity, $decorated, $formatter); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Gets the stream attached to this StreamOutput instance. |
| 59 |
* |
| 60 |
* @return resource A stream resource |
| 61 |
*/ |
| 62 |
public function getStream() |
| 63 |
{ |
| 64 |
return $this->stream; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* {@inheritdoc} |
| 69 |
*/ |
| 70 |
protected function doWrite($message, $newline) |
| 71 |
{ |
| 72 |
if ($newline) { |
| 73 |
$message .= \PHP_EOL; |
| 74 |
} |
| 75 |
|
| 76 |
@fwrite($this->stream, $message); |
| 77 |
|
| 78 |
fflush($this->stream); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns true if the stream supports colorization. |
| 83 |
* |
| 84 |
* Colorization is disabled if not supported by the stream: |
| 85 |
* |
| 86 |
* This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo |
| 87 |
* terminals via named pipes, so we can only check the environment. |
| 88 |
* |
| 89 |
* Reference: Composer\XdebugHandler\Process::supportsColor |
| 90 |
* https://github.com/composer/xdebug-handler |
| 91 |
* |
| 92 |
* @return bool true if the stream supports colorization, false otherwise |
| 93 |
*/ |
| 94 |
protected function hasColorSupport() |
| 95 |
{ |
| 96 |
if ('Hyper' === getenv('TERM_PROGRAM')) { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
if (\DIRECTORY_SEPARATOR === '\\') { |
| 101 |
return (\function_exists('sapi_windows_vt100_support') |
| 102 |
&& @sapi_windows_vt100_support($this->stream)) |
| 103 |
|| false !== getenv('ANSICON') |
| 104 |
|| 'ON' === getenv('ConEmuANSI') |
| 105 |
|| 'xterm' === getenv('TERM'); |
| 106 |
} |
| 107 |
|
| 108 |
if (\function_exists('stream_isatty')) { |
| 109 |
return @stream_isatty($this->stream); |
| 110 |
} |
| 111 |
|
| 112 |
if (\function_exists('posix_isatty')) { |
| 113 |
return @posix_isatty($this->stream); |
| 114 |
} |
| 115 |
|
| 116 |
$stat = @fstat($this->stream); |
| 117 |
// Check if formatted mode is S_IFCHR |
| 118 |
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; |
| 119 |
} |
| 120 |
} |
| 121 |
|