Skip to content

Commit 2d5f879

Browse files
committed
feature #5062 Cookbook about Command in Application with AnsiToHtml (Rvanlaak)
This PR was submitted for the 2.7 branch but it was merged into the 2.3 branch instead (closes #5062). Discussion ---------- Cookbook about Command in Application with AnsiToHtml | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | >= 2.3 | Fixed tickets | symfony/symfony#11392 Commits ------- 0799366 Documentation about using Command in Controller
2 parents cd025f8 + 0799366 commit 2d5f879

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 extends 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 getting colorful HTML::
64+
65+
// src/AppBundle/Controller/SpoolController.php
66+
namespace AppBundle\Controller;
67+
68+
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
69+
// ...
70+
71+
class SpoolController extends 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

cookbook/console/index.rst

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

77
console_command
88
usage
9+
command_in_controller
910
sending_emails
1011
logging

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

0 commit comments

Comments
 (0)