Skip to content

Commit

Permalink
lets use attributes in commands if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jrushlow committed Apr 8, 2021
1 parent e3e3c61 commit 95eafee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Maker/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
[
'command_name' => $commandName,
'set_description' => !class_exists(LazyCommand::class),
'use_command_attribute' => 80000 <= \PHP_VERSION_ID && class_exists(AsCommand::class),
]
);

Expand Down
11 changes: 11 additions & 0 deletions src/Resources/skeleton/command/Command.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

namespace <?= $namespace; ?>;

<?php if ($use_attributes && $use_command_attribute): ?>
use Symfony\Component\Console\Attribute\AsCommand;
<?php endif; ?>
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

<?php if ($use_attributes && $use_command_attribute): ?>
#[AsCommand(
name: '<?= $command_name; ?>',
description: 'Add a short description for your command',
)]
<?php endif; ?>
class <?= $class_name; ?> extends Command
{
<?php if (!$use_attributes || !$use_command_attribute): ?>
protected static $defaultName = '<?= $command_name; ?>';
protected static $defaultDescription = 'Add a short description for your command';

<?php endif; ?>
protected function configure()
{
$this
Expand Down
21 changes: 20 additions & 1 deletion tests/Maker/MakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class MakeCommandTest extends MakerTestCase
{
public function getTestDetails()
public function getTestDetails(): \Generator
{
yield 'command' => [MakerTestDetails::createTest(
$this->getMakerInstance(MakeCommand::class),
Expand All @@ -37,5 +37,24 @@ public function getTestDetails()
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCommandInCustomRootNamespace')
->changeRootNamespace('Custom'),
];

yield 'command_with_attributes' => [MakerTestDetails::createTest(
$this->getMakerInstance(MakeCommand::class),
[
// command name
'app:foo',
])
->setRequiredPhpVersion(80000)
->addRequiredPackageVersion('symfony/console', '>=5.3')
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCommand')
->assert(
static function (string $output, string $directory) {
$commandFileContents = file_get_contents(sprintf('%s/src/Command/FooCommand.php', $directory));

self::assertStringContainsString('use Symfony\Component\Console\Attribute\AsCommand;', $commandFileContents);
self::assertStringContainsString('#[AsCommand(', $commandFileContents);
}
),
];
}
}

0 comments on commit 95eafee

Please sign in to comment.