8000 [Console] Ease writing to stderr using styles · symfony/symfony@293e627 · GitHub
[go: up one dir, main page]

Skip to content

Commit 293e627

Browse files
committed
[Console] Ease writing to stderr using styles
1 parent a4edafb commit 293e627

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

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

Lines 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

Lines 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
/** 8000
341356
* @return ProgressBar
342357
*/

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
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;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
21+
use Symfony\Component\Console\Input\InputInterface;
1822

1923
class SymfonyStyleTest extends PHPUnit_Framework_TestCase
2024
{
@@ -71,4 +75,29 @@ public function inputCommandToOutputFilesProvider()
7175

7276
return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
7377
}
78+
79+
public function testGetErrorIo()
80+
{
81+
$input = $this->getMock(InputInterface::class);
82+
83+
$errorOutput = $this->getMock(OutputInterface::class);
84+
$errorOutput
85+
->method('getFormatter')
86+
->willReturn(new OutputFormatter());
87+
$errorOutput
88+
->expects($this->once())
89+
->method('write');
90+
91+
$output = $this->getMock(ConsoleOutputInterface::class);
92+
$output
93+
->method('getFormatter')
94+
->willReturn(new OutputFormatter());
95+
$output
96+
->expects($this->once())
97+
->method('getErrorOutput')
98+
->willReturn($errorOutput);
99+
100+
$io = new SymfonyStyle($input, $output);
101+
$io->getErrorIo()->write('');
102+
}
74103
}

0 commit comments

Comments
 (0)
0