TYPO3  7.6
StreamOutput.php
Go to the documentation of this file.
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 
15 
31 class StreamOutput extends Output
32 {
33  private $stream;
34 
47  public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
48  {
49  if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
50  throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
51  }
52 
53  $this->stream = $stream;
54 
55  if (null === $decorated) {
56  $decorated = $this->hasColorSupport();
57  }
58 
59  parent::__construct($verbosity, $decorated, $formatter);
60  }
61 
67  public function getStream()
68  {
69  return $this->stream;
70  }
71 
75  protected function doWrite($message, $newline)
76  {
77  if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) {
78  // should never happen
79  throw new \RuntimeException('Unable to write output.');
80  }
81 
82  fflush($this->stream);
83  }
84 
95  protected function hasColorSupport()
96  {
97  if (DIRECTORY_SEPARATOR === '\\') {
98  return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
99  }
100 
101  return function_exists('posix_isatty') && @posix_isatty($this->stream);
102  }
103 }