From 59f8c95e5e2e426ac41a651836a8c8c54a9eb655 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sat, 23 May 2015 12:08:08 -0400 Subject: [PATCH 1/2] Tweaks to the new using commands in a controller article --- cookbook/console/command_in_controller.rst | 46 +++++++++++++++------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/cookbook/console/command_in_controller.rst b/cookbook/console/command_in_controller.rst index f17bcaea019..4545411604d 100644 --- a/cookbook/console/command_in_controller.rst +++ b/cookbook/console/command_in_controller.rst @@ -12,17 +12,17 @@ You may have the need to execute some function that is only available in a console command. Usually, you should refactor the command and move some logic into a service that can be reused in the controller. However, when the command is part of a third-party library, you wouldn't want to modify or duplicate -their code, but want to directly execute the command instead. +their code. Instead, you can execute the command directly. .. caution:: In comparison with a direct call from the console, calling a command from a controller has a slight performance impact because of the request stack - overhead. This way of calling a command is only useful for small tasks. + overhead. -An example of this is sending the emails that Swift Mailer spooled earlier -:doc:`using the swiftmailer:spool:send command `. Symfony -allows you to directly execute a registered command inside your controller:: +Imagine you want to send spooled Swift Mailer messages by +:doc:`using the swiftmailer:spool:send command `. +Run this command from inside your controller via:: // src/AppBundle/Controller/SpoolController.php namespace AppBundle\Controller; @@ -30,7 +30,7 @@ allows you to directly execute a registered command inside your controller:: use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Console\Input\ArrayInput; - use Symfony\Component\Console\Output\StreamOutput; + use Symfony\Component\Console\Output\BufferedOutput; class SpoolController extends Controller { @@ -44,28 +44,38 @@ allows you to directly execute a registered command inside your controller:: 'command' => 'swiftmailer:spool:send', '--message-limit' => $messages, )); - $output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL); + // our use NullOutput() if you don't need the outpu + $output = new BufferedOutput(); $application->run($input, $output); - rewind($output->getStream()); - $content = stream_get_contents($output->getStream()); - fclose($output->getStream()); + // return the output + $content = $output->fetch(); - return $content; + return new Response($content); } } Showing Colorized Command Output -------------------------------- -By telling the ``StreamOutput`` it is decorated via the third parameter, +By telling the ``BufferedOutput`` it is decorated via the second parameter, it will return the Ansi color-coded content. The `SensioLabs AnsiToHtml converter`_ -can be required using ``Composer`` and helps you getting colorful HTML:: +can be used to convert this to colorful HTML. + +First, require the package: + +.. code-block:: bash + + composer require sensiolabs/ansi-to-html + +Now, use it in your controller:: // src/AppBundle/Controller/SpoolController.php namespace AppBundle\Controller; use SensioLabs\AnsiConverter\AnsiToHtmlConverter; + use Symfony\Component\Console\Output\BufferedOutput; + use Symfony\Component\Console\Output\OutputInterface; // ... class SpoolController extends Controller @@ -73,9 +83,17 @@ can be required using ``Composer`` and helps you getting colorful HTML:: public function sendSpoolAction($messages = 10) { // ... + $output = new BufferedOutput( + OutputInterface::VERBOSITY_NORMAL, + true // true for decorated + ); + // ... + // return the output $converter = new AnsiToHtmlConverter(); - return $converter->convert($content); + $content = $output->fetch(); + + return new Response($converter->convert($content)); } } From aad727761af99f27b1910f5ecc370d8feaa0c2af Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sat, 23 May 2015 12:19:14 -0400 Subject: [PATCH 2/2] Fixing things thanks to Wouter --- cookbook/console/command_in_controller.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cookbook/console/command_in_controller.rst b/cookbook/console/command_in_controller.rst index 4545411604d..36a7d1e79a9 100644 --- a/cookbook/console/command_in_controller.rst +++ b/cookbook/console/command_in_controller.rst @@ -31,6 +31,7 @@ Run this command from inside your controller via:: use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\BufferedOutput; + use Symfony\Component\HttpFoundation\Response; class SpoolController extends Controller { @@ -66,7 +67,7 @@ First, require the package: .. code-block:: bash - composer require sensiolabs/ansi-to-html + $ composer require sensiolabs/ansi-to-html Now, use it in your controller:: @@ -76,6 +77,7 @@ Now, use it in your controller:: use SensioLabs\AnsiConverter\AnsiToHtmlConverter; use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Console\Output\OutputInterface; + use Symfony\Component\HttpFoundation\Response; // ... class SpoolController extends Controller