@@ -12,25 +12,26 @@ You may have the need to execute some function that is only available in a
1212console command. Usually, you should refactor the command and move some logic
1313into a service that can be reused in the controller. However, when the command
1414is part of a third-party library, you wouldn't want to modify or duplicate
15- their code, but want to directly execute the commandinstead .
15+ their code. Instead, you can execute the commanddirectly .
1616
1717..caution ::
1818
1919 In comparison with a direct call from the console, calling a command from
2020 a controller has a slight performance impact because of the request stack
21- overhead. This way of calling a command is only useful for small tasks.
21+ overhead.
2222
23- An example of this is sending the emails that Swift Mailerspooled earlier
24- :doc: `using the swiftmailer:spool:send command </cookbook/email/spool >`. Symfony
25- allows you to directly execute a registered command inside your controller::
23+ Imagine you want to send spooled Swift Mailermessages by
24+ :doc: `using the swiftmailer:spool:send command </cookbook/email/spool >`.
25+ Run this commandfrom inside your controller via ::
2626
2727 // src/AppBundle/Controller/SpoolController.php
2828 namespace AppBundle\Controller;
2929
3030 use Symfony\Bundle\FrameworkBundle\Console\Application;
3131 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
3232 use Symfony\Component\Console\Input\ArrayInput;
33- use Symfony\Component\Console\Output\StreamOutput;
33+ use Symfony\Component\Console\Output\BufferedOutput;
34+ use Symfony\Component\HttpFoundation\Response;
3435
3536 class SpoolController extends Controller
3637 {
@@ -44,38 +45,57 @@ allows you to directly execute a registered command inside your controller::
4445 'command' => 'swiftmailer:spool:send',
4546 '--message-limit' => $messages,
4647 ));
47- $output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL);
48+ // our use NullOutput() if you don't need the outpu
49+ $output = new BufferedOutput();
4850 $application->run($input, $output);
4951
50- rewind($output->getStream());
51- $content = stream_get_contents($output->getStream());
52- fclose($output->getStream());
52+ // return the output
53+ $content = $output->fetch();
5354
54- return $content;
55+ returnnew Response( $content) ;
5556 }
5657 }
5758
5859Showing Colorized Command Output
5960--------------------------------
6061
61- By telling the ``StreamOutput `` it is decorated via thethird parameter,
62+ By telling the ``BufferedOutput `` it is decorated via thesecond parameter,
6263it will return the Ansi color-coded content. The `SensioLabs AnsiToHtml converter `_
63- can be required using ``Composer `` and helps you getting colorful HTML::
64+ can be used to convert this to colorful HTML.
65+
66+ First, require the package:
67+
68+ ..code-block ::bash
69+
70+ $ composer require sensiolabs/ansi-to-html
71+
72+ Now, use it in your controller::
6473
6574 // src/AppBundle/Controller/SpoolController.php
6675 namespace AppBundle\Controller;
6776
6877 use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
78+ use Symfony\Component\Console\Output\BufferedOutput;
79+ use Symfony\Component\Console\Output\OutputInterface;
80+ use Symfony\Component\HttpFoundation\Response;
6981 // ...
7082
7183 class SpoolController extends Controller
7284 {
7385 public function sendSpoolAction($messages = 10)
7486 {
7587 // ...
88+ $output = new BufferedOutput(
89+ OutputInterface::VERBOSITY_NORMAL,
90+ true // true for decorated
91+ );
92+ // ...
7693
94+ // return the output
7795 $converter = new AnsiToHtmlConverter();
78- return $converter->convert($content);
96+ $content = $output->fetch();
97+
98+ return new Response($converter->convert($content));
7999 }
80100 }
81101