[Console] added a new writeln() method to the Output classes

This commit is contained in:
Fabien Potencier 2010-01-14 15:40:25 +01:00
parent 104ee29f4e
commit 1a0bcd141a
15 changed files with 72 additions and 57 deletions

View File

@ -187,7 +187,7 @@ class Application
if (true === $input->hasParameterOption(array('--version', '-V'))) if (true === $input->hasParameterOption(array('--version', '-V')))
{ {
$output->write($this->getLongVersion()); $output->writeln($this->getLongVersion());
return 0; return 0;
} }
@ -732,22 +732,22 @@ class Application
$messages[] = str_repeat(' ', $len); $messages[] = str_repeat(' ', $len);
$output->write("\n"); $output->writeln("\n");
foreach ($messages as $message) foreach ($messages as $message)
{ {
$output->write("<error>$message</error>"); $output->writeln("<error>$message</error>");
} }
$output->write("\n"); $output->writeln("\n");
if (null !== $this->runningCommand) if (null !== $this->runningCommand)
{ {
$output->write(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName()))); $output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
$output->write("\n"); $output->writeln("\n");
} }
if (Output::VERBOSITY_VERBOSE === $output->getVerbosity()) if (Output::VERBOSITY_VERBOSE === $output->getVerbosity())
{ {
$output->write('</comment>Exception trace:</comment>'); $output->writeln('</comment>Exception trace:</comment>');
// exception related properties // exception related properties
$trace = $e->getTrace(); $trace = $e->getTrace();
@ -766,10 +766,10 @@ class Application
$file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a'; $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
$line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a'; $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
$output->write(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line)); $output->writeln(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line));
} }
$output->write("\n"); $output->writeln("\n");
} }
} }

View File

@ -72,11 +72,11 @@ EOF
if ($input->getOption('xml')) if ($input->getOption('xml'))
{ {
$output->write($this->command->asXml(), Output::OUTPUT_RAW); $output->writeln($this->command->asXml(), Output::OUTPUT_RAW);
} }
else else
{ {
$output->write($this->command->asText()); $output->writeln($this->command->asText());
} }
} }
} }

View File

@ -62,11 +62,11 @@ EOF
{ {
if ($input->getOption('xml')) if ($input->getOption('xml'))
{ {
$output->write($this->application->asXml($input->getArgument('namespace')), Output::OUTPUT_RAW); $output->writeln($this->application->asXml($input->getArgument('namespace')), Output::OUTPUT_RAW);
} }
else else
{ {
$output->write($this->application->asText($input->getArgument('namespace'))); $output->writeln($this->application->asText($input->getArgument('namespace')));
} }
} }
} }

View File

@ -34,7 +34,7 @@ class DialogHelper extends Helper
public function ask(OutputInterface $output, $question, $default = null) public function ask(OutputInterface $output, $question, $default = null)
{ {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
$output->write($question); $output->writeln($question);
$ret = trim(fgets(STDIN)); $ret = trim(fgets(STDIN));
@ -91,7 +91,7 @@ class DialogHelper extends Helper
{ {
if (null !== $error) if (null !== $error)
{ {
$output->write($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error')); $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
} }
$value = $this->ask($output, $question, null); $value = $this->ask($output, $question, null);

View File

@ -26,8 +26,9 @@ class NullOutput extends Output
* Writes a message to the output. * Writes a message to the output.
* *
* @param string $message A message to write to the output * @param string $message A message to write to the output
* @param Boolean $newline Whether to add a newline or not
*/ */
public function doWrite($message) public function doWrite($message, $newline)
{ {
} }
} }

View File

@ -111,12 +111,24 @@ abstract class Output implements OutputInterface
} }
/** /**
* Writes a message to the output. * Writes a message to the output and adds a newline at the end.
* *
* @param string|array $messages The message as an array of lines of a single string * @param string|array $messages The message as an array of lines of a single string
* @param integer $type The type of output * @param integer $type The type of output
*/ */
public function write($messages, $type = 0) public function writeln($messages, $type = 0)
{
$this->write($messages, true, $type);
}
/**
* Writes a message to the output.
*
* @param string|array $messages The message as an array of lines of a single string
* @param Boolean $newline Whether to add a newline or not
* @param integer $type The type of output
*/
public function write($messages, $newline = false, $type = 0)
{ {
if (self::VERBOSITY_QUIET === $this->verbosity) if (self::VERBOSITY_QUIET === $this->verbosity)
{ {
@ -144,16 +156,17 @@ abstract class Output implements OutputInterface
throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type)); throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
} }
$this->doWrite($message); $this->doWrite($message, $newline);
} }
} }
/** /**
* Writes a message to the output. * Writes a message to the output.
* *
* @param string $message A message to write to the output * @param string $message A message to write to the output
* @param Boolean $newline Whether to add a newline or not
*/ */
abstract public function doWrite($message); abstract public function doWrite($message, $newline);
/** /**
* Formats a message according to the given styles. * Formats a message according to the given styles.

View File

@ -67,11 +67,12 @@ class StreamOutput extends Output
/** /**
* Writes a message to the output. * Writes a message to the output.
* *
* @param string $message A message to write to the output * @param string $message A message to write to the output
* @param Boolean $newline Whether to add a newline or not
*/ */
public function doWrite($message) public function doWrite($message, $newline)
{ {
if (false === @fwrite($this->stream, $message.PHP_EOL)) if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : '')))
{ {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
// should never happen // should never happen

View File

@ -62,14 +62,14 @@ class Shell
readline_read_history($this->history); readline_read_history($this->history);
readline_completion_function(array($this, 'autocompleter')); readline_completion_function(array($this, 'autocompleter'));
$this->output->write($this->getHeader()); $this->output->writeln($this->getHeader());
while (true) while (true)
{ {
$command = readline($this->application->getName().' > '); $command = readline($this->application->getName().' > ');
if (false === $command) if (false === $command)
{ {
$this->output->write("\n"); $this->output->writeln("\n");
break; break;
} }
@ -79,7 +79,7 @@ class Shell
if (0 !== $ret = $this->application->run(new StringInput($command), $this->output)) if (0 !== $ret = $this->application->run(new StringInput($command), $this->output))
{ {
$this->output->write(sprintf('<error>The command terminated with an error status (%s)</error>', $ret)); $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
} }
} }
} }

