简体   繁体   English

Magento 2:如何从另一个CLI命令类运行CLI命令?

[英]Magento 2: How to run CLI command from another CLI command class?

I'm working on a custom CLI command & I was wondering what's the best way to call other commands from the PHP code (without shell_exec() or similar). 我正在开发一个自定义CLI命令,我想知道从PHP代码调用其他命令的最佳方法是什么(没有shell_exec()或类似命令)。
For example: 例如:
When running "php bin/magento my:custom:command", it'll do it's thing & in the end will run "php bin/magento cache:flush". 当运行“php bin / magento my:custom:command”时,它会做它的事情并最终运行“php bin / magento cache:flush”。

Any Ideas? 有任何想法吗?

Thanks. 谢谢。

The Magento CLI is built on top of Symfony Console. Magento CLI构建于Symfony控制台之上。 You can load up and run other commands with this component as such: 您可以使用此组件加载和运行其他命令:

$arguments = new ArrayInput(['command' => 'my:custom:command']);
$this->getApplication()->find('my:custom:command')->run($arguments, $output);

$arguments = new ArrayInput(['command' => 'cache:flush']);
$this->getApplication()->find('cache:flush')->run($arguments, $output);

More information here . 更多信息在这里 Although it's unlikely to be a problem for you, please note that the documentation suggests this is not always the best idea: 虽然这对您来说不太可能有问题,但请注意文档表明这并不总是最好的主意:

Most of the times, calling a command from code that is not executed on the command line is not a good idea. 大多数情况下,从未在命令行上执行的代码调用命令不是一个好主意。 The main reason is that the command's output is optimized for the console and not to be passed to other commands. 主要原因是命令的输出针对控制台进行了优化,而不是传递给其他命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM