【发布时间】:2015-11-21 07:26:09
【问题描述】:
我正在使用 Symfony 2 控制台组件编写控制台应用程序,并且我想在我的应用程序的第一行之前(或作为)运行控制台清除命令。
我做了一些研究,正在努力寻找这是否可行。
【问题讨论】:
我正在使用 Symfony 2 控制台组件编写控制台应用程序,并且我想在我的应用程序的第一行之前(或作为)运行控制台清除命令。
我做了一些研究,正在努力寻找这是否可行。
【问题讨论】:
我一直在寻找类似的功能。据我所知,symfony 控制台没有内置任何东西可以清除屏幕,但您仍然可以通过使用转义码来重置终端来实现clear 行为,如下所示:
$output->write(sprintf("\033\143"));
请参阅帖子“Clear the Ubuntu bash screen for real”和“What commands can I use to reset and clear my terminal?”以获得对代码的更广泛解释,以及我从中获取代码的这个 symfony 控制台pull request,它试图模拟clear 功能.
【讨论】:
正如doc 所说,你可以使用这样的东西:
$kernel = $this->getContainer()->get('kernel');
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput(array(
'command' => 'cache:clear',
));
// You can use NullOutput() if you don't need the output
$output = new BufferedOutput();
$application->run($input, $output);
// return the output, don't use if you used NullOutput()
$content = $output->fetch();
当您使用命令时,您的命令必须扩展 ContainerAwareCommand,以便您可以访问容器和您的服务。
【讨论】: