Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add accessor for SymfonyStyle object. #440

Merged
merged 2 commits into from
Sep 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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