Skip to content
Newer
Older
100644 114 lines (100 sloc) 3.27 KB
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
1 <?php
2
3 /*
4a83d99 @blue-eyes normalized license messages in PHP files
blue-eyes authored Jan 15, 2011
4 * This file is part of the Symfony package.
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
5 *
b933805 @fabpot replaced symfony-project.org by symfony.com
fabpot authored Mar 6, 2011
6 * (c) Fabien Potencier <fabien@symfony.com>
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
7 *
4a83d99 @blue-eyes normalized license messages in PHP files
blue-eyes authored Jan 15, 2011
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
10 */
11
4a83d99 @blue-eyes normalized license messages in PHP files
blue-eyes authored Jan 15, 2011
12 namespace Symfony\Component\Console\Output;
13
1c1a940 @canni [BugFix][Console] Fix type hint for output formatter
canni authored Dec 17, 2011
14 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
c9a6856 @everzet [Console] updated console and stream outputs to support new output fo…
everzet authored Mar 17, 2011
15
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
16 /**
17 * StreamOutput writes the output to a given stream.
18 *
19 * Usage:
20 *
21 * $output = new StreamOutput(fopen('php://stdout', 'w'));
22 *
23 * As `StreamOutput` can use any stream, you can also use a file:
24 *
25 * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
26 *
b933805 @fabpot replaced symfony-project.org by symfony.com
fabpot authored Mar 6, 2011
27 * @author Fabien Potencier <fabien@symfony.com>
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored Mar 24, 2011
28 *
29 * @api
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
30 */
31 class StreamOutput extends Output
32 {
67dbf5e @fabpot moved most protected to private in the Console component
fabpot authored Mar 11, 2011
33 private $stream;
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
34
35 /**
36 * Constructor.
37 *
0e67dc7 @fabpot fixed phpdoc @param alignment
fabpot authored May 15, 2012
38 * @param mixed $stream A stream resource
39 * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL,
4ec78d1 @pborreli [Phpdoc] Cleaning/fixing
pborreli authored Apr 23, 2011
40 * self::VERBOSITY_VERBOSE)
41 * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
42 * @param OutputFormatter $formatter Output formatter instance
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
43 *
44 * @throws \InvalidArgumentException When first argument is not a real stream
00b2567 @fabpot [Console] tagged the guaranteed BC API
fabpot authored Mar 24, 2011
45 *
46 * @api
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
47 */
1c1a940 @canni [BugFix][Console] Fix type hint for output formatter
canni authored Dec 17, 2011
48 public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
49 {
50 if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
51 throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
52 }
53
54 $this->stream = $stream;
55
56 if (null === $decorated) {
457f9f6 @h4ck3rm1k3 hasColorSupport does not take an argument
h4ck3rm1k3 authored Sep 27, 2012
57 $decorated = $this->hasColorSupport();
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
58 }
59
c9a6856 @everzet [Console] updated console and stream outputs to support new output fo…
everzet authored Mar 17, 2011
60 parent::__construct($verbosity, $decorated, $formatter);
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
61 }
62
63 /**
64 * Gets the stream attached to this StreamOutput instance.
65 *
66 * @return resource A stream resource
67 */
68 public function getStream()
69 {
70 return $this->stream;
71 }
72
73 /**
74 * Writes a message to the output.
75 *
76 * @param string $message A message to write to the output
77 * @param Boolean $newline Whether to add a newline or not
78 *
79 * @throws \RuntimeException When unable to write output (should never happen)
80 */
afc5ecf @kriswallsmith [Console] reduced visibility to protected
kriswallsmith authored Jun 8, 2012
81 protected function doWrite($message, $newline)
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
82 {
83 if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) {
84 // @codeCoverageIgnoreStart
85 // should never happen
86 throw new \RuntimeException('Unable to write output.');
87 // @codeCoverageIgnoreEnd
88 }
89
23dc80c @fabpot [Console] fixed typo
fabpot authored Jan 24, 2011
90 fflush($this->stream);
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
91 }
92
93 /**
94 * Returns true if the stream supports colorization.
95 *
96 * Colorization is disabled if not supported by the stream:
97 *
ef1ba67 @hason [Console] Console colorization is also provided by ConEmu on Windows
hason authored Sep 12, 2012
98 * - windows without ansicon and ConEmu
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
99 * - non tty consoles
100 *
101 * @return Boolean true if the stream supports colorization, false otherwise
102 */
103 protected function hasColorSupport()
104 {
105 // @codeCoverageIgnoreStart
106 if (DIRECTORY_SEPARATOR == '\\') {
ef1ba67 @hason [Console] Console colorization is also provided by ConEmu on Windows
hason authored Sep 12, 2012
107 return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
108 }
2622e1a @pborreli [Console] Removed useless else
pborreli authored Feb 27, 2011
109
110 return function_exists('posix_isatty') && @posix_isatty($this->stream);
68c191c @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
111 // @codeCoverageIgnoreEnd
112 }
113 }
Something went wrong with that request. Please try again.