Skip to content

Commit 0a40b0e

Browse files
committed
feature #792 Make commands compatible with LazyCommand (nicolas-grekas)
This PR was merged into the 1.0-dev branch. Discussion ---------- Make commands compatible with LazyCommand Sidekick of symfony/symfony#39851 When run with Symfony < 5.3, the generated commands are made forward-compatible with 5.3+ Commits ------- 42c199a Make commands compatible with LazyCommand
2 parents 01a9058 + 42c199a commit 0a40b0e

25 files changed

+129
-24
lines changed

src/DependencyInjection/CompilerPass/MakeCommandRegistrationPass.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\MakerBundle\Command\MakerCommand;
1515
use Symfony\Bundle\MakerBundle\MakerInterface;
1616
use Symfony\Bundle\MakerBundle\Str;
17+
use Symfony\Component\Console\Command\LazyCommand;
1718
use Symfony\Component\DependencyInjection\ChildDefinition;
1819
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1920
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -36,7 +37,18 @@ public function process(ContainerBuilder $container)
3637
$commandDefinition = new ChildDefinition('maker.auto_command.abstract');
3738
$commandDefinition->setClass(MakerCommand::class);
3839
$commandDefinition->replaceArgument(0, new Reference($id));
39-
$commandDefinition->addTag('console.command', ['command' => $class::getCommandName()]);
40+
41+
$tagAttributes = ['command' => $class::getCommandName()];
42+
43+
if (!method_exists($class, 'getCommandDescription')) {
44+
// no-op
45+
} elseif (class_exists(LazyCommand::class)) {
46+
$tagAttributes['description'] = $class::getCommandDescription();
47+
} else {
48+
$commandDefinition->addMethodCall('setDescription', [$class::getCommandDescription()]);
49+
}
50+
51+
$commandDefinition->addTag('console.command', $tagAttributes);
4052

4153
$container->setDefinition(sprintf('maker.auto_command.%s', Str::asTwigVariable($class::getCommandName())), $commandDefinition);
4254
}

src/Maker/MakeAuthenticator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,14 @@ public static function getCommandName(): string
7373
return 'make:auth';
7474
}
7575

76+
public static function getCommandDescription(): string
77+
{
78+
return 'Creates a Guard authenticator of different flavors';
79+
}
80+
7681
public function configureCommand(Command $command, InputConfiguration $inputConfig)
7782
{
7883
$command
79-
->setDescription('Creates a Guard authenticator of different flavors')
8084
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeAuth.txt'));
8185
}
8286

src/Maker/MakeCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bundle\MakerBundle\InputConfiguration;
1818
use Symfony\Bundle\MakerBundle\Str;
1919
use Symfony\Component\Console\Command\Command;
20+
use Symfony\Component\Console\Command\LazyCommand;
2021
use Symfony\Component\Console\Input\InputArgument;
2122
use Symfony\Component\Console\Input\InputInterface;
2223

@@ -31,10 +32,14 @@ public static function getCommandName(): string
3132
return 'make:command';
3233
}
3334

35+
public static function getCommandDescription(): string
36+
{
37+
return 'Creates a new console command class';
38+
}
39+
3440
public function configureCommand(Command $command, InputConfiguration $inputConf)
3541
{
3642
$command
37-
->setDescription('Creates a new console command class')
3843
->addArgument('name', InputArgument::OPTIONAL, sprintf('Choose a command name (e.g. <fg=yellow>app:%s</>)', Str::asCommand(Str::getRandomTerm())))
3944
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeCommand.txt'))
4045
;
@@ -57,6 +62,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
5762
'command/Command.tpl.php',
5863
[
5964
'command_name' => $commandName,
65+
'set_description' => !class_exists(LazyCommand::class),
6066
]
6167
);
6268

src/Maker/MakeController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ public static function getCommandName(): string
3434
return 'make:controller';
3535
}
3636

37+
public static function getCommandDescription(): string
38+
{
39+
return 'Creates a new controller class';
40+
}
41+
3742
public function configureCommand(Command $command, InputConfiguration $inputConf)
3843
{
3944
$command
40-
->setDescription('Creates a new controller class')
4145
->addArgument('controller-class', InputArgument::OPTIONAL, sprintf('Choose a name for your controller class (e.g. <fg=yellow>%sController</>)', Str::asClassName(Str::getRandomTerm())))
4246
->addOption('no-template', null, InputOption::VALUE_NONE, 'Use this option to disable template generation')
4347
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))

