@@ -12,25 +12,26 @@ You may have the need to execute some function that is only available in a
12
12
console command. Usually, you should refactor the command and move some logic
13
13
into a service that can be reused in the controller. However, when the command
14
14
is part of a third-party library, you wouldn't want to modify or duplicate
15
- their code, but want to directly execute the command instead .
15
+ their code. Instead, you can execute the command directly .
16
16
17
17
.. caution ::
18
18
19
19
In comparison with a direct call from the console, calling a command from
20
20
a controller has a slight performance impact because of the request stack
21
- overhead. This way of calling a command is only useful for small tasks.
21
+ overhead.
22
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::
23
+ Imagine you want to send spooled Swift Mailer messages by
24
+ :doc: `using the swiftmailer:spool:send command </cookbook/email/spool >`.
25
+ Run this command from inside your controller via ::
26
26
27
27
// src/AppBundle/Controller/SpoolController.php
28
28
namespace AppBundle\Controller;
29
29
30
30
use Symfony\Bundle\FrameworkBundle\Console\Application;
31
31
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
32
32
use Symfony\Component\Console\Input\ArrayInput;
33
- use Symfony\Component\Console\Output\StreamOutput;
33
+ use Symfony\Component\Console\Output\BufferedOutput;
34
+ use Symfony\Component\HttpFoundation\Response;
34
35
35
36
class SpoolController extends Controller
36
37
{
@@ -44,38 +45,57 @@ allows you to directly execute a registered command inside your controller::
44
45
'command' => 'swiftmailer:spool:send',
45
46
'--message-limit' => $messages,
46
47
));
47
- $output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL);
48
+ // our use NullOutput() if you don't need the outpu
49
+ $output = new BufferedOutput();
48
50
$application->run($input, $output);
49
51
50
- rewind($output->getStream());
51
- $content = stream_get_contents($output->getStream());
52
- fclose($output->getStream());
52
+ // return the output
53
+ $content = $output->fetch();
53
54
54
- return $content;
55
+ return new Response( $content) ;
55
56
}
56
57
}
57
58
58
59
Showing Colorized Command Output
59
60
--------------------------------
60
61
61
- By telling the ``StreamOutput `` it is decorated via the third parameter,
62
+ By telling the ``BufferedOutput `` it is decorated via the second parameter,
62
63
it will return the Ansi color-coded content. The `SensioLabs AnsiToHtml converter `_
63
- can be required using ``Composer `` and helps you getting colorful HTML::
64
+ can be used to convert this to colorful HTML.
65
+
66
+ First, require the package:
67
+
68
+ .. code-block :: bash
69
+
70
+ $ composer require sensiolabs/ansi-to-html
71
+
72
+ Now, use it in your controller::
64
73
65
74
// src/AppBundle/Controller/SpoolController.php
66
75
namespace AppBundle\Controller;
67
76
68
77
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
78
+ use Symfony\Component\Console\Output\BufferedOutput;
79
+ use Symfony\Component\Console\Output\OutputInterface;
80
+ use Symfony\Component\HttpFoundation\Response;
69
81
// ...
70
82
71
83
class SpoolController extends Controller
72
84
{
73
85
public function sendSpoolAction($messages = 10)
74
86
{
75
87
// ...
88
+ $output = new BufferedOutput(
89
+ OutputInterface::VERBOSITY_NORMAL,
90
+ true // true for decorated
91
+ );
92
+ // ...
76
93
94
+ // return the output
77
95
$converter = new AnsiToHtmlConverter();
78
- return $converter->convert($content);
96
+ $content = $output->fetch();
97
+
98
+ return new Response($converter->convert($content));
79
99
}
80
100
}
81
101
0 commit comments