Symfony 在同一文件中注册多个控制台命令
创建控制台命令需要创建一个PHP文件,后缀为command.PHP。 例如,我想创建DocumentCommand.php 困难来了。我想在这里插入3个命令,例如:Symfony 在同一文件中注册多个控制台命令,symfony,symfony-console,Symfony,Symfony Console,创建控制台命令需要创建一个PHP文件,后缀为command.PHP。 例如,我想创建DocumentCommand.php 困难来了。我想在这里插入3个命令,例如: document:unused:check document:unused:delete document:used:size 如果可能的话,在单个文件(DocumentCommand.php)中执行该操作的最佳方法是什么 尊敬的,R.尝试以下方法: class TestCommand extends ContainerAware
document:unused:check
document:unused:delete
document:used:size
如果可能的话,在单个文件(DocumentCommand.php)中执行该操作的最佳方法是什么
尊敬的,R.尝试以下方法:
class TestCommand extends ContainerAwareCommand
{
private $progress;
protected function configure()
{
$this
->setName('acme:test')
->setDescription('Write your description here.')
->addArgument(
'argument1',
InputArgument::REQUIRED,
'Which will be your first argument?'
)->addArgument(
'argument2',
InputArgument::REQUIRED,
'Which will be your second argument?'
)
->addOption(
'extra-option',
null,
InputOption::VALUE_NONE,
'Whether or not to do something optional'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$argument1 = $input->getArgument('argument1');
$argument2 = $input->getArgument('argument2');
$extraOption = $input->getArgument('extra-option');
$output->writeln("Argument 1 is $argument1 , argument is $argument2");
$output->writeln(PHP_EOL);
if ($extraOption != null)
{
$output->writeln("Extra option is selected");
$output->writeln(PHP_EOL);
}
if($argument1 == 'unused' && $argument2 == 'check')
{
/*
* Do your stuff
*/
}
/**
* Do something here acording to the arguments passed
*/
return;
}
}
使用此选项,您可以在终端中运行(从项目目录):
您可以根据需要添加更多必需或可选参数,并在根据逻辑检测到未使用的检查/删除时执行某些操作
编辑:(使用别名)
这是关于只有一个命令“acme:test”:)我的问题是关于如何在单个文件中嵌入多个命令。好吧,看看这个文件:“Symfony\Component\Console\command”如图所示,“acme:test”只是命令的name属性。您可以做的是将一些别名写入命令,如“acme:test:alias1”,并根据所选别名执行操作。。这将是一种只在一个文件中实现目标的方法:)查看我所做的编辑我非常感谢您的尝试,但这不是我想要的。此外,我做了一些研究,并且必须认识到,在一个XYZCommand.php文件中不能放置多个命令。不得不接受,它需要为每个eachen命令创建一个新的命令:(无论如何,我真的非常感谢你的努力,
php app/console acme:test unused check --extra-option
class TestCommand extends ContainerAwareCommand
{
private $progress;
protected function configure()
{
$this
->setName('document:command')
->setDescription('Write your description here.')
->addArgument(
'action',
InputArgument::REQUIRED,
'Which will be your first argument? (check/delete/size)'
)
->addOption(
'extra-option',
null,
InputOption::VALUE_NONE,
'Whether or not to do something optional'
)
->setAliases(array('document:unused', 'document:used'))
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$action = $input->getArgument('action');
$commandSelected = $input->getFirstArgument();
$extraOption = $input->getArgument('extra-option');
if ($commandSelected == 'document:unused')
{
/*
* Do something
*/
}
else if ($commandSelected == 'document:used')
{
/*
* Do something
*/
}
if ($extraOption != null)
{
$output->writeln("Extra option is selected");
$output->writeln(PHP_EOL);
}
if($action== 'check')
{
/*
* Do your stuff
*/
}
else if($action == 'delete')
{
/*
* Do your stuff
*/
}
else if($action == 'size')
{
/*
* Do your stuff
*/
}
/**
* Do something here acording to the arguments passed
*/
return;
}
}