|
| 1 | +.. index:: |
| 2 | + single: Console; Commands as Services |
| 3 | + |
| 4 | +How to define Commands as Services |
| 5 | +================================== |
| 6 | + |
| 7 | +.. versionadded:: 2.4 |
| 8 | + Support for registering commands in the service container was added in |
| 9 | + version 2.4. |
| 10 | + |
| 11 | +By default, Symfony will take a look in the ``Command`` directory of your |
| 12 | +bundles and automatically register your commands. For the ones implementing |
| 13 | +the ``ContainerAwareCommand`` interface, Symfony will even inject the container. |
| 14 | + |
| 15 | +While making life easier, this default implementation has some drawbacks in some |
| 16 | +situations: |
| 17 | + |
| 18 | +* what if you want your command to be defined elsewhere than in the ``Command`` |
| 19 | + folder? |
| 20 | +* what if you want to register conditionally your command, depending on the |
| 21 | + current environment or on the availability of some dependencies? |
| 22 | +* what if you need to access dependencies before the ``setContainer`` is called |
| 23 | + (for example in the ``configure`` method)? |
| 24 | +* what if you want to reuse a command many times, but with different |
| 25 | + dependencies or parameters? |
| 26 | + |
| 27 | +To solve those problems, you can register your command as a service by simply |
| 28 | +defining it with the ``console.command`` tag: |
| 29 | + |
| 30 | +.. configuration-block:: |
| 31 | + |
| 32 | + .. code-block:: yaml |
| 33 | +
|
| 34 | + # app/config/config.yml |
| 35 | + services: |
| 36 | + acme_hello.command.my_command: |
| 37 | + class: Acme\HelloBundle\Command\MyCommand |
| 38 | + tags: |
| 39 | + - { name: console.command } |
| 40 | +
|
| 41 | + .. code-block:: xml |
| 42 | +
|
| 43 | + <!-- app/config/config.xml --> |
| 44 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 45 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 46 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 47 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 48 | +
|
| 49 | + <service id="acme_hello.command.my_command" |
| 50 | + class="Acme\HelloBundle\Command\MyCommand"> |
| 51 | + <tag name="console.command" /> |
| 52 | + </service> |
| 53 | + </container> |
| 54 | +
|
| 55 | + .. code-block:: php |
| 56 | +
|
| 57 | + // app/config/config.php |
| 58 | +
|
| 59 | + $container |
| 60 | + ->register('acme_hello.command.my_command', 'Acme\HelloBundle\Command\MyCommand') |
| 61 | + ->addTag('console.command') |
| 62 | + ; |
| 63 | +
|
| 64 | +Here are some use cases. |
| 65 | + |
| 66 | +Use dependencies and parameters in configure |
| 67 | +-------------------------------------------- |
| 68 | + |
| 69 | +For example, imagine you want to provide a default value for the ``name`` |
| 70 | +argument. You could: |
| 71 | + |
| 72 | +* hard code a string and pass it as the 4th argument of ``addArgument``; |
| 73 | +* allow the user to set the default value in the configuration; |
| 74 | +* retrieve the default value from a service (a repository for example). |
| 75 | + |
| 76 | +With a ``ContainerAwareCommand`` you wouldn't be able to retrieve the |
| 77 | +configuration parameter, because the ``configure`` method is called in the |
| 78 | +command's constructor. The only solution is to inject them through its |
| 79 | +constructor: |
| 80 | + |
| 81 | + <?php |
| 82 | + // src/Acme/DemoBundle/Command/GreetCommand.php |
| 83 | + namespace Acme\DemoBundle\Command; |
| 84 | + |
| 85 | + use Acme\DemoBundle\Entity\NameRepository; |
| 86 | + use Symfony\Component\Console\Command\Command; |
| 87 | + use Symfony\Component\Console\Input\InputArgument; |
| 88 | + use Symfony\Component\Console\Input\InputInterface; |
| 89 | + use Symfony\Component\Console\Input\InputOption; |
| 90 | + use Symfony\Component\Console\Output\OutputInterface; |
| 91 | + |
| 92 | + class GreetCommand extends Command |
| 93 | + { |
| 94 | + protected $nameRepository; |
| 95 | + |
| 96 | + public function __construct(NameRepository $nameRepository) |
| 97 | + { |
| 98 | + $this->nameRepository = $nameRepository; |
| 99 | + } |
| 100 | + |
| 101 | + protected function configure() |
| 102 | + { |
| 103 | + $defaultName = $this->nameRepository->findLastOne(); |
| 104 | + |
| 105 | + $this |
| 106 | + ->setName('demo:greet') |
| 107 | + ->setDescription('Greet someone') |
| 108 | + ->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?', $defaultName) |
| 109 | + ; |
| 110 | + } |
| 111 | + |
| 112 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 113 | + { |
| 114 | + $name = $input->getArgument('name'); |
| 115 | + |
| 116 | + $output->writeln($name); |
| 117 | + } |
| 118 | + } |
0 commit comments