Skip to content

Commit

Permalink
Merge pull request #440 from consolidation-org/symfony-style
Browse files Browse the repository at this point in the history
Add accessor for SymfonyStyle object.
  • Loading branch information
greg-1-anderson authored Sep 8, 2016
2 parents 2779e16 + 3f5a4e2 commit 6a07453
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ $name = $this->ask("What is your name?");

There are also `askDefault`, `askHidden`, and `confirm` methods.

In addition, Robo makes all of the methods of Symfony Style available throgh the `io()` method:

$this->io()->title("Build all site assets");

This allows Robo scripts to follow the [Symfony Console Style Guide](http://symfony.com/blog/new-in-symfony-2-8-console-style-guide) if desired.

### Formatters

It is preferable for commands that look up and display information should avoid doing IO directly, and should instead return the data they wish to display as an array. This data can then be converted into different data formats, such as "table" and "json". The user may select which formatter to use via the --format option. For details on formatters, see the [consolidation/output-formatters](https://github.com/consolidation-org/output-formatters) project.
Expand Down
17 changes: 17 additions & 0 deletions src/Common/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;

trait IO
{
use InputAwareTrait;
use OutputAwareTrait;

/** var: SymfonyStyle */
protected $io;

/**
* Provide access to SymfonyStyle object.
* See: http://symfony.com/blog/new-in-symfony-2-8-console-style-guide
* @return SymfonyStyle
*/
protected function io()
{
if (!$this->io) {
$this->io = new SymfonyStyle($this->input(), $this->output());
}
return $this->io;
}

protected function decorationCharacter($nonDecorated, $decorated)
{
if (!$this->output()->isDecorated() || (strncasecmp(PHP_OS, 'WIN', 3) == 0)) {
Expand Down
13 changes: 13 additions & 0 deletions tests/src/TestRoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ public function testArrayArgs(array $a)
$this->say("The parameters passed are:\n" . var_export($a, true));
}

/**
* Demonstrate use of SymfonyStyle
*/
public function testSymfonyStyle()
{
$this->io()->title('My Title');
$this->io()->section('Section 1');
$this->io()->text('Some text in section one.');
$this->io()->comment('This is just an example of different styles.');
$this->io()->section('Section 2');
$this->io()->text('Some text in section two.');
}

/**
* Demonstrate Robo error output and command failure.
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public function testRunnerTryArgs()
$this->guy->seeOutputEquals($expected);
}

public function testSymfonyStyle()
{
$argv = ['placeholder', 'test:symfony-style'];
$this->runner->execute($argv, Robo::output());
$this->guy->seeInOutput('Some text in section one.');
}

public function testRunnerTryError()
{
$argv = ['placeholder', 'test:error'];
Expand Down

0 comments on commit 6a07453

Please sign in to comment.