View File

@ -20,7 +20,7 @@ class FooCommand extends Command
protected function interact(InputInterface $input, OutputInterface $output) protected function interact(InputInterface $input, OutputInterface $output)
{ {
$output->write('interact called'); $output->writeln('interact called');
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
@ -28,6 +28,6 @@ class FooCommand extends Command
$this->input = $input; $this->input = $input;
$this->output = $output; $this->output = $output;
$output->write('called'); $output->writeln('called');
} }
} }

View File

@ -28,11 +28,11 @@ class TestCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$output->write('execute called'); $output->writeln('execute called');
} }
protected function interact(InputInterface $input, OutputInterface $output) protected function interact(InputInterface $input, OutputInterface $output)
{ {
$output->write('interact called'); $output->writeln('interact called');
} }
} }

View File

@ -190,7 +190,7 @@ $command = new TestCommand();
$command->setApplication($application); $command->setApplication($application);
$ret = $command->setCode(function (InputInterface $input, OutputInterface $output) $ret = $command->setCode(function (InputInterface $input, OutputInterface $output)
{ {
$output->write('from the code...'); $output->writeln('from the code...');
}); });
$t->is($ret, $command, '->setCode() implements a fluent interface'); $t->is($ret, $command, '->setCode() implements a fluent interface');
$tester = new CommandTester($command); $tester = new CommandTester($command);

View File

