Skip to content

Commit d7722e8

Browse files
committed
Documentation about using Command in Controller
... and color coding Ansi output using SensioLabs AnsiToHtml Documentation about using Command in Controller ... and color coding Ansi output using SensioLabs AnsiToHtml Line syntax Change component link Update command_in_controller.rst with real world example about Swift Mailer. Update command_in_controller.rst Added command_in_controller to map.rst.inc Update map.rst.inc
1 parent d3192a7 commit d7722e8

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

cookbook/console/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Console
66

77
console_command
88
usage
9+
command_in_controller
910
sending_emails
1011
logging
1112
commands_as_services

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
* :doc:`/cookbook/console/console_command`
4444
* :doc:`/cookbook/console/usage`
45+
* :doc:`/cookbook/console/command_in_controller`
4546
* :doc:`/cookbook/console/sending_emails`
4647
* :doc:`/cookbook/console/logging`
4748
* :doc:`/cookbook/console/commands_as_services`

0 commit comments

Comments
 (0)