diff --git a/cookbook/console/command_in_controller.rst b/cookbook/console/command_in_controller.rst index 1b4081c4e3d..6ba7c6807ad 100644 --- a/cookbook/console/command_in_controller.rst +++ b/cookbook/console/command_in_controller.rst @@ -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 @@ -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); } @@ -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. @@ -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; // ... @@ -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)); }