Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b2d7a1a

Browse filesBrowse files
committed
[Console] Ease writing to stderr using styles
1 parent a4edafb commit b2d7a1a
Copy full SHA for b2d7a1a

File tree

3 files changed

+51
-0
lines changed
Filter options

3 files changed

+51
-0
lines changed

‎src/Symfony/Component/Console/Style/OutputStyle.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Style/OutputStyle.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
1515
use Symfony\Component\Console\Helper\ProgressBar;
1616
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1718

1819
/**
1920
* Decorates output to add console style guide helpers.
@@ -145,4 +146,13 @@ public function isDebug()
145146
{
146147
return $this->output->isDebug();
147148
}
149+
150+
protected function getErrorOutput()
151+
{
152+
if (!$this->output instanceof ConsoleOutputInterface) {
153+
return;
154+
}
155+
156+
return $this->output->getErrorOutput();
157+
}
148158
}

‎src/Symfony/Component/Console/Style/SymfonyStyle.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Style/SymfonyStyle.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,21 @@ public function newLine($count = 1)
337337
$this->bufferedOutput->write(str_repeat("\n", $count));
338338
}
339339

340+
/**
341+
* Returns a new instance which makes use of stderr if available, the current instance
342+
* otherwise.
343+
*
344+
* @return self
345+
*/
346+
public function getErrorIo()
347+
{
348+
if (!$stderr = $this->getErrorOutput()) {
349+
return $this;
350+
}
351+
352+
return new self($this->input, $stderr);
353+
}
354+
340355
/**
341356
* @return ProgressBar
342357
*/

‎src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\Style\SymfonyStyle;
1717
use Symfony\Component\Console\Tester\CommandTester;
18+
use Symfony\Component\Console\Formatter\OutputFormatter;
1819

1920
class SymfonyStyleTest extends PHPUnit_Framework_TestCase
2021
{
@@ -71,4 +72,29 @@ public function inputCommandToOutputFilesProvider()
7172

7273
return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
7374
}
75+
76+
public function testGetErrorIo()
77+
{
78+
$input = $this->getMock('Symfony\Component\Console\Input\InputInterface');
79+
80+
$errorOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
81+
$errorOutput
82+
->method('getFormatter')
83+
->willReturn(new OutputFormatter());
84+
$errorOutput
85+
->expects($this->once())
86+
->method('write');
87+
88+
$output = $this->getMock('Symfony\Component\Console\Output\ConsoleOutputInterface');
89+
$output
90+
->method('getFormatter')
91+
->willReturn(new OutputFormatter());
92+
$output
93+
->expects($this->once())
94+
->method('getErrorOutput')
95+
->willReturn($errorOutput);
96+
97+
$io = new SymfonyStyle($input, $output);
98+
$io->getErrorIo()->write('');
99+
}
74100
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.