11..index ::
22 single: Console; Commands as Services
33
4- How todefine Commands as Services
4+ How toDefine Commands as Services
55==================================
66
77..versionadded ::2.4
8- Support for registering commands in the service container wasadded in
8+ Support for registering commands in the service container wasintroduced in
99 version 2.4.
1010
1111By default, Symfony will take a look in the ``Command `` directory of your
1212bundles and automatically register your commands. For the ones implementing
1313the ``ContainerAwareCommand `` interface, Symfony will even inject the container.
14-
1514While making life easier, this default implementation has some drawbacks in some
1615situations:
1716
18- *what if you want your command to be defined elsewhere than in the ``Command ``
19- folder ?
20- * what if you want toregister conditionally your command, depending on the
17+ *What if you want your command to be defined elsewhere than in the ``Command ``
18+ directory ?
19+ * what if you want to conditionally register your command, depending on the
2120 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)?
21+ * what if you need to access dependencies before the ``setContainer() `` is
22+ called (for example in the ``configure() `` method)?
2423* what if you want to reuse a command many times, but with different
2524 dependencies or parameters?
2625
@@ -46,45 +45,41 @@ defining it with the ``console.command`` tag:
4645xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4746xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
4847
49- <service id =" acme_hello.command.my_command"
50- class =" Acme\HelloBundle\Command\MyCommand" >
51- <tag name =" console.command" />
52- </service >
48+ <services >
49+ <service id =" acme_hello.command.my_command"
50+ class =" Acme\HelloBundle\Command\MyCommand" >
51+ <tag name =" console.command" />
52+ </service >
53+ </services >
5354 </container >
5455
5556 ..code-block ::php
5657
5758 // app/config/config.php
58-
5959 $container
6060 ->register('acme_hello.command.my_command', 'Acme\HelloBundle\Command\MyCommand')
6161 ->addTag('console.command')
6262 ;
6363
64- Here are some use cases.
65-
66- Use dependencies and parameters in configure
67- --------------------------------------------
64+ Use Case: Using Dependencies and Parameters to Set Default Values for Options
65+ -----------------------------------------------------------------------------
6866
69- For example, imagine you want to provide a default value for the ``name ``
70- argument. You could :
67+ Imagine you want to provide a default value for the ``name``option. You could
68+ pass one of the following as the 5th argument of ``addOption() `` :
7169
72- *hard code a string and pass it as the 4th argument of `` addArgument `` ;
73- *allow the user to set thedefault value in the configuration ;
74- *retrieve the default valuefrom a service (a repository for example ).
70+ *an hardcoded string;
71+ *a value coming from the configuration (allows theuser to change it easily) ;
72+ *a valuecomputed by a service (e.g. a repository).
7573
7674With 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:
75+ configuration parameter, because the ``configure() `` method is called in the
76+ constructor. The only solution is to inject them through it::
8077
81- <?php
8278 // src/Acme/DemoBundle/Command/GreetCommand.php
8379 namespace Acme\DemoBundle\Command;
8480
8581 use Acme\DemoBundle\Entity\NameRepository;
8682 use Symfony\Component\Console\Command\Command;
87- use Symfony\C omponent\C onsole\I nput\I nputArgument;
8883 use Symfony\Component\Console\Input\InputInterface;
8984 use Symfony\Component\Console\Input\InputOption;
9085 use Symfony\Component\Console\Output\OutputInterface;
@@ -105,13 +100,13 @@ constructor:
105100 $this
106101 ->setName('demo:greet')
107102 ->setDescription('Greet someone')
108- ->addArgument ('name',InputArgument::OPTIONAL , 'Who do you want to greet?', $defaultName)
103+ ->addOption ('name','-n', InputOption::VALUE_REQUIRED , 'Who do you want to greet?', $defaultName)
109104 ;
110105 }
111106
112107 protected function execute(InputInterface $input, OutputInterface $output)
113108 {
114- $name = $input->getArgument ('name');
109+ $name = $input->getOption ('name');
115110
116111 $output->writeln($name);
117112 }