简体   繁体   English

如何在Symfony2控制台应用程序中添加辅助输出?

[英]How to add a secondary output in a Symfony2 Console application?

I have a Symfony2 Console application with a few commands: 我有一个带有一些命令的Symfony2 Console应用程序:

$application = new Application();

$application->add(new MyCommand1());
$application->add(new MyCommand2());

$application->run();

When running the application, I'd like the output to be redirected both to the console and to a file. 运行应用程序时,我希望将输出重定向到控制台文件。

I can use StreamOutput for this, but it looks like I can only provide such an output object if I manually run one of the commands: 我可以为此使用StreamOutput ,但看起来如果我手动运行以下命令之一,则只能提供这样的输出对象:

$input = ...;
$output = new StreamOutput(fopen('output.log', 'a'));

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

But this is not what I want. 但这不是我想要的。

Is it possible to add a secondary output to the Application itself? 是否可以向Application本身添加辅助输出? So that all commands output both to the console, and to a file. 这样所有命令都输出到控制台和文件。

To achieve this task, I would create my own Output. 为了完成此任务,我将创建自己的输出。

class MultipleOutput implements OutputInterface
{
    protected $outputs = array();

    public function __construct(array $outputs = array())
    {
        $this->setOutputs($outputs);
    }

    public function setOutputs(array $outputs = array())
    {
        foreach ($outputs as $output) {
            $this->addOutput($outputs);
        }
    }

    public function addOutput(OutputInterface $output)
    {
        $this->outputs[] = $output;
    }

    public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
    {
        foreach ($this->outputs as $output) {
            $output->write($messages, $newline, $type);
        }
    }

    public function writeln($messages, $type = self::OUTPUT_NORMAL)
    {
        foreach ($this->outputs as $output) {
            $output->writeln($messages, $type);
        }
    }

    public function setVerbosity($level)
    {
        foreach ($this->outputs as $output) {
            $output->setVerbosity($level);
        }
    }

    /**
     * Returns only the first one
     */
    public function getVerbosity()
    {
        foreach ($this->outputs as $output) {
            return $output->getVerbosity();
        }
    }

    public function setDecorated($decorated)
    {
        foreach ($this->outputs as $output) {
            $output->setDecorated($decorated);
        }
    }

    /**
     * Returns only the first one
     */
    public function isDecorated()
    {
        foreach ($this->outputs as $output) {
            return $output->isDecorated();
        }
    }

    public function setFormatter(OutputFormatterInterface $formatter)
    {
        foreach ($this->outputs as $output) {
            $output->setFormatter($formatter);
        }
    }

    /**
     * Returns only the first one
     */
    public function getFormatter()
    {
        foreach ($this->outputs as $output) {
            return $output->getFormatter();
        }
    }
}

Then you can use it in your application 然后可以在应用程序中使用它

$application = new Application();

$application->add(new MyCommand1());
$application->add(new MyCommand2());

$output = new MultipleOutput(array(new StreamOutput, new ConsoleOutput));

$application->run(new ArgvInput, $output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM