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

Vytvorené 4y | 3. 6. 2021, 7:20:07


Ak chcete pridať komentár, prihláste sa

Ostatné príspevky v tejto skupine

Just one month to go before SymfonyLive Paris 2025 workshops begin!

SymfonyLive Paris 2025, conference in French language only, will already start in 1 month with the workshops! Have a look on the topics and join us! Schedule details are available here.

📣

25. 2. 2025, 15:20:33 | Symfony
New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

24. 2. 2025, 16:20:03 | Symfony
SymfonyLive Paris 2025 : Du lego de composants pour un bundle Gotenberg !

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

24. 2. 2025, 13:50:07 | Symfony
A Week of Symfony #947 (17-23 February 2025)

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

23. 2. 2025, 10:10:09 | Symfony
SymfonyLive Berlin 2025: Agentic Applications with Symfony

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

21. 2. 2025, 9:20:12 | Symfony
SymfonyLive Paris 2025 : Postgres pour vos besoins NoSQL

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

20. 2. 2025, 10:10:13 | Symfony
SymfonyLive Berlin 2025: Asynchronous PHP

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

19. 2. 2025, 8:40:05 | Symfony