Skip to content

Commit

Permalink
bug #5379 [Cookbook][Console] don't use BufferedOutput on Symfony 2.3…
Browse files Browse the repository at this point in the history
… (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[Cookbook][Console] don't use BufferedOutput on Symfony 2.3

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | all
| Fixed tickets | #5299 (comment)

As @stof pointed out in #5299 (comment), the `StreamOutput` class is not available on Symfony 2.3. This should be reverted after merging it up to the `2.6` branch.

Commits
-------

4e9e0dd don't use BufferedOutput on Symfony 2.3
  • Loading branch information
wouterj committed Jun 19, 2015
2 parents 8f4aa8c + 4e9e0dd commit 94aed12
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions cookbook/console/command_in_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Run this command from inside your controller via::
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\HttpFoundation\Response;

class SpoolController extends Controller
Expand All @@ -46,12 +46,14 @@ Run this command from inside your controller via::
'--message-limit' => $messages,
));
// You can use NullOutput() if you don't need the output
$output = new BufferedOutput();
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL);
$application->run($input, $output);

// return the output, don't use if you used NullOutput()
$content = $output->fetch();
rewind($output->getStream());
$content = stream_get_contents($output->getStream());
fclose($output->getStream());

// return new Response(""), if you used NullOutput()
return new Response($content);
}
Expand All @@ -60,7 +62,7 @@ Run this command from inside your controller via::
Showing Colorized Command Output
--------------------------------

By telling the ``BufferedOutput`` it is decorated via the second parameter,
By telling the ``StreamOutput`` it is decorated via the third parameter,
it will return the Ansi color-coded content. The `SensioLabs AnsiToHtml converter`_
can be used to convert this to colorful HTML.

Expand All @@ -76,8 +78,8 @@ Now, use it in your controller::
namespace AppBundle\Controller;

use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\HttpFoundation\Response;
// ...

Expand All @@ -86,15 +88,14 @@ Now, use it in your controller::
public function sendSpoolAction($messages = 10)
{
// ...
$output = new BufferedOutput(
OutputInterface::VERBOSITY_NORMAL,
true // true for decorated
);
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL, true);
// ...

// return the output
$converter = new AnsiToHtmlConverter();
$content = $output->fetch();
rewind($output->getStream());
$content = stream_get_contents($output->getStream());
fclose($output->getStream());

return new Response($converter->convert($content));
}
Expand Down

0 comments on commit 94aed12

Please sign in to comment.