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