Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae27dd1

Browse filesBrowse files
committed
bug #5415 Updating for AppBundle and purposefully *not* doing work on configure (weaverryan)
This PR was merged into the 2.6 branch. Discussion ---------- Updating for AppBundle and purposefully *not* doing work on configure | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.6+ | Fixed tickets | n/a @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. Thanks! Commits ------- 6d87827 fixing standard and fixing missing argument in php+xml formats 6b1c640 Updating for AppBundle and purposefully *not* doing work on configure
2 parents 6d3d892 + 6d87827 commit ae27dd1
Copy full SHA for ae27dd1

File tree

Expand file treeCollapse file tree

1 file changed

+69
-16
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+69
-16
lines changed

‎cookbook/console/commands_as_services.rst

Copy file name to clipboardExpand all lines: cookbook/console/commands_as_services.rst
+69-16Lines changed: 69 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,60 @@ 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+
<argument>%command.default_name%</argument>
159+
<tag name="console.command" />
160+
</service>
161+
</services>
162+
</container>
163+
164+
.. code-block:: php
165+
166+
// app/config/config.php
167+
$container->setParameter('command.default_name', 'Javier');
168+
169+
$container
170+
->register(
171+
'app.command.my_command',
172+
'AppBundle\Command\MyCommand',
173+
)
174+
->setArguments(array('%command.default_name%'))
175+
->addTag('console.command')
176+
;
177+
178+
Great, you now have a dynamic default value!
126179

127180
.. caution::
128181

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.