|
| 1 | +.. index:: |
| 2 | + single: Console; Use commands in your Controller |
| 3 | + |
| 4 | +How to Call a Command from a Controller |
| 5 | +======================================= |
| 6 | + |
| 7 | +The :doc:`Console component documentation </components/console/introduction>` covers |
| 8 | +how to create a console command. This cookbook article covers how to use a console command |
| 9 | +directly in your application. |
| 10 | + |
| 11 | +You may have the need to execute some function that is only available in a console command. |
| 12 | +Usually, you should refactor the command and move some logic into a service that can be |
| 13 | +reused in the controller. However, when the command is part of a third-party library, you |
| 14 | +wouldn't want to modify or duplicate their code, but want to directly execute the command |
| 15 | +instead. |
| 16 | + |
| 17 | +An example of this is sending the emails that Swift Mailer spooled earlier |
| 18 | +:doc:`using the ``swiftmailer:spool:send`` command </cookbook/email/spool>`. Symfony |
| 19 | +allows you to directly execute a registered ``Command`` inside your Controller:: |
| 20 | + |
| 21 | + // src/AcmeBundle/Controller/SpoolController.php |
| 22 | + namespace AcmeBundle\Controller; |
| 23 | + |
| 24 | + use Symfony\Component\Console\Output\StreamOutput; |
| 25 | + use Symfony\Component\Console\Input\ArrayInput; |
| 26 | + use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 27 | + |
| 28 | + class SpoolController |
| 29 | + { |
| 30 | + public function sendSpoolAction($messages=10) |
| 31 | + { |
| 32 | + $kernel = $this->get('kernel'); |
| 33 | + $application = new Application($kernel); |
| 34 | + $application->setAutoExit(false); |
| 35 | + |
| 36 | + $input = new ArrayInput(array( |
| 37 | + 'command' => 'swiftmailer:spool:send', |
| 38 | + '--message-limit' => $messages, |
| 39 | + )); |
| 40 | + $output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL); |
| 41 | + $application->run($input, $output); |
| 42 | + |
| 43 | + rewind($output->getStream()); |
| 44 | + $content = stream_get_contents($output->getStream()); |
| 45 | + fclose($output->getStream()); |
| 46 | + |
| 47 | + return $content; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | +Showing Colorized Command Output |
| 52 | +-------------------------------- |
| 53 | + |
| 54 | +By telling the ``StreamOutput`` it is decorated via the third parameter, it will return |
| 55 | +the Ansi color-coded content. The `SensioLabs AnsiToHtml converter`_ can be required |
| 56 | +using ``Composer`` and helps you to get colorful html:: |
| 57 | + |
| 58 | + // src/AppBundle/Controller/SpoolController.php |
| 59 | + namespace AppBundle\Controller; |
| 60 | + |
| 61 | + use Symfony\Component\Console\Output\StreamOutput; |
| 62 | + use Symfony\Component\Console\Input\ArrayInput; |
| 63 | + use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 64 | + use SensioLabs\AnsiConverter\AnsiToHtmlConverter; |
| 65 | + |
| 66 | + class SpoolController |
| 67 | + { |
| 68 | + public function sendSpoolAction($messages=10) |
| 69 | + { |
| 70 | + $kernel = $this->get('kernel'); |
| 71 | + $application = new Application($kernel); |
| 72 | + $application->setAutoExit(false); |
| 73 | + |
| 74 | + $input = new ArrayInput(array( |
| 75 | + 'command' => 'swiftmailer:spool:send', |
| 76 | + '--message-limit' => $messages, |
| 77 | + )); |
| 78 | + $output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL, true); |
| 79 | + $application->run($input, $output); |
| 80 | + |
| 81 | + rewind($output->getStream()); |
| 82 | + $content = stream_get_contents($output->getStream()); |
| 83 | + fclose($output->getStream()); |
| 84 | + |
| 85 | + $converter = new AnsiToHtmlConverter(); |
| 86 | + $content = $converter->convert($content); |
| 87 | + |
| 88 | + return $content; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +The ``AnsiToHtmlConverter`` can also be registered `as a Twig Extension`_, |
| 93 | +and supports optional themes. |
| 94 | + |
| 95 | +.. _`SensioLabs AnsiToHtml converter`: https://github.com/sensiolabs/ansi-to-html |
| 96 | +.. _`as a Twig Extension`: https://github.com/sensiolabs/ansi-to-html#twig-integration |
0 commit comments