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

Établi 4y | 3 juin 2021 à 07:20:07


Connectez-vous pour ajouter un commentaire

Autres messages de ce groupe

Symfony 2024 Year in Review

This blog post highlights the key accomplishments of the Symfony project in 2024. We are grateful for your continuous support, which enabled the Symfony project to achieve a remarkable year.

Releases

7 janv. 2025 à 13:40:05 | Symfony
A Week of Symfony #940 (30 December 2024 - 5 January 2025)

This week, Symfony 6.4.17, 7.1.10 and 7.2.2 maintenance versions were released. In addition, we published more information about the upcoming SymfonyOnline January 2025 conference.

Symfony developmen

5 janv. 2025 à 10:30:12 | Symfony
SymfonyOnline January 2025: Join us in 2 weeks!

Get ready for the exciting SymfonyOnline January 2025, kicking off shortly on January 16-17! There’s still time to register and join the international online Symfony conference—along with pre-

2 janv. 2025 à 12:50:08 | Symfony
Symfony 6.4.17 released

Symfony 6.4.17 has just been released. Here is the list of the most important changes since 6.4.16:

bug #59304 [PropertyInfo] Remove @internal from PropertyReadInfo and PropertyWriteInfo (Dario G
31 déc. 2024 à 16:50:11 | Symfony
Symfony 7.1.10 released

Symfony 7.1.10 has just been released. Here is the list of the most important changes since 7.1.9:

bug #59304 [PropertyInfo] Remove @internal from PropertyReadInfo and PropertyWriteInfo (Dario Gu
31 déc. 2024 à 16:50:10 | Symfony
Symfony 7.2.2 released

Symfony 7.2.2 has just been released. Here is the list of the most important changes since 7.2.1:

bug #59304 [PropertyInfo] Remove @internal from PropertyReadInfo and PropertyWriteInfo (Dario Gua
31 déc. 2024 à 16:50:10 | Symfony
A Week of Symfony #939 (23-29 December 2024)

This week, we launched the new Twig playground, a tool that lets you test and experiment with Twig features in a safe, sandboxed environment. While Symfony development activity was lighter than usual

29 déc. 2024 à 11:30:09 | Symfony