[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')))
{
$output->write($this->getLongVersion());
$output->writeln($this->getLongVersion());
return 0;
}
@ -732,22 +732,22 @@ class Application
$messages[] = str_repeat(' ', $len);
$output->write("\n");
$output->writeln("\n");
foreach ($messages as $message)
{
$output->write("<error>$message</error>");
$output->writeln("<error>$message</error>");
}
$output->write("\n");
$output->writeln("\n");
if (null !== $this->runningCommand)
{
$output->write(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
$output->write("\n");
$output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
$output->writeln("\n");
}
if (Output::VERBOSITY_VERBOSE === $output->getVerbosity())
{
$output->write('</comment>Exception trace:</comment>');
$output->writeln('</comment>Exception trace:</comment>');
// exception related properties
$trace = $e->getTrace();
@ -766,10 +766,10 @@ class Application
$file = isset($trace[$i]['file']) ? $trace[$i]['file'] : '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'))
{
$output->write($this->command->asXml(), Output::OUTPUT_RAW);
$output->writeln($this->command->asXml(), Output::OUTPUT_RAW);
}
else
{
$output->write($this->command->asText());
$output->writeln($this->command->asText());
}
}
}

View File

@ -62,11 +62,11 @@ EOF
{
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
{
$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)
{
// @codeCoverageIgnoreStart
$output->write($question);
$output->writeln($question);
$ret = trim(fgets(STDIN));
@ -91,7 +91,7 @@ class DialogHelper extends Helper
{
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);

View File

@ -26,8 +26,9 @@ class NullOutput extends Output
* Writes a message 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 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)
{
@ -144,16 +156,17 @@ abstract class Output implements OutputInterface
throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
}
$this->doWrite($message);
$this->doWrite($message, $newline);
}
}
/**
* 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.

View File

@ -67,11 +67,12 @@ class StreamOutput extends 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
// should never happen

View File

@ -62,14 +62,14 @@ class Shell
readline_read_history($this->history);
readline_completion_function(array($this, 'autocompleter'));
$this->output->write($this->getHeader());
$this->output->writeln($this->getHeader());
while (true)
{
$command = readline($this->application->getName().' > ');
if (false === $command)
{
$this->output->write("\n");
$this->output->writeln("\n");
break;
}
@ -79,7 +79,7 @@ class Shell
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)
{
$output->write('interact called');
$output->writeln('interact called');
}
protected function execute(InputInterface $input, OutputInterface $output)
@ -28,6 +28,6 @@ class FooCommand extends Command
$this->input = $input;
$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)
{
$output->write('execute called');
$output->writeln('execute called');
}
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);
$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');
$tester = new CommandTester($command);

View File

@ -23,9 +23,9 @@ class TestOutput extends Output
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));
$t->is(TestOutput::getStyle('foo'), array('bg' => 'red', 'fg' => 'yellow', 'blink' => true), '::setStyle() sets a new style');
// ->write()
$t->diag('->write()');
// ->writeln()
$t->diag('->writeln()');
$output = new TestOutput(Output::VERBOSITY_QUIET);
$output->write('foo');
$t->is($output->output, '', '->write() outputs nothing if verbosity is set to VERBOSITY_QUIET');
$output->writeln('foo');
$t->is($output->output, '', '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
$output = new TestOutput();
$output->write(array('foo', 'bar'));
$t->is($output->output, "foo\nbar\n", '->write() can take an array of messages to output');
$output->writeln(array('foo', 'bar'));
$t->is($output->output, "foo\nbar\n", '->writeln() can take an array of messages to output');
$output = new TestOutput();
$output->write('<info>foo</info>', Output::OUTPUT_RAW);
$t->is($output->output, "<info>foo</info>\n", '->write() outputs the raw message if OUTPUT_RAW is specified');
$output->writeln('<info>foo</info>', Output::OUTPUT_RAW);
$t->is($output->output, "<info>foo</info>\n", '->writeln() outputs the raw message if OUTPUT_RAW is specified');
$output = new TestOutput();
$output->write('<info>foo</info>', Output::OUTPUT_PLAIN);
$t->is($output->output, "foo\n", '->write() strips decoration tags if OUTPUT_PLAIN is specified');
$output->writeln('<info>foo</info>', Output::OUTPUT_PLAIN);
$t->is($output->output, "foo\n", '->writeln() strips decoration tags if OUTPUT_PLAIN is specified');
$output = new TestOutput();
$output->setDecorated(false);
$output->write('<info>foo</info>');
$t->is($output->output, "foo\n", '->write() strips decoration tags if decoration is set to false');
$output->writeln('<info>foo</info>');
$t->is($output->output, "foo\n", '->writeln() strips decoration tags if decoration is set to false');
$output = new TestOutput();
$output->setDecorated(true);
$output->write('<foo>foo</foo>');
$t->is($output->output, "\033[33;41;5mfoo\033[0m\n", '->write() decorates the output');
$output->writeln('<foo>foo</foo>');
$t->is($output->output, "\033[33;41;5mfoo\033[0m\n", '->writeln() decorates the output');
try
{
$output->write('<foo>foo</foo>', 24);
$t->fail('->write() throws an \InvalidArgumentException when the type does not exist');
$output->writeln('<foo>foo</foo>', 24);
$t->fail('->writeln() throws an \InvalidArgumentException when the type does not exist');
}
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
{
$output->write('<bar>foo</bar>');
$t->fail('->write() throws an \InvalidArgumentException when a style does not exist');
$output->writeln('<bar>foo</bar>');
$t->fail('->writeln() throws an \InvalidArgumentException when a style does not exist');
}
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()
$t->diag('->doWrite()');
$output = new StreamOutput($stream);
$output->write('foo');
$output->writeln('foo');
rewind($output->getStream());
$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')
->addArgument('command')
->addArgument('foo')
->setCode(function ($input, $output) { $output->write('foo'); })
->setCode(function ($input, $output) { $output->writeln('foo'); })
;
$tester = new ApplicationTester($application);

View File

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