-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Console] Command as service #3621
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
Changes from 1 commit
947ad92
a055140
a7b916e
cdd534a
e137951
6a7a25f
11bfe50
e8b3320
c8fe610
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
.. index:: | ||
single: Console; Commands as Services | ||
|
||
How to define Commands as Services | ||
================================== | ||
|
||
.. versionadded:: 2.4 | ||
Support for registering commands in the service container was added in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] was introduced |
||
version 2.4. | ||
|
||
By default, Symfony will take a look in the ``Command`` directory of your | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... of each bundle ... |
||
bundles and automatically register your commands. For the ones implementing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to be careful because we're talking about the
|
||
the ``ContainerAwareCommand`` interface, Symfony will even inject the container. | ||
|
||
While making life easier, this default implementation has some drawbacks in some | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest adding this to the previous paragraph |
||
situations: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... this has some limitations ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I rephrased each bullet point below so that they all sound like limitations (not things that you will be able to do after registering as as service), since we mention limitations/drawbacks here. |
||
|
||
* what if you want your command to be defined elsewhere than in the ``Command`` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to start list items with an uppercase letter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] to be located somewhere different from the (folder or directory?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't folder and directory synonyms? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so. That question was more a note to myself to make that consistent all over the docs |
||
folder? | ||
* what if you want to register conditionally your command, depending on the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] want to conditionally register your command [...] |
||
current environment or on the availability of some dependencies? | ||
* what if you need to access dependencies before the ``setContainer`` is called | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
(for example in the ``configure`` method)? | ||
* what if you want to reuse a command many times, but with different | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better is to remove all the what ifs and just list use cases in which we want to do adhoc things |
||
dependencies or parameters? | ||
|
||
To solve those problems, you can register your command as a service by simply | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
defining it with the ``console.command`` tag: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In these cases we tag ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -1 never use the first person |
||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
services: | ||
acme_hello.command.my_command: | ||
class: Acme\HelloBundle\Command\MyCommand | ||
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"> | ||
|
||
<service id="acme_hello.command.my_command" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing |
||
class="Acme\HelloBundle\Command\MyCommand"> | ||
<tag name="console.command" /> | ||
</service> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. F438remove empty line |
||
$container | ||
->register('acme_hello.command.my_command', 'Acme\HelloBundle\Command\MyCommand') | ||
->addTag('console.command') | ||
; | ||
|
||
Here are some use cases. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like a bit misplaced. The usecases where listed below. I would suggest removing this completely |
||
|
||
Use dependencies and parameters in configure | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Dependencies and Parameters in the configure Method |
||
-------------------------------------------- | ||
|
||
For example, imagine you want to provide a default value for the ``name`` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would start with "Imagine you want to provide a default [...]" |
||
argument. You could: | ||
|
||
* hard code a string and pass it as the 4th argument of ``addArgument``; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hardcode is one word I believe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like many spelling are used: http://en.wikipedia.org/wiki/Hard_coding |
||
* allow the user to set the default value in the configuration; | ||
* retrieve the default value from a service (a repository for example). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] (e.g. a Repository) |
||
|
||
With a ``ContainerAwareCommand`` you wouldn't be able to retrieve the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
configuration parameter, because the ``configure`` method is called in the | ||
command's constructor. The only solution is to inject them through its | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we always have a discussion about object's constructor. I would go for just "constructor": "[...] method is called in the constructor." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and its would be the then |
||
constructor: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use double colon (which is the shortcut for |
||
|
||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove php start tag |
||
// src/Acme/DemoBundle/Command/GreetCommand.php | ||
namespace Acme\DemoBundle\Command; | ||
|
||
use Acme\DemoBundle\Entity\NameRepository; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class GreetCommand extends Command | ||
{ | ||
protected $nameRepository; | ||
|
||
public function __construct(NameRepository $nameRepository) | ||
{ | ||
$this->nameRepository = $nameRepository; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$defaultName = $this->nameRepository->findLastOne(); | ||
|
||
$this | ||
->setName('demo:greet') | ||
->setDescription('Greet someone') | ||
->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?', $defaultName) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$name = $input->getArgument('name'); | ||
|
||
$output->writeln($name); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ Console | |
|
||
introduction | ||
usage | ||
commands_as_services | ||
single_command_tool | ||
events | ||
helpers/index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How to Define Commands as Services (standard is to capitialize all words except from closed-class wo 8000 rds)