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