|
| 1 | +.. index:: |
| 2 | + single: Console; How to Call a Command from a 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 from your controller. |
| 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 | +.. caution:: |
| 18 | + |
| 19 | + In comparison with a direct call from the console, calling a command from a controller |
| 20 | + has a slight performance impact because of the request stack overhead. This way of |
| 21 | + calling a command is only useful for small tasks. |
| 22 | + |
| 23 | +An example of this is sending the emails that Swift Mailer spooled 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:: |
| 26 | + |
| 27 | + // src/AppBundle/Controller/SpoolController.php |
| 28 | + namespace AppBundle\Controller; |
| 29 | + |
| 30 | + use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 31 | + use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
| 32 | + use Symfony\Component\Console\Input\ArrayInput; |
| 33 | + use Symfony\Component\Console\Output\StreamOutput; |
| 34 | + |
| 35 | + class SpoolController extend Controller |
| 36 | + { |
| 37 | + public function sendSpoolAction($messages = 10) |
| 38 | + { |
| 39 | + $kernel = $this->get('kernel'); |
| 40 | + $application = new Application($kernel); |
| 41 | + $application->setAutoExit(false); |
| 42 | + |
| 43 | + $input = new ArrayInput(array( |
| 44 | + 'command' => 'swiftmailer:spool:send', |
| 45 | + '--message-limit' => $messages, |
| 46 | + )); |
| 47 | + $output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL); |
| 48 | + $application->run($input, $output); |
| 49 | + |
| 50 | + rewind($output->getStream()); |
| 51 | + $content = stream_get_contents($output->getStream()); |
| 52 | + fclose($output->getStream()); |
| 53 | + |
| 54 | + return $content; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +Showing Colorized Command Output |
| 59 | +-------------------------------- |
| 60 | + |
| 61 | +By telling the ``StreamOutput`` it is decorated via the third parameter, it will return |
| 62 | +the Ansi color-coded content. The `SensioLabs AnsiToHtml converter`_ can be required |
| 63 | +using ``Composer`` and helps you to get colorful HTML:: |
| 64 | + |
| 65 | + // src/AppBundle/Controller/SpoolController.php |
| 66 | + namespace AppBundle\Controller; |
| 67 | + |
| 68 | + use SensioLabs\AnsiConverter\AnsiToHtmlConverter; |
| 69 | + // ... |
| 70 | + |
| 71 | + class SpoolController extend Controller |
| 72 | + { |
| 73 | + public function sendSpoolAction($messages = 10) |
| 74 | + { |
| 75 | + // ... |
| 76 | + |
| 77 | + $converter = new AnsiToHtmlConverter(); |
| 78 | + return $converter->convert($content); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +The ``AnsiToHtmlConverter`` can also be registered `as a Twig Extension`_, |
| 83 | +and supports optional themes. |
| 84 | + |
| 85 | +.. _`SensioLabs AnsiToHtml converter`: https://github.com/sensiolabs/ansi-to-html |
| 86 | +.. _`as a Twig Extension`: https://github.com/sensiolabs/ansi-to-html#twig-integration |
0 commit comments