Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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\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 <fabien@symfony.com>
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
        $decorated ??= $this->hasColorSupport();
51
 
52
        parent::__construct($verbosity, $decorated, $formatter);
53
    }
54
 
55
    /**
56
     * Gets the stream attached to this StreamOutput instance.
57
     *
58
     * @return resource
59
     */
60
    public function getStream()
61
    {
62
        return $this->stream;
63
    }
64
 
65
    protected function doWrite(string $message, bool $newline)
66
    {
67
        if ($newline) {
68
            $message .= \PHP_EOL;
69
        }
70
 
71
        @fwrite($this->stream, $message);
72
 
73
        fflush($this->stream);
74
    }
75
 
76
    /**
77
     * Returns true if the stream supports colorization.
78
     *
79
     * Colorization is disabled if not supported by the stream:
80
     *
81
     * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
82
     * terminals via named pipes, so we can only check the environment.
83
     *
84
     * Reference: Composer\XdebugHandler\Process::supportsColor
85
     * https://github.com/composer/xdebug-handler
86
     *
87
     * @return bool true if the stream supports colorization, false otherwise
88
     */
89
    protected function hasColorSupport(): bool
90
    {
91
        // Follow https://no-color.org/
92
        if (isset($_SERVER['NO_COLOR']) || false !== getenv('NO_COLOR')) {
93
            return false;
94
        }
95
 
96
        if ('Hyper' === getenv('TERM_PROGRAM')) {
97
            return true;
98
        }
99
 
100
        if (\DIRECTORY_SEPARATOR === '\\') {
101
            return (\function_exists('sapi_windows_vt100_support')
102
                && @sapi_windows_vt100_support($this->stream))
103
                || false !== getenv('ANSICON')
104
                || 'ON' === getenv('ConEmuANSI')
105
                || 'xterm' === getenv('TERM');
106
        }
107
 
108
        return stream_isatty($this->stream);
109
    }
110
}