@@ -12,25 +12,25 @@ 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
34
35
35
class SpoolController extends Controller
36
36
{
@@ -44,38 +44,56 @@ allows you to directly execute a registered command inside your controller::
44
44
'command' => 'swiftmailer:spool:send',
45
45
'--message-limit' => $messages,
46
46
));
47
- $output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL);
47
+ // our use NullOutput() if you don't need the outpu
48
+ $output = new BufferedOutput();
48
49
$application->run($input, $output);
49
50
50
- rewind($output->getStream());
51
- $content = stream_get_contents($output->getStream());
52
- fclose($output->getStream());
51
+ // return the output
52
+ $content = $output->fetch();
53
53
54
- return $content;
54
+ return new Response( $content) ;
55
55
}
56
56
}
57
57
58
58
Showing Colorized Command Output
59
59
--------------------------------
60
60
61
- By telling the ``StreamOutput `` it is decorated via the third parameter,
61
+ By telling the ``BufferedOutput `` it is decorated via the second parameter,
62
62
it will return the Ansi color-coded content. The `SensioLabs AnsiToHtml converter `_
63
- can be required using ``Composer `` and helps you getting colorful HTML::
63
+ can be used to convert this to colorful HTML.
64
+
65
+ First, require the package:
66
+
67
+ .. code-block :: bash
68
+
69
+ composer require sensiolabs/ansi-to-html
70
+
71
+ Now, use it in your controller::
64
72
65
73
// src/AppBundle/Controller/SpoolController.php
66
74
namespace AppBundle\Controller;
67
75
68
76
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
77
+ use Symfony\Component\Console\Output\BufferedOutput;
78
+ use Symfony\Component\Console\Output\OutputInterface;
69
79
// ...
70
80
71
81
class SpoolController extends Controller
72
82
{
73
83
public function sendSpoolAction($messages = 10)
74
84
{
75
85
// ...
86
+ $output = new BufferedOutput(
87
+ OutputInterface::VERBOSITY_NORMAL,
88
+ true // true for decorated
89
+ );
90
+ // ...
76
91
92
+ // return the output
77
93
$converter = new AnsiToHtmlConverter();
78
- return $converter->convert($content);
94
+ $content = $output->fetch();
95
+
96
+ return new Response($converter->convert($content));
79
97
}
80
98
}
81
99
0 commit comments