Skip to content

[Console] Use AsCommand attribute in all commands #16488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/console/changing_default_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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"');
Expand Down
5 changes: 2 additions & 3 deletions components/console/console_arguments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,20 @@ 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;
use Symfony\Component\Console\Input\InputInterface;
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'),
Expand Down
16 changes: 5 additions & 11 deletions components/console/logger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions console/commands_as_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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';

// ...
}

Expand Down
16 changes: 5 additions & 11 deletions console/hide_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down