|
| 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