|
| 1 | +.. index:: |
| 2 | + single: Console; Use commands in your Controller |
| 3 | + |
| 4 | +How to make use of a Command in your application |
| 5 | +=============================== |
| 6 | + |
| 7 | +The Console page of the Components section (:doc:`/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 | +Execute Command directly from Controller |
| 12 | +---------------------------------------- |
| 13 | + |
| 14 | +Symfony allows you to directly execute a ``Command`` inside your Controller:: |
| 15 | + |
| 16 | + // src/AcmeBundle/Controller/GreetController.php |
| 17 | + namespace AcmeBundle\Controller; |
| 18 | + |
| 19 | + use Symfony\Component\Console\Output\StreamOutput; |
| 20 | + use Symfony\Component\Console\Input\ArrayInput; |
| 21 | + use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 22 | + |
| 23 | + class GreetController |
| 24 | + { |
| 25 | + public function greetAction($name) |
| 26 | + { |
| 27 | + $kernel = $this->get('kernel'); |
| 28 | + $application = new Application($kernel); |
| 29 | + $application->setAutoExit(false); |
| 30 | + |
| 31 | + $input = new ArrayInput(array('command' => 'acme:greet')); |
| 32 | + $output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL); |
| 33 | + $application->run($input, $output); |
| 34 | + |
| 35 | + rewind($output->getStream()); |
| 36 | + $content = stream_get_contents($output->getStream()); |
| 37 | + fclose($output->getStream()); |
| 38 | + |
| 39 | + return $content; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | +Color code the Ansi content to HTML |
| 44 | +---------------------------------------- |
| 45 | + |
| 46 | +By telling the ``StreamOutput`` it is decorated via the third parameter, it will return the Ansi color-coded content. |
| 47 | +The [SensioLabs AnsiToHtml converter](https://github.com/sensiolabs/ansi-to-html) can be required using ``Composer`` |
| 48 | +and helps you to get colorful html:: |
| 49 | + |
| 50 | + // src/AcmeBundle/Controller/GreetController.php |
| 51 | + namespace AcmeBundle\Controller; |
| 52 | + |
| 53 | + use Symfony\Component\Console\Output\StreamOutput; |
| 54 | + use Symfony\Component\Console\Input\ArrayInput; |
| 55 | + use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 56 | + use SensioLabs\AnsiConverter\AnsiToHtmlConverter; |
| 57 | + |
| 58 | + class GreetController |
| 59 | + { |
| 60 | + public function greetAction($name) |
| 61 | + { |
| 62 | + $kernel = $this->get('kernel'); |
| 63 | + $application = new Application($kernel); |
| 64 | + $application->setAutoExit(false); |
| 65 | + |
| 66 | + $input = new ArrayInput(array('command' => 'acme:greet')); |
| 67 | + $output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL, true); |
| 68 | + $application->run($input, $output); |
| 69 | + |
| 70 | + rewind($output->getStream()); |
| 71 | + $content = stream_get_contents($output->getStream()); |
| 72 | + fclose($output->getStream()); |
| 73 | + |
| 74 | + $converter = new AnsiToHtmlConverter(); |
| 75 | + $content = $converter->convert($content); |
| 76 | + |
| 77 | + return $content; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +The ``AnsiToHtmlConverter`` can also be registered |
| 82 | +[as a Twig Extension](https://github.com/sensiolabs/ansi-to-html#twig-integration), and supports optional themes |
0 commit comments