8000 [FrameworkBundle] Make use of stderr for non reliable output · symfony/symfony@2848adc · GitHub
[go: up one dir, main page]

Skip to content

Commit 2848adc

Browse files
committed
[FrameworkBundle] Make use of stderr for non reliable output
1 parent a4edafb commit 2848adc

File tree

8 files changed

+66
-14
lines changed

8 files changed

+66
-14
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ protected function configure()
5858
protected function execute(InputInterface $input, OutputInterface $output)
5959
{
6060
$io = new SymfonyStyle($input, $output);
61+
$errorIo = $io->getErrorIo();
6162

6263
if (null === $name = $input->getArgument('name')) {
63-
$this->listBundles($io);
64-
$io->comment('Provide the name of a bundle as the first argument of this command to dump its configuration. (e.g. <comment>debug:config FrameworkBundle</comment>)');
65-
$io->comment('For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>debug:config FrameworkBundle serializer</comment> to dump the <comment>framework.serializer</comment> configuration)');
66-
67-
return;
64+
$this->listBundles($errorIo);
65+
$errorIo->comment('Provide the name of a bundle as the first argument of this command to dump its configuration. (e.g. <comment>debug:config FrameworkBundle</comment>)');
66+
$errorIo->comment('For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>debug:config FrameworkBundle serializer</comment> to dump the <comment>framework.serializer</comment> configuration)');
6867
}
6968

7069
$extension = $this->findExtension($name);
@@ -94,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9493
try {
9594
$config = $this->getConfigForPath($config, $path, $extensionAlias);
9695
} catch (LogicException $e) {
97-
$io->error($e->getMessage());
96+
$errorIo->error($e->getMessage());
9897

9998
return;
10099
}

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7171

7272
if (null === $name = $input->getArgument('name')) {
7373
$this->listBundles($io);
74-
$io->comment('Provide the name of a bundle as the first argument of this command to dump its default configuration. (e.g. <comment>config:dump-reference FrameworkBundle</comment>)');
74+
$io->getErrorIo()->comment('Provide the name of a bundle as the first argument of this command to dump its default configuration. (e.g. <comment>config:dump-reference FrameworkBundle</comment>)');
7575

7676
return;
7777
}

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ protected function configure()
9292
protected function execute(InputInterface $input, OutputInterface $output)
9393
{
9494
$io = new SymfonyStyle($input, $output);
95+
$errorIo = $io->getErrorIo();
96+
9597
$this->validateInput($input);
9698
$object = $this->getContainerBuilder();
9799

@@ -105,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
105107
} elseif ($tag = $input->getOption('tag')) {
106108
$options = array('tag' => $tag, 'show_private' => $input->getOption('show-private'));
107109
} elseif ($name = $input->getArgument('name')) {
108-
$name = $this->findProperServiceName($input, $io, $object, $name);
110+
$name = $this->findProperServiceName($input, $errorIo, $object, $name);
109111
$options = array('id' => $name);
110112
} else {
111113
$options = array('show_private' => $input->getOption('show-private'));
@@ -115,15 +117,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
115117
$options['format'] = $input->getOption('format');
116118
$options['raw_text'] = $input->getOption('raw');
117119
$options['output'] = $io;
118-
$helper->describe($output, $object, $options);
120+
$helper->describe($io, $object, $options);
119121

120122
if (!$input->getArgument('name') && !$input->getOption('tag') && !$input->getOption('parameter') && $input->isInteractive()) {
121123
if ($input->getOption('tags')) {
122-
$io->comment('To search for a specific tag, re-run this command with a search term. (e.g. <comment>debug:container --tag=form.type</comment>)');
124+
$errorIo->comment('To search for a specific tag, re-run this command with a search term. (e.g. <comment>debug:container --tag=form.type</comment>)');
123125
} elseif ($input->getOption('parameters')) {
124-
$io->comment('To search for a specific parameter, re-run this command with a search term. (e.g. <comment>debug:container --parameter=kernel.debug</comment>)');
126+
$errorIo->comment('To search for a specific parameter, re-run this command with a search term. (e.g. <comment>debug:container --parameter=kernel.debug</comment>)');
125127
} else {
126-
$io->comment('To search for a specific service, re-run this command with a search term. (e.g. <comment>debug:container log</comment>)');
128+
$errorIo->comment('To search for a specific service, re-run this command with a search term. (e.g. <comment>debug:container log</comment>)');
127129
}
128130
}
129131
}

src/Symfony/Bundle/FrameworkBundle/Command/EventDispatcherDebugCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6565
$options = array();
6666
if ($event = $input->getArgument('event')) {
6767
if (!$dispatcher->hasListeners($event)) {
68-
$io->warning(sprintf('The event "%s" does not have any registered listeners.', $event));
68+
$io->getErrorIo()->warning(sprintf('The event "%s" does not have any registered listeners.', $event));
6969

7070
return;
7171
}

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
159159
$outputMessage .= sprintf(' and domain "%s"', $domain);
160160
}
161161

162-
$io->warning($outputMessage);
162+
$io->getErrorIo()->warning($outputMessage);
163163

164164
return;
165165
}

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

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

Lines 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