Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / console / Output / ConsoleOutput.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\Formatter\OutputFormatterInterface;
15
16 /**
17  * ConsoleOutput is the default class for all CLI output. It uses STDOUT and STDERR.
18  *
19  * This class is a convenient wrapper around `StreamOutput` for both STDOUT and STDERR.
20  *
21  *     $output = new ConsoleOutput();
22  *
23  * This is equivalent to:
24  *
25  *     $output = new StreamOutput(fopen('php://stdout', 'w'));
26  *     $stdErr = new StreamOutput(fopen('php://stderr', 'w'));
27  *
28  * @author Fabien Potencier <fabien@symfony.com>
29  */
30 class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
31 {
32     /**
33      * @var StreamOutput
34      */
35     private $stderr;
36
37     /**
38      * Constructor.
39      *
40      * @param int                           $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
41      * @param bool|null                     $decorated Whether to decorate messages (null for auto-guessing)
42      * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
43      */
44     public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
45     {
46         parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
47
48         $actualDecorated = $this->isDecorated();
49         $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
50
51         if (null === $decorated) {
52             $this->setDecorated($actualDecorated && $this->stderr->isDecorated());
53         }
54     }
55
56     /**
57      * {@inheritdoc}
58      */
59     public function setDecorated($decorated)
60     {
61         parent::setDecorated($decorated);
62         $this->stderr->setDecorated($decorated);
63     }
64
65     /**
66      * {@inheritdoc}
67      */
68     public function setFormatter(OutputFormatterInterface $formatter)
69     {
70         parent::setFormatter($formatter);
71         $this->stderr->setFormatter($formatter);
72     }
73
74     /**
75      * {@inheritdoc}
76      */
77     public function setVerbosity($level)
78     {
79         parent::setVerbosity($level);
80         $this->stderr->setVerbosity($level);
81     }
82
83     /**
84      * {@inheritdoc}
85      */
86     public function getErrorOutput()
87     {
88         return $this->stderr;
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     public function setErrorOutput(OutputInterface $error)
95     {
96         $this->stderr = $error;
97     }
98
99     /**
100      * Returns true if current environment supports writing console output to
101      * STDOUT.
102      *
103      * @return bool
104      */
105     protected function hasStdoutSupport()
106     {
107         return false === $this->isRunningOS400();
108     }
109
110     /**
111      * Returns true if current environment supports writing console output to
112      * STDERR.
113      *
114      * @return bool
115      */
116     protected function hasStderrSupport()
117     {
118         return false === $this->isRunningOS400();
119     }
120
121     /**
122      * Checks if current executing environment is IBM iSeries (OS400), which
123      * doesn't properly convert character-encodings between ASCII to EBCDIC.
124      *
125      * @return bool
126      */
127     private function isRunningOS400()
128     {
129         $checks = array(
130             function_exists('php_uname') ? php_uname('s') : '',
131             getenv('OSTYPE'),
132             PHP_OS,
133         );
134
135         return false !== stripos(implode(';', $checks), 'OS400');
136     }
137
138     /**
139      * @return resource
140      */
141     private function openOutputStream()
142     {
143         if (!$this->hasStdoutSupport()) {
144             return fopen('php://output', 'w');
145         }
146
147         return @fopen('php://stdout', 'w') ?: fopen('php://output', 'w');
148     }
149
150     /**
151      * @return resource
152      */
153     private function openErrorStream()
154     {
155         return fopen($this->hasStderrSupport() ? 'php://stderr' : 'php://output', 'w');
156     }
157 }