Skip to content

Commit 95eafee

Browse files
committed
lets use attributes in commands if possible
1 parent e3e3c61 commit 95eafee

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/Maker/MakeCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bundle\MakerBundle\Generator;
1717
use Symfony\Bundle\MakerBundle\InputConfiguration;
1818
use Symfony\Bundle\MakerBundle\Str;
19+
use Symfony\Component\Console\Attribute\AsCommand;
1920
use Symfony\Component\Console\Command\Command;
2021
use Symfony\Component\Console\Command\LazyCommand;
2122
use Symfony\Component\Console\Input\InputArgument;
@@ -63,6 +64,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
6364
[
6465
'command_name' => $commandName,
6566
'set_description' => !class_exists(LazyCommand::class),
67+
'use_command_attribute' => 80000 <= \PHP_VERSION_ID && class_exists(AsCommand::class),
6668
]
6769
);
6870

src/Resources/skeleton/command/Command.tpl.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22

33
namespace <?= $namespace; ?>;
44

5+
<?php if ($use_attributes && $use_command_attribute): ?>
6+
use Symfony\Component\Console\Attribute\AsCommand;
7+
<?php endif; ?>
58
use Symfony\Component\Console\Command\Command;
69
use Symfony\Component\Console\Input\InputArgument;
710
use Symfony\Component\Console\Input\InputInterface;
811
use Symfony\Component\Console\Input\InputOption;
912
use Symfony\Component\Console\Output\OutputInterface;
1013
use Symfony\Component\Console\Style\SymfonyStyle;
1114

15+
<?php if ($use_attributes && $use_command_attribute): ?>
16+
#[AsCommand(
17+
name: '<?= $command_name; ?>',
18+
description: 'Add a short description for your command',
19+
)]
20+
<?php endif; ?>
1221
class <?= $class_name; ?> extends Command
1322
{
23+
<?php if (!$use_attributes || !$use_command_attribute): ?>
1424
protected static $defaultName = '<?= $command_name; ?>';
1525
protected static $defaultDescription = 'Add a short description for your command';
1626

27+
<?php endif; ?>
1728
protected function configure()
1829
{
1930
$this

tests/Maker/MakeCommandTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class MakeCommandTest extends MakerTestCase
1919
{
20-
public function getTestDetails()
20+
public function getTestDetails(): \Generator
2121
{
2222
yield 'command' => [MakerTestDetails::createTest(
2323
$this->getMakerInstance(MakeCommand::class),
@@ -37,5 +37,24 @@ public function getTestDetails()
3737
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCommandInCustomRootNamespace')
3838
->changeRootNamespace('Custom'),
3939
];
40+
41+
yield 'command_with_attributes' => [MakerTestDetails::createTest(
42+
$this->getMakerInstance(MakeCommand::class),
43+
[
44+
// command name
45+
'app:foo',
46+
])
47+
->setRequiredPhpVersion(80000)
48+
->addRequiredPackageVersion('symfony/console', '>=5.3')
49+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCommand')
50+
->assert(
51+
static function (string $output, string $directory) {
52+
$commandFileContents = file_get_contents(sprintf('%s/src/Command/FooCommand.php', $directory));
53+
54+
self::assertStringContainsString('use Symfony\Component\Console\Attribute\AsCommand;', $commandFileContents);
55+
self::assertStringContainsString('#[AsCommand(', $commandFileContents);
56+
}
57+
),
58+
];
4059
}
4160
}

0 commit comments

Comments
 (0)