Important: #101567 - Use Symfony attribute to autoconfigure cli commands¶
See forge#101567
Description¶
The Symfony PHP attribute \Symfony\
is now accepted to register console commands.
This way CLI commands can be registered by setting the attribute on the command
class. Only the parameters command
, description
, aliases
and hidden
are
still viable. In order to overwrite the schedulable parameter use the old
Services.
way to register console commands. By default schedulable
is true.
Before:
MyVendor\MyExtension\Command\MyCommand:
tags:
- name: 'console.command'
command: 'myprefix:dofoo'
description: 'My description'
schedulable: true
- name: 'console.command'
command: 'myprefix:dofoo-alias'
alias: true
After:
The registration can be removed from the Services.
file and the
attribute is assigned to the command class instead:
<?php
namespace MyVendor\MyExtension\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand(
name: 'myprefix:dofoo',
description: 'My description',
aliases: ['myprefix:dofoo-alias']
)]
class MyCommand extends Command
{
}
Impact¶
The registration of cli commands is simplified that way.
When using this attribute there is no need to register the command in the
Services.
file. Existing configurations work as before.