微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