我正在开发一个自定义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.