Updated to Drupal 8.5. Core Media not yet in use.
[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 (false === @fwrite($this->stream, $message) || ($newline && (false === @fwrite($this->stream, PHP_EOL)))) {
74             // should never happen
75             throw new RuntimeException('Unable to write output.');
76         }
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 (DIRECTORY_SEPARATOR === '\\') {
97             return (function_exists('sapi_windows_vt100_support')
98                 && @sapi_windows_vt100_support($this->stream))
99                 || false !== getenv('ANSICON')
100                 || 'ON' === getenv('ConEmuANSI')
101                 || 'xterm' === getenv('TERM');
102         }
103
104         if (function_exists('stream_isatty')) {
105             return @stream_isatty($this->stream);
106         }
107
108         if (function_exists('posix_isatty')) {
109             return @posix_isatty($this->stream);
110         }
111
112         $stat = @fstat($this->stream);
113         // Check if formatted mode is S_IFCHR
114         return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
115     }
116 }