PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / symfony / console / Output / StreamOutput.php

StreamOutput.php in Depicter — Popup & Slider Builder trunk, at vendor/symfony/console/Output/StreamOutput.php

124 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 <[email protected]>
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, int $verbosity = self::VERBOSITY_NORMAL, ?bool $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
61 */
62 public function getStream()
63 {
64 return $this->stream;
65 }
66
67 protected function doWrite(string $message, bool $newline)
68 {
69 if ($newline) {
70 $message .= \PHP_EOL;
71 }
72
73 @fwrite($this->stream, $message);
74
75 fflush($this->stream);
76 }
77
78 /**
79 * Returns true if the stream supports colorization.
80 *
81 * Colorization is disabled if not supported by the stream:
82 *
83 * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
84 * terminals via named pipes, so we can only check the environment.
85 *
86 * Reference: Composer\XdebugHandler\Process::supportsColor
87 * https://github.com/composer/xdebug-handler
88 *
89 * @return bool true if the stream supports colorization, false otherwise
90 */
91 protected function hasColorSupport()
92 {
93 // Follow https://no-color.org/
94 if ('' !== (($_SERVER['NO_COLOR'] ?? getenv('NO_COLOR'))[0] ?? '')) {
95 return false;
96 }
97
98 // Detect msysgit/mingw and assume this is a tty because detection
99 // does not work correctly, see https://github.com/composer/composer/issues/9690
100 if (!@stream_isatty($this->stream) && !\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) {
101 return false;
102 }
103
104 if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($this->stream)) {
105 return true;
106 }
107
108 if ('Hyper' === getenv('TERM_PROGRAM')
109 || false !== getenv('COLORTERM')
110 || false !== getenv('ANSICON')
111 || 'ON' === getenv('ConEmuANSI')
112 ) {
113 return true;
114 }
115
116 if ('dumb' === $term = (string) getenv('TERM')) {
117 return false;
118 }
119
120 // See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157
121 return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);
122 }
123 }
124