-
-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathMakeCommand.php
More file actions
149 lines (131 loc) · 5.48 KB
/
MakeCommand.php
File metadata and controls
149 lines (131 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MakerBundle\Maker;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
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\UseStatementGenerator;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\Option;
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\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\Kernel;
/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Ryan Weaver <weaverryan@gmail.com>
*/
final class MakeCommand extends AbstractMaker
{
public function __construct(private ?PhpCompatUtil $phpCompatUtil = null)
{
if (null !== $phpCompatUtil) {
@trigger_deprecation(
'symfony/maker-bundle',
'1.55.0',
\sprintf('Initializing MakeCommand while providing an instance of "%s" is deprecated. The $phpCompatUtil param will be removed in a future version.', PhpCompatUtil::class),
);
}
}
public static function getCommandName(): string
{
return 'make:command';
}
public static function getCommandDescription(): string
{
return 'Create a new console command class';
}
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())))
->addOption('invokable', null, InputOption::VALUE_NEGATABLE, 'Generate an invokable command (using PHP attributes) starting from Symfony 7.3?')
->setHelp($this->getHelpFileContents('MakeCommand.txt'))
;
}
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
if ($input->getOption('invokable') === null) {
$description = $command->getDefinition()->getOption('invokable')->getDescription();
$question = new ConfirmationQuestion($description, Kernel::VERSION_ID >= 70300);
$invokable = $io->askQuestion($question);
$input->setOption('invokable', $invokable);
}
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$commandName = trim($input->getArgument('name'));
$commandNameHasAppPrefix = str_starts_with($commandName, 'app:');
$commandClassNameDetails = $generator->createClassNameDetails(
$commandNameHasAppPrefix ? substr($commandName, 4) : $commandName,
'Command\\',
'Command',
\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'))
);
if ($input->getOption('invokable')) {
$useStatements = new UseStatementGenerator([
Command::class,
SymfonyStyle::class,
AsCommand::class,
Argument::class,
Option::class,
]);
$generator->generateClass(
$commandClassNameDetails->getFullName(),
'command/InvokableCommand.tpl.php',
[
'use_statements' => $useStatements,
'command_name' => $commandName,
]
);
} else {
$useStatements = new UseStatementGenerator([
Command::class,
InputArgument::class,
InputInterface::class,
InputOption::class,
OutputInterface::class,
SymfonyStyle::class,
AsCommand::class,
]);
$generator->generateClass(
$commandClassNameDetails->getFullName(),
'command/Command.tpl.php',
[
'use_statements' => $useStatements,
'command_name' => $commandName,
'set_description' => !class_exists(LazyCommand::class),
]
);
}
$generator->writeChanges();
$this->writeSuccessMessage($io);
$io->text([
'Next: open your new command class and customize it!',
'Find the documentation at <fg=yellow>https://symfony.com/doc/current/console.html</>',
]);
}
public function configureDependencies(DependencyBuilder $dependencies): void
{
$dependencies->addClassDependency(
Command::class,
'console'
);
}
}