-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Console] Document invokable command #20932
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
base: 7.3
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,16 +27,16 @@ Suppose you want to confirm an action before actually executing it. Add | |
the following to your command:: | ||
|
||
// ... | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
|
||
class YourCommand extends Command | ||
#[AsCommand(name: 'app:my-command')] | ||
class MyCommand | ||
{ | ||
// ... | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
public function __invoke(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$helper = $this->getHelper('question'); | ||
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 wont work isnt it ? 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. Good catch! Yes, there is a way, but I’m not sure if it’s simpler to extend Command or to get the helper via: public function __invoke(InputInterface $input, OutputInterface $output, Application $application): int
{
$helper = $application->getHelperSet()->get('question');
// ...
} Anyway, this all looks old-fashioned, because SymfonyStyle can do it better. There’s even a note right at the beginning:
IMO, it’s not just an alternative, but the simplest way to ask questions from now on: public function __invoke(SymfonyStyle $io): int
{
$answer = $io->ask(...);
} 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 we should do the same here as with invokable commands: start with the simpler approach (SymfonyStyle), then mention alternatives for custom needs. wdyt @symfony/docs ? 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 documentation I agree yes, basic usage then more advance 👍🏻 a question just: cant we imagine those helper as console service as injectable in the invoke method ? (can open an dedicated issue for later :)) 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. @yceruto for many items, SymfonyStyle is a good alternative to Console helpers ... but e.g. for the That helper also doesn't provide that much help, but for legacy commands it's better to just inject the |
||
$question = new ConfirmationQuestion('Continue with this action?', false); | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -110,23 +110,19 @@ completion (by default, by pressing the Tab key). | |||||||||
Creating a Command | ||||||||||
------------------ | ||||||||||
|
||||||||||
Commands are defined in classes extending | ||||||||||
:class:`Symfony\\Component\\Console\\Command\\Command`. For example, you may | ||||||||||
want a command to create a user:: | ||||||||||
Commands are defined in classes, for example, you may want a command to create a user:: | ||||||||||
|
||||||||||
// src/Command/CreateUserCommand.php | ||||||||||
namespace App\Command; | ||||||||||
|
||||||||||
use Symfony\Component\Console\Attribute\AsCommand; | ||||||||||
use Symfony\Component\Console\Command\Command; | ||||||||||
use Symfony\Component\Console\Input\InputInterface; | ||||||||||
use Symfony\Component\Console\Output\OutputInterface; | ||||||||||
|
||||||||||
// the name of the command is what users type after "php bin/console" | ||||||||||
#[AsCommand(name: 'app:create-user')] | ||||||||||
class CreateUserCommand extends Command | ||||||||||
class CreateUserCommand | ||||||||||
{ | ||||||||||
protected function execute(InputInterface $input, OutputInterface $output): int | ||||||||||
public function __invoke(): int | ||||||||||
{ | ||||||||||
// ... put here the code to create the user | ||||||||||
|
||||||||||
|
@@ -147,6 +143,10 @@ want a command to create a user:: | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
Additionally, can extend the :class:`Symfony\\Component\\Console\\Command\\Command` class to | ||||||||||
yceruto marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
leverage advanced features like lifecycle hooks: :method:`Symfony\\Component\\Console\\Command\\Command::initialize`, | ||||||||||
:method:`Symfony\\Component\\Console\\Command\\Command::interact`, and built-in helpers. | ||||||||||
Comment on lines
+147
to
+148
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.
Suggested change
|
||||||||||
|
||||||||||
Configuring 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. This whole section needs updating, the text currently no longer matches the updated code example. I think we can remove it completely, and instead merge it with the previous section. Something like You can also use ``#[AsCommand]`` to add a description and longer help text for the command::
// src/Command/CreateUserCommand.php
#[AsCommand(
name: 'app:create-user',
description: 'Creates a new user.', // the command description shown when running "php bin/console list"
help: 'This command allows you to create a user...', // the command help shown when running the command with the "--help" option
)]
class CreateUserCommand
{
public function __invoke(): int
{
// ...
}
}
Additionally, you can extend the :class:`Symfony\\Component\\Console\\Command\\Command`
class to leverage advanced features like lifecycle hooks (e.g.
:method:`Symfony\\Component\\Console\\Command\\Command::initialize`,
:method:`Symfony\\Component\\Console\\Command\\Command::interact`) and built-in
helpers. |
||||||||||
~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
|
@@ -156,18 +156,16 @@ You can optionally define a description, help message and the | |||||||||
|
||||||||||
// src/Command/CreateUserCommand.php | ||||||||||
|
||||||||||
// ... | ||||||||||
class CreateUserCommand extends Command | ||||||||||
#[AsCommand( | ||||||||||
name: 'app:create-user', | ||||||||||
description: 'Creates a new user.', // the command description shown when running "php bin/console list" | ||||||||||
help: 'This command allows you to create a user...', // the command help shown when running the command with the "--help" option | ||||||||||
)] | ||||||||||
class CreateUserCommand | ||||||||||
{ | ||||||||||
// ... | ||||||||||
protected function configure(): void | ||||||||||
public function __invoke(): int | ||||||||||
{ | ||||||||||
$this | ||||||||||
// the command description shown when running "php bin/console list" | ||||||||||
->setDescription('Creates a new user.') | ||||||||||
// the command help shown when running the command with the "--help" option | ||||||||||
->setHelp('This command allows you to create a user...') | ||||||||||
; | ||||||||||
// ... | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
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. The next section ("Registering the Command") also needs rework, the document now only shows the However, we need to document the final paragraph ("If you can't use PHP attributes, register the command as a service [...]") somewhere in this article I think. |
||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -18,16 +18,15 @@ the returned code from the command (return value from command ``execute()`` | |||
method):: | ||||
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 paragraph needs rework, it talks about the |
||||
|
||||
// ... | ||||
use Symfony\Component\Console\Attribute\AsCommand; | ||||
use Symfony\Component\Console\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.
Suggested change
|
||||
use Symfony\Component\Console\Input\ArrayInput; | ||||
use Symfony\Component\Console\Input\InputInterface; | ||||
use Symfony\Component\Console\Output\OutputInterface; | ||||
|
||||
class CreateUserCommand extends Command | ||||
#[AsCommand(name: 'app:create-user')] | ||||
class CreateUserCommand | ||||
Comment on lines
+26
to
+27
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. maybe we should make all these changes with the #AsCommand attribute in a separate PR to avoid growing this PR? 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’d actually prefer to keep all these related changes in a single PR, since they’re closely tied together. It helps keep the context in one place and makes it easier to review everything as a whole. In any case, we can go ahead and merge this one, then open a new PR for the remaining updates. |
||||
{ | ||||
// ... | ||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int | ||||
public function __invoke(OutputInterface $output): int | ||||
{ | ||||
$greetInput = new ArrayInput([ | ||||
// the command name is passed as first argument | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,27 +16,16 @@ For example, suppose you want to log something from within your command:: | |
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
#[AsCommand(name: 'app:sunshine')] | ||
class SunshineCommand extends Command | ||
#[AsCommand(name: 'app:sunshine', description: 'Good morning!')] | ||
class SunshineCommand | ||
{ | ||
public function __construct( | ||
private LoggerInterface $logger, | ||
) { | ||
// you *must* call the parent constructor | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Good morning!'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
public function __invoke(): int | ||
{ | ||
$this->logger->info('Waking up the sun'); | ||
// ... | ||
|
@@ -70,7 +59,7 @@ To make your command lazily loaded, either define its name using the PHP | |
// ... | ||
|
||
#[AsCommand(name: 'app:sunshine')] | ||
class SunshineCommand extends Command | ||
class SunshineCommand | ||
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 reuse this command in the example below to configure it using the |
||
{ | ||
// ... | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.