Skip to content

Commit 6b1c640

Browse files
committed
Updating for AppBundle and purposefully *not* doing work on configure
@tacman pointed out that we have a caution that says to *not* do work on configure, like make database queries, but that's exactly what we were doing. So, I replaced it with a parameter. He also noted that the final configuration block was missing and would have been helpful to him.
1 parent 179526c commit 6b1c640

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

cookbook/console/commands_as_services.rst

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ with ``console.command``:
2727
2828
# app/config/config.yml
2929
services:
30-
acme_hello.command.my_command:
31-
class: Acme\HelloBundle\Command\MyCommand
30+
app.command.my_command:
31+
class: AppBundle\Command\MyCommand
3232
tags:
3333
- { name: console.command }
3434
@@ -42,9 +42,8 @@ with ``console.command``:
4242
http://symfony.com/schema/dic/services/services-1.0.xsd">
4343
4444
<services>
45-
<service id="acme_hello.command.my_command"
46-
class="Acme\HelloBundle\Command\MyCommand">
47-
45+
<service id="app.command.my_command"
46+
class="AppBundle\Command\MyCommand">
4847
<tag name="console.command" />
4948
</service>
5049
</services>
@@ -55,8 +54,8 @@ with ``console.command``:
5554
// app/config/config.php
5655
$container
5756
->register(
58-
'acme_hello.command.my_command',
59-
'Acme\HelloBundle\Command\MyCommand'
57+
'app.command.my_command',
58+
'AppBundle\Command\MyCommand'
6059
)
6160
->addTag('console.command')
6261
;
@@ -74,31 +73,32 @@ pass one of the following as the 5th argument of ``addOption()``:
7473
By extending ``ContainerAwareCommand``, only the first is possible, because you
7574
can't access the container inside the ``configure()`` method. Instead, inject
7675
any parameter or service you need into the constructor. For example, suppose you
77-
have some ``NameRepository`` service that you'll use to get your default value::
76+
store the default value in some ``%command.default_name%`` parameter::
7877

79-
// src/Acme/DemoBundle/Command/GreetCommand.php
80-
namespace Acme\DemoBundle\Command;
78+
// src/AppBundle/Command/GreetCommand.php
79+
namespace AppBundle\Command;
8180

82-
use Acme\DemoBundle\Entity\NameRepository;
8381
use Symfony\Component\Console\Command\Command;
8482
use Symfony\Component\Console\Input\InputInterface;
8583
use Symfony\Component\Console\Input\InputOption;
8684
use Symfony\Component\Console\Output\OutputInterface;
8785

8886
class GreetCommand extends Command
8987
{
90-
protected $nameRepository;
88+
protected $defaultName;
9189

92-
public function __construct(NameRepository $nameRepository)
90+
public function __construct($defaultName)
9391
{
94-
$this->nameRepository = $nameRepository;
92+
$this->defaultName = $defaultName;
9593
9694
parent::__construct();
9795
}
9896

9997
protected function configure()
10098
{
101-
$defaultName = $this->nameRepository->findLastOne();
99+
// try to avoid work here (e.g. database query)
100+
// this method is *always* called - see warning below
101+
$defaultName = $this->defaultName;
102102

103103
$this
104104
->setName('demo:greet')
@@ -122,7 +122,58 @@ have some ``NameRepository`` service that you'll use to get your default value::
122122
}
123123

124124
Now, just update the arguments of your service configuration like normal to
125-
inject the ``NameRepository``. Great, you now have a dynamic default value!
125+
inject the ``command.default_name`` parameter:
126+
127+
.. configuration-block::
128+
129+
.. code-block:: yaml
130+
131+
# app/config/config.yml
132+
parameters:
133+
command.default_name: Javier
134+
135+
services:
136+
app.command.my_command:
137+
class: AppBundle\Command\MyCommand
138+
arguments: ['%command.default_name%']
139+
tags:
140+
- { name: console.command }
141+
142+
.. code-block:: xml
143+
144+
<!-- app/config/config.xml -->
145+
<?xml version="1.0" encoding="UTF-8" ?>
146+
<container xmlns="http://symfony.com/schema/dic/services"
147+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
148+
xsi:schemaLocation="http://symfony.com/schema/dic/services
149+
http://symfony.com/schema/dic/services/services-1.0.xsd">
150+
151+
<parameters>
152+
<parameter key="command.default_name">Javier</parameter>
153+
</parameters>
154+
155+
<services>
156+
<service id="app.command.my_command"
157+
class="AppBundle\Command\MyCommand">
158+
<tag name="console.command" />
159+
</service>
160+
</services>
161+
</container>
162+
163+
.. code-block:: php
164+
165+
// app/config/config.php
166+
$container->setParameter('command.default_name', 'Javier');
167+
168+
$container
169+
->register(
170+
'app.command.my_command',
171+
'AppBundle\Command\MyCommand'
172+
)
173+
->addTag('console.command')
174+
;
175+
176+
Great, you now have a dynamic default value!
126177

127178
.. caution::
128179

0 commit comments

Comments
 (0)