从命令中运行多个Symfony控制台命令 [英] Run multiple Symfony console commands, from within a command

查看:117
本文介绍了从命令中运行多个Symfony控制台命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Symfony控制台应用程序中定义了两个命令,分别为clean-redis-keysclean-temp-files.我想定义一个执行这两个命令的命令clean.

I have two commands defined in a Symfony console application, clean-redis-keys and clean-temp-files. I want to define a command clean that executes these two commands.

我应该怎么做?

推荐答案

请参见如何调用其他命令:

从另一个命令中调用命令很简单:

Calling a command from another one is straightforward:

use Symfony\Component\Console\Input\ArrayInput;
// ...

protected function execute(InputInterface $input, OutputInterface $output)
{
    $command = $this->getApplication()->find('demo:greet');

    $arguments = array(
        'command' => 'demo:greet',
        'name'    => 'Fabien',
        '--yell'  => true,
    );

    $greetInput = new ArrayInput($arguments);
    $returnCode = $command->run($greetInput, $output);

    // ...
}

首先,通过传递命令名称来find()您要执行的命令.然后,您需要使用要传递给命令的参数和选项创建一个新的ArrayInput.

First, you find() the command you want to execute by passing the command name. Then, you need to create a new ArrayInput with the arguments and options you want to pass to the command.

最终,调用run()方法实际上会执行命令并从命令返回返回的代码(命令的execute()方法的返回值).

Eventually, calling the run() method actually executes the command and returns the returned code from the command (return value from command's execute() method).

这篇关于从命令中运行多个Symfony控制台命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