src/Maker/MakeCrud.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ public static function getCommandName(): string
5959
return 'make:crud';
6060
}
6161

62+
public static function getCommandDescription(): string
63+
{
64+
return 'Creates CRUD for Doctrine entity class';
65+
}
66+
6267
/**
6368
* {@inheritdoc}
6469
*/
6570
public function configureCommand(Command $command, InputConfiguration $inputConfig)
6671
{
6772
$command
68-
->setDescription('Creates CRUD for Doctrine entity class')
6973
->addArgument('entity-class', InputArgument::OPTIONAL, sprintf('The class name of the entity to create CRUD (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
7074
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeCrud.txt'))
7175
;

src/Maker/MakeDockerDatabase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ public static function getCommandName(): string
6464
return 'make:docker:database';
6565
}
6666

67+
public static function getCommandDescription(): string
68+
{
69+
return 'Adds a database container to your docker-compose.yaml file';
70+
}
71+
6772
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
6873
{
6974
$command
70-
->setDescription('Adds a database container to your docker-compose.yaml file')
7175
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeDockerDatabase.txt'))
7276
;
7377
}

src/Maker/MakeEntity.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ public static function getCommandName(): string
7575
return 'make:entity';
7676
}
7777

78+
public static function getCommandDescription(): string
79+
{
80+
return 'Creates or updates a Doctrine entity class, and optionally an API Platform resource';
81+
}
82+
7883
public function configureCommand(Command $command, InputConfiguration $inputConf)
7984
{
8085
$command
81-
->setDescription('Creates or updates a Doctrine entity class, and optionally an API Platform resource')
8286
->addArgument('name', InputArgument::OPTIONAL, sprintf('Class name of the entity to create or update (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
8387
->addOption('api-resource', 'a', InputOption::VALUE_NONE, 'Mark this class as an API Platform resource (expose a CRUD API for it)')
8488
->addOption('regenerate', null, InputOption::VALUE_NONE, 'Instead of adding new fields, simply generate the methods (e.g. getter/setter) for existing fields')

src/Maker/MakeFixtures.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ public static function getCommandName(): string
3434
return 'make:fixtures';
3535
}
3636

37+
public static function getCommandDescription(): string
38+
{
39+
return 'Creates a new class to load Doctrine fixtures';
40+
}
41+
3742
public function configureCommand(Command $command, InputConfiguration $inputConf)
3843
{
3944
$command
40-
->setDescription('Creates a new class to load Doctrine fixtures')
4145
->addArgument('fixtures-class', InputArgument::OPTIONAL, 'The class name of the fixtures to create (e.g. <fg=yellow>AppFixtures</>)')
4246
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeFixture.txt'))
4347
;

src/Maker/MakeForm.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ public static function getCommandName(): string
4848
return 'make:form';
4949
}
5050

51+
public static function getCommandDescription(): string
52+
{
53+
return 'Creates a new form class';
54+
}
55+
5156
public function configureCommand(Command $command, InputConfiguration $inputConf)
5257
{
5358
$command
54-
->setDescription('Creates a new form class')
5559
->addArgument('name', InputArgument::OPTIONAL, sprintf('The name of the form class (e.g. <fg=yellow>%sType</>)', Str::asClassName(Str::getRandomTerm())))
5660
->addArgument('bound-class', InputArgument::OPTIONAL, 'The name of Entity or fully qualified model class name that the new form will be bound to (empty for none)')
5761
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeForm.txt'))

src/Maker/MakeFunctionalTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ public static function getCommandName(): string
3434
return 'make:functional-test';
3535
}
3636

37+
public static function getCommandDescription(): string
38+
{
39+
return 'Creates a new functional test class';
40+
}
41+
3742
public function configureCommand(Command $command, InputConfiguration $inputConf)
3843
{
3944
$command
40-
->setDescription('Creates a new functional test class')
4145
->addArgument('name', InputArgument::OPTIONAL, 'The name of the functional test class (e.g. <fg=yellow>DefaultControllerTest</>)')
4246
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeFunctionalTest.txt'))
4347
;

0 commit comments

Comments
 (0)