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

我正在开发一个自定义CLI命令&我想知道从PHP代码调用其他命令的最佳方法是什么(没有shell_exec()或类似命令).
例如:
当运行“PHP bin / magento my:custom:command”时,它会做的事情&最后将运行“PHP bin / magento cache:flush”.

有任何想法吗?

谢谢.

解决方法:

Magento CLI构建于Symfony控制台之上.您可以使用此组件加载和运行其他命令:

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

更多信息here.虽然它不太可能对您有任何问题,但请注意文档表明这并不总是最好的主意:

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.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...