Skip to content
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

[make:*] use sorted use statements #1106

Merged
merged 21 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[make:command] use sorted use statements
  • Loading branch information
jrushlow committed Apr 26, 2022
commit 42369476b6836d7f430413a3764bf2cead89c1dd
36 changes: 32 additions & 4 deletions src/Maker/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,30 @@
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
use Symfony\Bundle\MakerBundle\Util\TemplateComponentGenerator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
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;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Ryan Weaver <weaverryan@gmail.com>
*/
final class MakeCommand extends AbstractMaker
{
private $phpCompatUtil;
OskarStark marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(PhpCompatUtil $phpCompatUtil)
{
$this->phpCompatUtil = $phpCompatUtil;
}

public static function getCommandName(): string
{
return 'make:command';
Expand All @@ -38,15 +50,15 @@ public static function getCommandDescription(): string
return 'Creates a new console command class';
}

public function configureCommand(Command $command, InputConfiguration $inputConf)
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->addArgument('name', InputArgument::OPTIONAL, sprintf('Choose a command name (e.g. <fg=yellow>app:%s</>)', Str::asCommand(Str::getRandomTerm())))
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeCommand.txt'))
;
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$commandName = trim($input->getArgument('name'));
$commandNameHasAppPrefix = 0 === strpos($commandName, 'app:');
Expand All @@ -58,13 +70,29 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
sprintf('The "%s" command name is not valid because it would be implemented by "%s" class, which is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores).', $commandName, Str::asClassName($commandName, 'Command'))
);

$useStatements = [
Command::class,
InputArgument::class,
InputInterface::class,
InputOption::class,
OutputInterface::class,
SymfonyStyle::class,
];
jrushlow marked this conversation as resolved.
Show resolved Hide resolved

$canUseCommandAttribute = class_exists(AsCommand::class) && $this->phpCompatUtil->canUseAttributes();

if ($canUseCommandAttribute) {
$useStatements[] = AsCommand::class;
}

$generator->generateClass(
$commandClassNameDetails->getFullName(),
'command/Command.tpl.php',
[
'use_statements' => TemplateComponentGenerator::generateUseStatements($useStatements),
'command_name' => $commandName,
'set_description' => !class_exists(LazyCommand::class),
'use_command_attribute' => 80000 <= \PHP_VERSION_ID && class_exists(AsCommand::class),
'use_command_attribute' => $canUseCommandAttribute,
]
);

Expand All @@ -77,7 +105,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
]);
}

public function configureDependencies(DependencyBuilder $dependencies)
public function configureDependencies(DependencyBuilder $dependencies): void
{
$dependencies->addClassDependency(
Command::class,
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</service>

<service id="maker.maker.make_command" class="Symfony\Bundle\MakerBundle\Maker\MakeCommand">
<argument type="service" id="maker.php_compat_util" />
<tag name="maker.command" />
</service>

Expand Down
16 changes: 4 additions & 12 deletions src/Resources/skeleton/command/Command.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

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): ?>
<?= $use_statements; ?>

<?php if ($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): ?>
<?php if (!$use_command_attribute): ?>
protected static $defaultName = '<?= $command_name; ?>';
protected static $defaultDescription = 'Add a short description for your command';

Expand Down