1
1
.. index ::
2
2
single: Console; Commands as Services
3
3
4
- How to define Commands as Services
4
+ How to Define Commands as Services
5
5
==================================
6
6
7
7
.. versionadded :: 2.4
8
- Support for registering commands in the service container was added in
8
+ Support for registering commands in the service container was introduced in
9
9
version 2.4.
10
10
11
11
By default, Symfony will take a look in the ``Command `` directory of your
12
12
bundles and automatically register your commands. For the ones implementing
13
13
the ``ContainerAwareCommand `` interface, Symfony will even inject the container.
14
-
15
14
While making life easier, this default implementation has some drawbacks in some
16
15
situations:
17
16
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
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
21
20
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)?
24
23
* what if you want to reuse a command many times, but with different
25
24
dependencies or parameters?
26
25
@@ -46,45 +45,41 @@ defining it with the ``console.command`` tag:
46
45
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
47
46
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
48
47
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 >
53
54
</container >
54
55
55
56
.. code-block :: php
56
57
57
58
// app/config/config.php
58
-
59
59
$container
60
60
->register('acme_hello.command.my_command', 'Acme\HelloBundle\Command\MyCommand')
61
61
->addTag('console.command')
62
62
;
63
63
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
+ -----------------------------------------------------------------------------
68
66
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() `` :
71
69
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 ).
70
+ * an hardcoded string;
71
+ * a value coming from the configuration (allows the user to change it easily) ;
72
+ * a value computed by a service (e.g. a repository).
75
73
76
74
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:
75
+ configuration parameter, because the ``configure() `` method is called in the
76
+ constructor. The only solution is to inject them through it::
80
77
81
- <?php
82
78
// src/Acme/DemoBundle/Command/GreetCommand.php
83
79
namespace Acme\DemoBundle\Command;
84
80
85
81
use Acme\DemoBundle\Entity\NameRepository;
86
82
use Symfony\Component\Console\Command\Command;
87
- use Symfony\C omponent\C onsole\I nput\I nputArgument;
88
83
use Symfony\Component\Console\Input\InputInterface;
89
84
use Symfony\Component\Console\Input\InputOption;
90
85
use Symfony\Component\Console\Output\OutputInterface;
@@ -105,13 +100,13 @@ constructor:
105
100
$this
106
101
->setName('demo:greet')
107
102
->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)
109
104
;
110
105
}
111
106
112
107
protected function execute(InputInterface $input, OutputInterface $output)
113
108
{
114
- $name = $input->getArgument ('name');
109
+ $name = $input->getOption ('name');
115
110
116
111
$output->writeln($name);
117
112
}
0 commit comments