Skip to content

Commit

Permalink
Merge pull request #38 from mnapoli/fix-36/symfony-style
Browse files Browse the repository at this point in the history
#36 Enable injecting the `SymfonyStyle` object
  • Loading branch information
mnapoli committed May 22, 2017
2 parents 293e7f4 + 1c68c3d commit e53d02f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": ">=5.5",
"symfony/console": "~2.6|~3.0",
"symfony/console": "~2.8|~3.0",
"php-di/invoker": "~1.2",
"container-interop/container-interop": "~1.0"
},
Expand Down
12 changes: 12 additions & 0 deletions docs/command-callables.md
Expand Up @@ -57,6 +57,18 @@ $app->command(
);
```

You can also inject the `SymfonyStyle` object by type-hinting it:

```php
use \Symfony\Component\Console\Style\SymfonyStyle;

...

$app->command('greet', function (SymfonyStyle $io) {
$io->write('hello');
});
```

Finally, you can mix all that (remember the order of parameters doesn't matter):

```php
Expand Down
2 changes: 2 additions & 0 deletions src/Application.php
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* CLI application.
Expand Down Expand Up @@ -79,6 +80,7 @@ public function command($expression, $callable, array $aliases = [])
OutputInterface::class => $output,
Input::class => $input,
Output::class => $output,
SymfonyStyle::class => new SymfonyStyle($input, $output),
],
// Arguments and options are injected by parameter names
$input->getArguments(),
Expand Down
12 changes: 12 additions & 0 deletions tests/FunctionalTest.php
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface as Out;
use Symfony\Component\Console\Style\SymfonyStyle;

class FunctionalTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -114,6 +115,17 @@ public function it_should_inject_the_output_and_input_by_type_hint_even_if_a_ser
$this->assertOutputIs('greet john', 'hello john');
}

/**
* @test
*/
public function it_should_inject_the_symfony_style_object()
{
$this->application->command('greet', function (SymfonyStyle $io) {
$io->write('hello');
});
$this->assertOutputIs('greet', 'hello');
}

/**
* @test
*/
Expand Down

0 comments on commit e53d02f

Please sign in to comment.