@ -23,9 +23,9 @@ class TestOutput extends Output
return static::$styles[$name]; return static::$styles[$name];
} }
public function doWrite($message) public function doWrite($message, $newline)
{ {
$this->output .= $message."\n"; $this->output .= $message.($newline ? "\n" : '');
} }
} }
@ -51,50 +51,50 @@ $t->diag('::setStyle()');
Output::setStyle('FOO', array('bg' => 'red', 'fg' => 'yellow', 'blink' => true)); Output::setStyle('FOO', array('bg' => 'red', 'fg' => 'yellow', 'blink' => true));
$t->is(TestOutput::getStyle('foo'), array('bg' => 'red', 'fg' => 'yellow', 'blink' => true), '::setStyle() sets a new style'); $t->is(TestOutput::getStyle('foo'), array('bg' => 'red', 'fg' => 'yellow', 'blink' => true), '::setStyle() sets a new style');
// ->write() // ->writeln()
$t->diag('->write()'); $t->diag('->writeln()');
$output = new TestOutput(Output::VERBOSITY_QUIET); $output = new TestOutput(Output::VERBOSITY_QUIET);
$output->write('foo'); $output->writeln('foo');
$t->is($output->output, '', '->write() outputs nothing if verbosity is set to VERBOSITY_QUIET'); $t->is($output->output, '', '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
$output = new TestOutput(); $output = new TestOutput();
$output->write(array('foo', 'bar')); $output->writeln(array('foo', 'bar'));
$t->is($output->output, "foo\nbar\n", '->write() can take an array of messages to output'); $t->is($output->output, "foo\nbar\n", '->writeln() can take an array of messages to output');
$output = new TestOutput(); $output = new TestOutput();
$output->write('<info>foo</info>', Output::OUTPUT_RAW); $output->writeln('<info>foo</info>', Output::OUTPUT_RAW);
$t->is($output->output, "<info>foo</info>\n", '->write() outputs the raw message if OUTPUT_RAW is specified'); $t->is($output->output, "<info>foo</info>\n", '->writeln() outputs the raw message if OUTPUT_RAW is specified');
$output = new TestOutput(); $output = new TestOutput();
$output->write('<info>foo</info>', Output::OUTPUT_PLAIN); $output->writeln('<info>foo</info>', Output::OUTPUT_PLAIN);
$t->is($output->output, "foo\n", '->write() strips decoration tags if OUTPUT_PLAIN is specified'); $t->is($output->output, "foo\n", '->writeln() strips decoration tags if OUTPUT_PLAIN is specified');
$output = new TestOutput(); $output = new TestOutput();
$output->setDecorated(false); $output->setDecorated(false);
$output->write('<info>foo</info>'); $output->writeln('<info>foo</info>');
$t->is($output->output, "foo\n", '->write() strips decoration tags if decoration is set to false'); $t->is($output->output, "foo\n", '->writeln() strips decoration tags if decoration is set to false');
$output = new TestOutput(); $output = new TestOutput();
$output->setDecorated(true); $output->setDecorated(true);
$output->write('<foo>foo</foo>'); $output->writeln('<foo>foo</foo>');
$t->is($output->output, "\033[33;41;5mfoo\033[0m\n", '->write() decorates the output'); $t->is($output->output, "\033[33;41;5mfoo\033[0m\n", '->writeln() decorates the output');
try try
{ {
$output->write('<foo>foo</foo>', 24); $output->writeln('<foo>foo</foo>', 24);
$t->fail('->write() throws an \InvalidArgumentException when the type does not exist'); $t->fail('->writeln() throws an \InvalidArgumentException when the type does not exist');
} }
catch (\InvalidArgumentException $e) catch (\InvalidArgumentException $e)
{ {
$t->pass('->write() throws an \InvalidArgumentException when the type does not exist'); $t->pass('->writeln() throws an \InvalidArgumentException when the type does not exist');
} }
try try
{ {
$output->write('<bar>foo</bar>'); $output->writeln('<bar>foo</bar>');
$t->fail('->write() throws an \InvalidArgumentException when a style does not exist'); $t->fail('->writeln() throws an \InvalidArgumentException when a style does not exist');
} }
catch (\InvalidArgumentException $e) catch (\InvalidArgumentException $e)
{ {
$t->pass('->write() throws an \InvalidArgumentException when a style does not exist'); $t->pass('->writeln() throws an \InvalidArgumentException when a style does not exist');
} }

View File

@ -42,6 +42,6 @@ $t->is($output->getStream(), $stream, '->getStream() returns the current stream'
// ->doWrite() // ->doWrite()
$t->diag('->doWrite()'); $t->diag('->doWrite()');
$output = new StreamOutput($stream); $output = new StreamOutput($stream);
$output->write('foo'); $output->writeln('foo');
rewind($output->getStream()); rewind($output->getStream());
$t->is(stream_get_contents($output->getStream()), "foo\n", '->doWrite() writes to the stream'); $t->is(stream_get_contents($output->getStream()), "foo\n", '->doWrite() writes to the stream');

View File

@ -21,7 +21,7 @@ $application->setAutoExit(false);
$application->register('foo') $application->register('foo')
->addArgument('command') ->addArgument('command')
->addArgument('foo') ->addArgument('foo')
->setCode(function ($input, $output) { $output->write('foo'); }) ->setCode(function ($input, $output) { $output->writeln('foo'); })
; ;
$tester = new ApplicationTester($application); $tester = new ApplicationTester($application);

View File

@ -19,7 +19,7 @@ $t = new LimeTest(6);
$command = new Command('foo'); $command = new Command('foo');
$command->addArgument('command'); $command->addArgument('command');
$command->addArgument('foo'); $command->addArgument('foo');
$command->setCode(function ($input, $output) { $output->write('foo'); }); $command->setCode(function ($input, $output) { $output->writeln('foo'); });
$tester = new CommandTester($command); $tester = new CommandTester($command);
$tester->execute(array('foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE)); $tester->execute(array('foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));