-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Promote Invokable Commands #1580
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
Open
yceruto
wants to merge
1
commit into
main
Choose a base branch
from
invokable_command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,9 @@ | |
use App\Entity\User; | ||
use App\Repository\UserRepository; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Attribute\Option; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
@@ -42,54 +42,47 @@ | |
#[AsCommand( | ||
name: 'app:list-users', | ||
description: 'Lists all the existing users', | ||
aliases: ['app:users'] | ||
aliases: ['app:users'], | ||
help: <<<'HELP' | ||
The <info>%command.name%</info> command lists all the users registered in the application: | ||
|
||
<info>php %command.full_name%</info> | ||
|
||
By default the command only displays the 50 most recent users. Set the number of | ||
results to display with the <comment>--max-results</comment> option: | ||
|
||
<info>php %command.full_name%</info> <comment>--max-results=2000</comment> | ||
|
||
In addition to displaying the user list, you can also send this information to | ||
the email address specified in the <comment>--send-to</comment> option: | ||
|
||
<info>php %command.full_name%</info> <comment>[email protected]</comment> | ||
HELP, | ||
)] | ||
final class ListUsersCommand extends Command | ||
final class ListUsersCommand | ||
{ | ||
public function __construct( | ||
private readonly MailerInterface $mailer, | ||
#[Autowire('%app.notifications.email_sender%')] | ||
private readonly string $emailSender, | ||
private readonly UserRepository $users, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setHelp(<<<'HELP' | ||
The <info>%command.name%</info> command lists all the users registered in the application: | ||
|
||
<info>php %command.full_name%</info> | ||
|
||
By default the command only displays the 50 most recent users. Set the number of | ||
results to display with the <comment>--max-results</comment> option: | ||
|
||
<info>php %command.full_name%</info> <comment>--max-results=2000</comment> | ||
|
||
In addition to displaying the user list, you can also send this information to | ||
the email address specified in the <comment>--send-to</comment> option: | ||
|
||
<info>php %command.full_name%</info> <comment>[email protected]</comment> | ||
HELP | ||
) | ||
// commands can optionally define arguments and/or options (mandatory and optional) | ||
// see https://symfony.com/doc/current/components/console/console_arguments.html | ||
->addOption('max-results', null, InputOption::VALUE_OPTIONAL, 'Limits the number of users listed', 50) | ||
->addOption('send-to', null, InputOption::VALUE_OPTIONAL, 'If set, the result is sent to the given email address') | ||
; | ||
} | ||
|
||
/** | ||
* This method is executed after initialize(). It usually contains the logic | ||
* to execute to complete this command task. | ||
* | ||
* Commands can optionally define arguments and/or options (mandatory and optional) | ||
* | ||
* @see https://symfony.com/doc/current/console/input.html | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
/** @var int|null $maxResults */ | ||
$maxResults = $input->getOption('max-results'); | ||
|
||
public function __invoke( | ||
InputInterface $input, | ||
OutputInterface $output, | ||
#[Option('If set, the result is sent to the given email address', 'send-to')] ?string $email = null, | ||
#[Option('Limits the number of users listed')] int $maxResults = 50, | ||
): int { | ||
// Use ->findBy() instead of ->findAll() to allow result sorting and limiting | ||
$allUsers = $this->users->findBy([], ['id' => 'DESC'], $maxResults); | ||
|
||
|
@@ -122,9 +115,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
$usersAsATable = $bufferedOutput->fetch(); | ||
$output->write($usersAsATable); | ||
|
||
/** @var string|null $email */ | ||
$email = $input->getOption('send-to'); | ||
|
||
if (null !== $email) { | ||
$this->sendReport($usersAsATable, $email); | ||
} | ||
|
Empty file.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should probably not extend
Command
here if you want to showcase the simplest usage of invokable commands.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.
This one is still not possible without extending from
Command
as we're showcasing the initialize and interact methods.It’s the
ListUsersCommand
that showcases the simplest usage