Skip to content

Commit a928fa5

Browse files
committed
Merge pull request #1093 from richardmiller/console_framework_separation
Split Framework specific console docs into separate cookbook article
2 parents ace4679 + cb425ae commit a928fa5

File tree

2 files changed

+117
-17
lines changed

2 files changed

+117
-17
lines changed

components/console.rst

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ The Console Component
77
The Console component eases the creation of beautiful and testable command
88
line interfaces.
99

10-
Symfony2 ships with a Console component, which allows you to create
11-
command-line commands. Your console commands can be used for any recurring
12-
task, such as cronjobs, imports, or other batch jobs.
10+
The Console component allows you to create command-line commands. Your console
11+
commands can be used for any recurring task, such as cronjobs, imports, or
12+
other batch jobs.
1313

1414
Installation
1515
------------
@@ -23,23 +23,16 @@ You can install the component in many different ways:
2323
Creating a basic Command
2424
------------------------
2525

26-
To make the console commands available automatically with Symfony2, create a
27-
``Command`` directory inside your bundle and create a php file suffixed with
28-
``Command.php`` for each command that you want to provide. For example, if you
29-
want to extend the ``AcmeDemoBundle`` (available in the Symfony Standard
30-
Edition) to greet us from the command line, create ``GreetCommand.php`` and
31-
add the following to it::
26+
To make a console command to greet us from the command line, create ``GreetCommand.php``
27+
and add the following to it::
3228

33-
// src/Acme/DemoBundle/Command/GreetCommand.php
34-
namespace Acme\DemoBundle\Command;
35-
36-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
29+
use Symfony\Component\Console\Command\Command;
3730
use Symfony\Component\Console\Input\InputArgument;
3831
use Symfony\Component\Console\Input\InputInterface;
3932
use Symfony\Component\Console\Input\InputOption;
4033
use Symfony\Component\Console\Output\OutputInterface;
4134

42-
class GreetCommand extends ContainerAwareCommand
35+
class GreetCommand extends Command
4336
{
4437
protected function configure()
4538
{
@@ -255,9 +248,8 @@ useful one is the :class:`Symfony\\Component\\Console\\Tester\\CommandTester`
255248
class. It uses special input and output classes to ease testing without a real
256249
console::
257250

251+
use Symfony\Component\Console\Application;
258252
use Symfony\Component\Console\Tester\CommandTester;
259-
use Symfony\Bundle\FrameworkBundle\Console\Application;
260-
use Acme\DemoBundle\Command\GreetCommand;
261253

262254
class ListCommandTest extends \PHPUnit_Framework_TestCase
263255
{
@@ -285,7 +277,7 @@ as an array to the :method:`Symfony\\Component\\Console\\Tester\\CommandTester::
285277
method::
286278

287279
use Symfony\Component\Console\Tester\CommandTester;
288-
use Symfony\Bundle\FrameworkBundle\Console\Application;
280+
use Symfony\Component\Console\Application;
289281
use Acme\DemoBundle\Command\GreetCommand;
290282

291283
class ListCommandTest extends \PHPUnit_Framework_TestCase

cookbook/console/console_command.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
How to create a Console Command
2+
===============================
3+
4+
The Console page of the Components section (:doc:`/components/console`) covers
5+
how to create a Console command. This cookbook articles covers the differences
6+
when creating Console commands within the Symfony2 framework.
7+
8+
Automatically Registering Commands
9+
----------------------------------
10+
11+
To make the console commands available automatically with Symfony2, create a
12+
``Command`` directory inside your bundle and create a php file suffixed with
13+
``Command.php`` for each command that you want to provide. For example, if you
14+
want to extend the ``AcmeDemoBundle`` (available in the Symfony Standard
15+
Edition) to greet us from the command line, create ``GreetCommand.php`` and
16+
add the following to it::
17+
18+
// src/Acme/DemoBundle/Command/GreetCommand.php
19+
namespace Acme\DemoBundle\Command;
20+
21+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Input\InputOption;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
27+
class GreetCommand extends ContainerAwareCommand
28+
{
29+
protected function configure()
30+
{
31+
$this
32+
->setName('demo:greet')
33+
->setDescription('Greet someone')
34+
->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?')
35+
->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
36+
;
37+
}
38+
39+
protected function execute(InputInterface $input, OutputInterface $output)
40+
{
41+
$name = $input->getArgument('name');
42+
if ($name) {
43+
$text = 'Hello '.$name;
44+
} else {
45+
$text = 'Hello';
46+
}
47+
48+
if ($input->getOption('yell')) {
49+
$text = strtoupper($text);
50+
}
51+
52+
$output->writeln($text);
53+
}
54+
}
55+
56+
This command will now automatically be available to run:
57+
58+
.. code-block:: bash
59+
60+
app/console demo:greet Fabien
61+
62+
Testing Commands
63+
----------------
64+
65+
When testing commands used as part of the full framework :class:`Symfony\\Bundle\\FrameworkBundle\\Console\\Application`
66+
should be used instead of :class:`Symfony\\Component\\Console\\Application`::
67+
68+
use Symfony\Component\Console\Tester\CommandTester;
69+
use Symfony\Bundle\FrameworkBundle\Console\Application;
70+
use Acme\DemoBundle\Command\GreetCommand;
71+
72+
class ListCommandTest extends \PHPUnit_Framework_TestCase
73+
{
74+
public function testExecute()
75+
{
76+
// mock the Kernel or create one depending on your needs
77+
$application = new Application($kernel);
78+
$application->add(new GreetCommand());
79+
80+
$command = $application->find('demo:greet');
81+
$commandTester = new CommandTester($command);
82+
$commandTester->execute(array('command' => $command->getName()));
83+
84+
$this->assertRegExp('/.../', $commandTester->getDisplay());
85+
86+
// ...
87+
}
88+
}
89+
90+
Getting Services from the Service Container
91+
-------------------------------------------
92+
93+
By using :class:`Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand`
94+
as the base class for the command (instead of the more basic
95+
:class:`Symfony\\Component\\Console\\Command\\Command`), you have access to the
96+
service container. In other words, you have access to any configured service.
97+
For example, you could easily extend the task to be translatable::
98+
99+
protected function execute(InputInterface $input, OutputInterface $output)
100+
{
101+
$name = $input->getArgument('name');
102+
$translator = $this->getContainer()->get('translator');
103+
if ($name) {
104+
$output->writeln($translator->trans('Hello %name%!', array('%name%' => $name)));
105+
} else {
106+
$output->writeln($translator->trans('Hello!'));
107+
}
108+
}

0 commit comments

Comments
 (0)