在同一文件中注册多个控制台命令

时间:2015-12-11 18:02:35

标签: symfony symfony-console

创建控制台命令需要创建一个后缀为Command.php的PHP文件。 例如,我想创建DocumentCommand.php。

困难来了。我想在这里插入3个命令,例如:

document:unused:check
document:unused:delete
document:used:size

如果可能的话,在单个文件中执行(DocumentCommand.php)的最佳方法是什么?

问候,R。

1 个答案:

答案 0 :(得分:0)

尝试类似:

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;
    }
}

有了这个,您可以在终端(从项目目录)中运行:

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;
}
}