Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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