Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating for AppBundle and purposefully *not* doing work on configure #5415

Merged
merged 2 commits into from
Jun 30, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 67 additions & 16 deletions cookbook/console/commands_as_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ with ``console.command``:

# app/config/config.yml
services:
acme_hello.command.my_command:
class: Acme\HelloBundle\Command\MyCommand
app.command.my_command:
class: AppBundle\Command\MyCommand
tags:
- { name: console.command }

Expand All @@ -42,9 +42,8 @@ with ``console.command``:
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="acme_hello.command.my_command"
class="Acme\HelloBundle\Command\MyCommand">

<service id="app.command.my_command"
class="AppBundle\Command\MyCommand">
<tag name="console.command" />
</service>
</services>
Expand All @@ -55,8 +54,8 @@ with ``console.command``:
// app/config/config.php
$container
->register(
'acme_hello.command.my_command',
'Acme\HelloBundle\Command\MyCommand'
'app.command.my_command',
'AppBundle\Command\MyCommand'
)
->addTag('console.command')
;
Expand All @@ -74,31 +73,32 @@ pass one of the following as the 5th argument of ``addOption()``:
By extending ``ContainerAwareCommand``, only the first is possible, because you
can't access the container inside the ``configure()`` method. Instead, inject
any parameter or service you need into the constructor. For example, suppose you
have some ``NameRepository`` service that you'll use to get your default value::
store the default value in some ``%command.default_name%`` parameter::

// src/Acme/DemoBundle/Command/GreetCommand.php
namespace Acme\DemoBundle\Command;
// src/AppBundle/Command/GreetCommand.php
namespace AppBundle\Command;

use Acme\DemoBundle\Entity\NameRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class GreetCommand extends Command
{
protected $nameRepository;
protected $defaultName;

public function __construct(NameRepository $nameRepository)
public function __construct($defaultName)
{
$this->nameRepository = $nameRepository;
$this->defaultName = $defaultName;

parent::__construct();
}

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

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

Now, just update the arguments of your service configuration like normal to
inject the ``NameRepository``. Great, you now have a dynamic default value!
inject the ``command.default_name`` parameter:

.. configuration-block::

.. code-block:: yaml

# app/config/config.yml
parameters:
command.default_name: Javier

services:
app.command.my_command:
class: AppBundle\Command\MyCommand
arguments: ['%command.default_name%']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we always use double qutoes

tags:
- { name: console.command }

.. code-block:: xml

<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="command.default_name">Javier</parameter>
</parameters>

<services>
<service id="app.command.my_command"
class="AppBundle\Command\MyCommand">
<tag name="console.command" />
</service>
</services>
</container>

.. code-block:: php

// app/config/config.php
$container->setParameter('command.default_name', 'Javier');

$container
->register(
'app.command.my_command',
'AppBundle\Command\MyCommand'
)
->addTag('console.command')
;

Great, you now have a dynamic default value!

.. caution::

Expand Down