New in Symfony 5.3: Lazy Command Description

Contributed by Nicolas Grekas in #39851.

Symfony introduced lazy commands back in 2017 for its 3.4 version. That change made applications faster and more robust because commands no longer needed to be instantiated to register them in the application. However, Symfony commands aren’t fully lazy. If you run bin/console without arguments (or the equivalent bin/console list command) you see the entire list of commands registered in the application and their descriptions. This requires instantiating all commands, because their description is not lazy. You might think that this is not important, because you rarely list commands and it’s OK if it takes a few milliseconds to run. That’s true, but this makes it harder/impossible to implement another feature that we want to include in future Symfony versions: console autocompletion. That’s why in Symfony 5.3 we’ve introduced a way to define command descriptions (and command aliases) lazily, like the command name. First, you can now add the description attribute to the console.command tag when defining the command in a configuration file: 1 2 3 4 5# config/services.yaml app.command.my_command:

...

tags:
    - { name: console.command, command: app:my-command, description: '...' }

If you prefer to define all this inside the command class, add the new $defaultDescription static property: 1 2 3 4 5 6 7 8 9 10 11 12// src/Command/MyCommand.php namespace App\Command;

use Symfony\Component\Console\Command\Command;

class MyCommand extends Command { protected static $defaultName = 'app:my-command'; protected static $defaultDescription = '...';

// ...

}

You can now define command aliases lazily too (which will also be important for the future console autocompletion). To do so, add the aliases inside the $defaultName property separated with pipe characters (|): 1protected static $defaultName = 'app:my-command|project:my-command|legacy:foo';

If your application uses PHP 8, keep in mind that you have another way of defining this lazy command configuration with the AsCommand attribute: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15// src/Command/MyCommand.php namespace App\Command;

use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command;

[AsCommand(

name: 'app:my-command',
description: '...',
aliases: ['project:my-command', 'legacy:foo']

)] class MyCommand extends Command { // ... }

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/vgDzCyKssug/new-in-symfony-5-3-lazy-command-description

Utworzony 4y | 3 cze 2021, 07:20:07


Zaloguj się, aby dodać komentarz

Inne posty w tej grupie

New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

22 kwi 2025, 09:10:36 | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

20 kwi 2025, 08:30:06 | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

16 kwi 2025, 16:30:02 | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

15 kwi 2025, 14:50:24 | Symfony
A Week of Symfony #954 (April 7–13, 2025)

This week, Symfony 7.3 entered its "feature freeze" period in preparation for its release at the end of May 2025. Development activity focused on refining and polishing its new features, including a n

13 kwi 2025, 09:40:02 | Symfony
SymfonyOnline June 2025: What's New in Symfony 7.3

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

10 kwi 2025, 16:40:34 | Symfony
SymfonyCon Amsterdam 2025: Last days to enjoy early bird tickets!

The international Symfony conference of the year, SymfonyCon Amsterdam 2025, will take place in the Netherlands on November 27 & 28, 2025!

⏳ Early bird registration ends this Wednesday! D

8 kwi 2025, 09:10:13 | Symfony