运行 多个 Symfony 控制台命令,来自一个命令
Run multiple Symfony console commands, from within a command
我在 Symfony 控制台应用程序中定义了两个命令,clean-redis-keys
和 clean-temp-files
。我想定义一个执行这两个命令的命令clean
。
我应该怎么做?
请参阅 How to Call Other 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);
// ...
}
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.
Eventually, calling the run()
method actually executes the command and returns the returned code from the command (return value from command's execute()
method).
获取应用实例,找到命令并执行它们:
protected function configure()
{
$this->setName('clean');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$app = $this->getApplication();
$cleanRedisKeysCmd = $app->find('clean-redis-keys');
$cleanRedisKeysInput = new ArrayInput([]);
$cleanTempFilesCmd = $app->find('clean-temp-files');
$cleanTempFilesInput = new ArrayInput([]);
// Note if "subcommand" returns an exit code, run() method will return it.
$cleanRedisKeysCmd->run($cleanRedisKeysInput, $output);
$cleanTempFilesCmd->run($cleanTempFilesInput, $output);
}
为避免代码重复,您可以创建通用方法来调用子命令。像这样:
private function executeSubCommand(string $name, array $parameters, OutputInterface $output)
{
return $this->getApplication()
->find($name)
->run(new ArrayInput($parameters), $output);
}
我在 Symfony 控制台应用程序中定义了两个命令,clean-redis-keys
和 clean-temp-files
。我想定义一个执行这两个命令的命令clean
。
我应该怎么做?
请参阅 How to Call Other 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); // ... }
First, you
find()
the command you want to execute by passing the command name. Then, you need to create a newArrayInput
with the arguments and options you want to pass to the command.Eventually, calling the
run()
method actually executes the command and returns the returned code from the command (return value from command'sexecute()
method).
获取应用实例,找到命令并执行它们:
protected function configure()
{
$this->setName('clean');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$app = $this->getApplication();
$cleanRedisKeysCmd = $app->find('clean-redis-keys');
$cleanRedisKeysInput = new ArrayInput([]);
$cleanTempFilesCmd = $app->find('clean-temp-files');
$cleanTempFilesInput = new ArrayInput([]);
// Note if "subcommand" returns an exit code, run() method will return it.
$cleanRedisKeysCmd->run($cleanRedisKeysInput, $output);
$cleanTempFilesCmd->run($cleanTempFilesInput, $output);
}
为避免代码重复,您可以创建通用方法来调用子命令。像这样:
private function executeSubCommand(string $name, array $parameters, OutputInterface $output)
{
return $this->getApplication()
->find($name)
->run(new ArrayInput($parameters), $output);
}