8000 [Console] added a new writeln() method to the Output classes · Dahipster/symfony@1a0bcd1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a0bcd1

Browse files
committed
[Console] added a new writeln() method to the Output classes
1 parent 104ee29 commit 1a0bcd1

File tree

15 files changed

+71
-56
lines changed

15 files changed

+71
-56
lines changed

src/Symfony/Components/Console/Application.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
187187

188188
if (true === $input->hasParameterOption(array('--version', '-V')))
189189
{
190-
$output->write($this->getLongVersion());
190+
$output->writeln($this->getLongVersion());
191191

192192
return 0;
193193
}
@@ -732,22 +732,22 @@ public function renderException($e, $output)
732732

733733
$messages[] = str_repeat(' ', $len);
734734

735-
$output->write("\n");
735+
$output->writeln("\n");
736736
foreach ($messages as $message)
737737
{
738-
$output->write("<error>$message</error>");
738+
$output->writeln("<error>$message</error>");
739739
}
740-
$output->write("\n");
740+
$output->writeln("\n");
741741

742742
if (null !== $this->runningCommand)
743743
{
744-
$output->write(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
745-
$output->write("\n");
744+
$output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
745+
$output->writeln("\n");
746746
}
747747

748748
if (Output::VERBOSITY_VERBOSE === $output->getVerbosity())
749749
{
750-
$output->write('</comment>Exception trace:</comment>');
750+
$output->writeln('</comment>Exception trace:</comment>');
751751

752752
// exception related properties
753753
$trace = $e->getTrace();
@@ -766,10 +766,10 @@ public function renderException($e, $output)
766766
$file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
767767
$line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
768768

769-
$output->write(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line));
769+
$output->writeln(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line));
770770
}
771771

772-
$output->write("\n");
772+
$output->writeln("\n");
773773
}
774774
}
775775

src/Symfony/Components/Console/Command/HelpCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
7272

7373
if ($input->getOption('xml'))
7474
{
75-
$output->write($this->command->asXml(), Output::OUTPUT_RAW);
75+
$output->writeln($this->command->asXml(), Output::OUTPUT_RAW);
7676
}
7777
else
7878
{
79-
$output->write($this->command->asText());
79+
$output->writeln($this->command->asText());
8080
}
8181
}
8282
}

src/Symfony/Components/Console/Command/ListCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
6262
{
6363
if ($input->getOption('xml'))
6464
{
65-
$output->write($this->application->asXml($input->getArgument('namespace')), Output::OUTPUT_RAW);
65+
$output->writeln($this->application->asXml($input->getArgument('namespace')), Output::OUTPUT_RAW);
6666
}
6767
else
6868
{
69-
$output->write($this->application->asText($input->getArgument('namespace')));
69+
$output->writeln($this->application->asText($input->getArgument('namespace')));
7070
}
7171
}
7272
}

src/Symfony/Components/Console/Helper/DialogHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class DialogHelper extends Helper
3434
public function ask(OutputInterface $output, $question, $default = null)
3535
{
3636
// @codeCoverageIgnoreStart
37-
$output->write($question);
37+
$output->writeln($question);
3838

3939
$ret = trim(fgets(STDIN));
4040

@@ -91,7 +91,7 @@ public function askAndValidate(OutputInterface $output, $question, \Closure $val
9191
{
9292
if (null !== $error)
9393
{
94-
$output->write($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
94+
$output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
9595
}
9696

9797
$value = $this->ask($output, $question, null);

src/Symfony/Components/Console/Output/NullOutput.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ class NullOutput extends Output
2626
* Writes a message to the output.
2727
*
2828
* @param string $message A message to write to the output
29+
* @param Boolean $newline Whether to add a newline or not
2930
*/
30-
public function doWrite($message)
31+
public function doWrite($message, $newline)
3132
{
3233
}
3334
}

src/Symfony/Components/Console/Output/Output.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,25 @@ public function getVerbosity()
110110
return $this->verbosity;
111111
}
112112

113+
/**
114+
* Writes a message to the output and adds a newline at the end.
115+
*
116+
* @param string|array $messages The message as an array of lines of a single string
117+
* @param integer $type The type of output
118+
*/
119+
public function writeln($messages, $type = 0)
120+
{
121+
$this->write($messages, true, $type);
122+
}
123+
113124
/**
114125
* Writes a message to the output.
115126
*
116127
* @param string|array $messages The message as an array of lines of a single string
128+
* @param Boolean $newline Whether to add a newline or not
117129
* @param integer $type The type of output
118130
*/
119-
public function write($messages, $type = 0)
131+
public function write($messages, $newline = false, $type = 0)
120132
{
121133
if (self::VERBOSITY_QUIET === $this->verbosity)
122134
{
@@ -144,16 +156,17 @@ public function write($messages, $type = 0)
144156
throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
145157
}
146158

147-
$this->doWrite($message);
159+
$this->doWrite($message, $newline);
148160
}
149161
}
150162

151163
/**
152164
* Writes a message to the output.
153165
*
154-
* @param string $message A message to write to the output
166+
* @param string $message A message to write to the output
167+
* @param Boolean $newline Whether to add a newline or not
155168
*/
156-
abstract public function doWrite($message);
169+
abstract public function doWrite($message, $newline);
157170

158171
/**
159172
* Formats a message according to the given styles.

src/Symfony/Components/Console/Output/StreamOutput.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ public function getStream()
6767
/**
6868
* Writes a message to the output.
6969
*
70-
* @param string $message A message to write to the output
70+
* @param string $message A message to write to the output
71+
* @param Boolean $newline Whether to add a newline or not
7172
*/
72-
public function doWrite($message)
73+
public function doWrite($message, $newline)
7374
{
74-
if (false === @fwrite($this->stream, $message.PHP_EOL))
75+
if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : '')))
7576
{
7677
// @codeCoverageIgnoreStart
7778
// should never happen

src/Symfony/Components/Console/Shell.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ public function run()
6262
readline_read_history($this->history);
6363
readline_completion_function(array($this, 'autocompleter'));
6464

65-
$this->output->write($this->getHeader());
65+
$this->output->writeln($this->getHeader());
6666
while (true)
6767
{
6868
$command = readline($this->application->getName().' > ');
6969

7070
if (false === $command)
7171
{
72-
$this->output->write("\n");
72+
$this->output->writeln("\n");
7373

7474
break;
7575
}
@@ -79,7 +79,7 @@ public function run()
7979

8080
if (0 !== $ret = $this->application->run(new StringInput($command), $this->output))
8181
{
82-
$this->output->write(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
82+
$this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
8383
}
8484
}
8585
}

tests/fixtures/Symfony/Components/Console/FooCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ protected function configure()
2020

2121
protected function interact(InputInterface $input, OutputInterface $output)
2222
{
23-
$output->write('interact called');
23+
$output->writeln('interact called');
2424
}
2525

2626
protected function execute(InputInterface $input, OutputInterface $output)
2727
{
2828
$this->input = $input;
2929
$this->output = $output;
3030

31-
$output->write('called');
31+
$output->writeln('called');
3232
}
3333
}

tests/fixtures/Symfony/Components/Console/TestCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public function getApplication()
2828

2929
protected function execute(InputInterface $input, OutputInterface $output)
3030
{
31-
$output->write('execute called');
31+
$output->writeln('execute called');
3232
}
3333

3434
protected function interact(InputInterface $input, OutputInterface $output)
3535
{
36-
$output->write('interact called');
36+
$output->writeln('interact called');
3737
}
3838
}

0 commit comments

Comments
 (0)
0