从一个命令中运行多个 Symfony 控制台命令

6 php symfony-components symfony-console

我在 Symfony 控制台应用程序中定义了两个命令,clean-redis-keys并且clean-temp-files. 我想定义一个clean执行这两个命令的命令。

我该怎么做?

Wou*_*r J 5

请参阅有关如何调用其他命令的文档:

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

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

    // ...
}
Run Code Online (Sandbox Code Playgroud)

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

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