Skip to content

Commit

Permalink
[Console] Enable stderr support
Browse files Browse the repository at this point in the history
Bug fix: no
Feature addition: yes
BC break: no
Symfony2 test pass: yes, but some tests had to be modified

Now all error messages goes to stdout, we cannot separate error
from normal behaviour, this enables writing to stderr stream,
so scripts ran e.g. from cron, can benefit from this well known concept.

There are 2 much nicer implememntations, but:
1) First requires to break the `@api` tagged interfaces.
2) Second requires rewrite of `execute` command declatarion all commands in bundles.
  • Loading branch information
canni committed Dec 17, 2011
1 parent be4e538 commit cab0334
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Symfony/Component/Console/Application.php
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\ListCommand;
Expand Down Expand Up @@ -108,7 +109,11 @@ public function run(InputInterface $input = null, OutputInterface $output = null
throw $e;
}

$this->renderException($e, $output);
if ($output instanceof ConsoleOutputInterface) {
$this->renderException($e, $output->getErrorOutput());
} else {
$this->renderException($e, $output);
}
$statusCode = $e->getCode();

$statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1;
Expand Down
38 changes: 37 additions & 1 deletion src/Symfony/Component/Console/Output/ConsoleOutput.php
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Console\Output;

use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;

/**
* ConsoleOutput is the default class for all CLI output. It uses STDOUT.
Expand All @@ -28,8 +30,10 @@
*
* @api
*/
class ConsoleOutput extends StreamOutput
class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
{
private $stderr;

/**
* Constructor.
*
Expand All @@ -43,5 +47,37 @@ class ConsoleOutput extends StreamOutput
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatter $formatter = null)
{
parent::__construct(fopen('php://stdout', 'w'), $verbosity, $decorated, $formatter);
$this->stderr = new StreamOutput(fopen('php://stderr', 'w'), $verbosity, $decorated, $formatter);
}

public function setDecorated($decorated)
{
parent::setDecorated($decorated);
$this->stderr->setDecorated($decorated);
}

public function setFormatter(OutputFormatterInterface $formatter)
{
parent::setFormatter($formatter);
$this->stderr->setFormatter($formatter);
}

public function setVerbosity($level)
{
parent::setVerbosity($level);
$this->stderr->setVerbosity($level);
}

/**
* @return OutputInterface
*/
public function getErrorOutput()
{
return $this->stderr;
}

public function setErrorOutput(OutputInterface $error)
{
$this->stderr = $error;
}
}
30 changes: 30 additions & 0 deletions src/Symfony/Component/Console/Output/ConsoleOutputInterface.php
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Output;

use Symfony\Component\Console\Output\OutputInterface;

/**
* ConsoleOutputInterface is the interface implemented by ConsoleOutput class.
* This adds information about stderr output stream.
*
* @author Dariusz Górecki <darek.krk@gmail.com>
*/
interface ConsoleOutputInterface extends OutputInterface
{
/**
* @return OutputInterface
*/
public function getErrorOutput();

public function setErrorOutput(OutputInterface $error);
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/Tester/ApplicationTester.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

/**
Expand Down

3 comments on commit cab0334

@DocAl
Copy link

@DocAl DocAl commented on cab0334 Jun 11, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this feature was never released.
Any particular reason for that?

@Seldaek
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be part of the 2.1 release coming in the next couple months. beta1 should be available soon.

@DocAl
Copy link

@DocAl DocAl commented on cab0334 Jun 11, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks for the prompt response!

Please sign in to comment.