22b29aa1731837f923e66cb72f64f0d946624408
[yaffs-website] / 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      * Constructor.
37      *
38      * @param resource                      $stream    A stream resource
39      * @param int                           $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
40      * @param bool|null                     $decorated Whether to decorate messages (null for auto-guessing)
41      * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
42      *
43      * @throws InvalidArgumentException When first argument is not a real stream
44      */
45     public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
46     {
47         if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
48             throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
49         }
50
51         $this->stream = $stream;
52
53         if (null === $decorated) {
54             $decorated = $this->hasColorSupport();
55         }
56
57         parent::__construct($verbosity, $decorated, $formatter);
58     }
59
60     /**
61      * Gets the stream attached to this StreamOutput instance.
62      *
63      * @return resource A stream resource
64      */
65     public function getStream()
66     {
67         return $this->stream;
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     protected function doWrite($message, $newline)
74     {
75         if (false === @fwrite($this->stream, $message) || ($newline && (false === @fwrite($this->stream, PHP_EOL)))) {
76             // should never happen
77             throw new RuntimeException('Unable to write output.');
78         }
79
80         fflush($this->stream);
81     }
82
83     /**
84      * Returns true if the stream supports colorization.
85      *
86      * Colorization is disabled if not supported by the stream:
87      *
88      *  -  Windows != 10.0.10586 without Ansicon, ConEmu or Mintty
89      *  -  non tty consoles
90      *
91      * @return bool true if the stream supports colorization, false otherwise
92      */
93     protected function hasColorSupport()
94     {
95         if (DIRECTORY_SEPARATOR === '\\') {
96             return
97                 '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
98                 || false !== getenv('ANSICON')
99                 || 'ON' === getenv('ConEmuANSI')
100                 || 'xterm' === getenv('TERM');
101         }
102
103         return function_exists('posix_isatty') && @posix_isatty($this->stream);
104     }
105 }