没有 OutputInterface 的 Symfony2 控制台输出 [英] Symfony2 Console Output without OutputInterface

查看:36
本文介绍了没有 OutputInterface 的 Symfony2 控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Symfony 控制台命令中将一些信息打印到控制台.通常你会做这样的事情:

受保护的函数执行(InputInterface $input, OutputInterface $output){$name = $input->getArgument('name');如果($名称){$text = '你好'.$name;} 别的 {$text = '你好';}if ($input->getOption('yell')) {$text = strtoupper($text);}$output->writeln($text);}

示例的完整代码 -Symfony 文档

很遗憾,我无法访问 OutputInterface.是否可以将消息打印到控制台?

不幸的是,我无法将 OutputInterface 传递给我想打印一些输出的类.

解决方案

了解定时调试的问题,你可以随时用 echovar_dump 打印调试信息

如果你打算使用一个没有 Symfony 应用程序的命令和全局调试消息,这里有一种方法可以做到这一点.

Symfony 提供 3 种不同的 OutputInterfaces

调试到文件

这样做,每当您在命令中调用 $output->writeln() 时,它都会在 /path/to/debug/file.log 中写入一个新行

使用Symfony\Component\Console\Output\StreamOutput;使用 Symfony\Component\Console\Input\ArrayInput;使用 Acme\FooBundle\Command\MyCommand;$params = 数组();$input = new ArrayInput($params);$file = '/path/to/debug/file.log';$handle = fopen($file, 'w+');$output = new StreamOutput($handle);$command = 新的我的命令;$command->run($input, $output);fclose($handle);

在控制台中调试

这是相同的过程,只是您使用 ConsoleOutput 代替

use Symfony\Component\Console\Output\ConsoleOutput;使用 Symfony\Component\Console\Input\ArrayInput;使用 Acme\FooBundle\Command\MyCommand;$params = 数组();$input = new ArrayInput($params);$output = new ConsoleOutput();$command = 新的我的命令;$command->run($input, $output);

无调试

不会打印任何消息

use Symfony\Component\Console\Output\NullOutput;使用 Symfony\Component\Console\Input\ArrayInput;使用 Acme\FooBundle\Command\MyCommand;$params = 数组();$input = new ArrayInput($params);$output = new NullOutput();$command = 新的我的命令;$command->run($input, $output);

I am trying to print some Information to the Console in a Symfony Console Command. Regularly you would do something like this:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $name = $input->getArgument('name');
    if ($name) {
        $text = 'Hello '.$name;
    } else {
        $text = 'Hello';
    }

    if ($input->getOption('yell')) {
        $text = strtoupper($text);
    }

    $output->writeln($text);
}

For the full Code of the Example - Symfony Documentation

Unfortunately I can't access the OutputInterface. Is it possible to print a Message to the Console?

Unfortunately I can't pass the OutputInterface to the Class where I want to print some Output.

解决方案

Understanding the matter of ponctual debugging, you can always print debug messages with echo or var_dump

If you plan to use a command without Symfony's application with global debug messages, here's a way to do this.

Symfony offers 3 different OutputInterfaces

  • NullOutput - Will result in no output at all and keep the command quiet
  • ConsoleOutput - Will result in console messages
  • StreamOutput - Will result in printing messages into a given stream

Debugging to a file

Doing such, whenever you call $output->writeln() in your command, it will write a new line in /path/to/debug/file.log

use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Acme\FooBundle\Command\MyCommand;

$params = array();
$input  = new ArrayInput($params);

$file   = '/path/to/debug/file.log';
$handle = fopen($file, 'w+');
$output = new StreamOutput($handle);

$command = new MyCommand;
$command->run($input, $output);

fclose($handle);

Debugging in the console

It is quietly the same process, except that you use ConsoleOutput instead

use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Acme\FooBundle\Command\MyCommand;

$params = array();
$input  = new ArrayInput($params);

$output = new ConsoleOutput();

$command = new MyCommand;
$command->run($input, $output);

No debugging

No message will be printed

use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Acme\FooBundle\Command\MyCommand;

$params = array();
$input  = new ArrayInput($params);

$output = new NullOutput();

$command = new MyCommand;
$command->run($input, $output);

这篇关于没有 OutputInterface 的 Symfony2 控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