diff --git a/components/console/changing_default_command.rst b/components/console/changing_default_command.rst index 6eb9f2b5227..6a2fe877478 100644 --- a/components/console/changing_default_command.rst +++ b/components/console/changing_default_command.rst @@ -10,14 +10,14 @@ name to the ``setDefaultCommand()`` method:: namespace Acme\Console\Command; + use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; + #[AsCommand(name: 'hello:world')] class HelloWorldCommand extends Command { - protected static $defaultName = 'hello:world'; - protected function configure() { $this->setDescription('Outputs "Hello World"'); diff --git a/components/console/console_arguments.rst b/components/console/console_arguments.rst index 79f5c6c1f4c..5b641c26774 100644 --- a/components/console/console_arguments.rst +++ b/components/console/console_arguments.rst @@ -14,6 +14,7 @@ Have a look at the following command that has three options:: namespace Acme\Console\Command; + use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputDefinition; @@ -21,14 +22,12 @@ Have a look at the following command that has three options:: use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; + #[AsCommand(name: 'demo:args', description: 'Describe args behaviors')] class DemoArgsCommand extends Command { - protected static $defaultName = 'demo:args'; - protected function configure() { $this - ->setDescription('Describe args behaviors') ->setDefinition( new InputDefinition([ new InputOption('foo', 'f'), diff --git a/components/console/logger.rst b/components/console/logger.rst index 8f029e47002..25fce56d7d9 100644 --- a/components/console/logger.rst +++ b/components/console/logger.rst @@ -37,24 +37,18 @@ You can rely on the logger to use this dependency inside a command:: namespace Acme\Console\Command; use Acme\MyDependency; + use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Logger\ConsoleLogger; use Symfony\Component\Console\Output\OutputInterface; + #[AsCommand( + name: 'my:command', + description: 'Use an external dependency requiring a PSR-3 logger' + )] class MyCommand extends Command { - protected static $defaultName = 'my:command'; - - protected function configure() - { - $this - ->setDescription( - 'Use an external dependency requiring a PSR-3 logger' - ) - ; - } - protected function execute(InputInterface $input, OutputInterface $output) { $logger = new ConsoleLogger($output); diff --git a/console.rst b/console.rst index fdf668a375d..a7fe52108b1 100644 --- a/console.rst +++ b/console.rst @@ -29,13 +29,15 @@ want a command to create a user:: // src/Command/CreateUserCommand.php namespace App\Command; + use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; + // the name of the command is what users type after "php bin/console" + #[AsCommand(name: 'app:create-user')] class CreateUserCommand extends Command { - // the name of the command (the part after "bin/console") protected static $defaultName = 'app:create-user'; protected function configure(): void diff --git a/console/commands_as_services.rst b/console/commands_as_services.rst index 6323f21ac50..d279c762ec6 100644 --- a/console/commands_as_services.rst +++ b/console/commands_as_services.rst @@ -18,13 +18,14 @@ For example, suppose you want to log something from within your command:: namespace App\Command; use Psr\Log\LoggerInterface; + use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; + #[AsCommand(name: 'app:sunshine')] class SunshineCommand extends Command { - protected static $defaultName = 'app:sunshine'; private $logger; public function __construct(LoggerInterface $logger) @@ -68,12 +69,15 @@ command and start logging. Lazy Loading ------------ -To make your command lazily loaded, either define its ``$defaultName`` static property:: +To make your command lazily loaded, either define its name using the PHP +``AsCommand`` attribute:: + use Symfony\Component\Console\Attribute\AsCommand; + // ... + + #[AsCommand(name: 'app:sunshine')] class SunshineCommand extends Command { - protected static $defaultName = 'app:sunshine'; - // ... } diff --git a/console/hide_commands.rst b/console/hide_commands.rst index 2f9d2819873..44a69d09289 100644 --- a/console/hide_commands.rst +++ b/console/hide_commands.rst @@ -8,25 +8,19 @@ However, sometimes commands are not intended to be run by end-users; for example, commands for the legacy parts of the application, commands exclusively run through scheduled tasks, etc. -In those cases, you can define the command as **hidden** by setting the -``setHidden()`` method to ``true`` in the command configuration:: +In those cases, you can define the command as **hidden** by setting to ``true`` +the ``hidden`` property of the ``AsCommand`` attribute:: // src/Command/LegacyCommand.php namespace App\Command; + use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; + #[AsCommand(name: 'app:legacy', hidden: true)] class LegacyCommand extends Command { - protected static $defaultName = 'app:legacy'; - - protected function configure(): void - { - $this - ->setHidden(true) - // ... - ; - } + // ... } Hidden commands behave the same as normal commands but they are no longer displayed