diff --git a/src/Common/IO.php b/src/Common/IO.php index fa6fdc2a8..fd93349da 100644 --- a/src/Common/IO.php +++ b/src/Common/IO.php @@ -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)) { diff --git a/tests/src/TestRoboFile.php b/tests/src/TestRoboFile.php index ec5f4e874..acc9ea32c 100644 --- a/tests/src/TestRoboFile.php +++ b/tests/src/TestRoboFile.php @@ -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. */ diff --git a/tests/unit/RunnerTest.php b/tests/unit/RunnerTest.php index 1989911f6..08d154e21 100644 --- a/tests/unit/RunnerTest.php +++ b/tests/unit/RunnerTest.php @@ -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'];