Skip to content

Commit 59f8c95

Browse files
committed
Tweaks to the new using commands in a controller article
1 parent 41bf9d2 commit 59f8c95

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

cookbook/console/command_in_controller.rst

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ You may have the need to execute some function that is only available in a
1212
console command. Usually, you should refactor the command and move some logic
1313
into a service that can be reused in the controller. However, when the command
1414
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.
1616

1717
.. caution::
1818

1919
In comparison with a direct call from the console, calling a command from
2020
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.
2222

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::
2626

2727
// src/AppBundle/Controller/SpoolController.php
2828
namespace AppBundle\Controller;
2929

3030
use Symfony\Bundle\FrameworkBundle\Console\Application;
3131
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
3232
use Symfony\Component\Console\Input\ArrayInput;
33-
use Symfony\Component\Console\Output\StreamOutput;
33+
use Symfony\Component\Console\Output\BufferedOutput;
3434

3535
class SpoolController extends Controller
3636
{
@@ -44,38 +44,56 @@ allows you to directly execute a registered command inside your controller::
4444
'command' => 'swiftmailer:spool:send',
4545
'--message-limit' => $messages,
4646
));
47-
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL);
47+
// our use NullOutput() if you don't need the outpu
48+
$output = new BufferedOutput();
4849
$application->run($input, $output);
4950

50-
rewind($output->getStream());
51-
$content = stream_get_contents($output->getStream());
52-
fclose($output->getStream());
51+
// return the output
52+
$content = $output->fetch();
5353

54-
return $content;
54+
return new Response($content);
5555
}
5656
}
5757

5858
Showing Colorized Command Output
5959
--------------------------------
6060

61-
By telling the ``StreamOutput`` it is decorated via the third parameter,
61+
By telling the ``BufferedOutput`` it is decorated via the second parameter,
6262
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::
6472

6573
// src/AppBundle/Controller/SpoolController.php
6674
namespace AppBundle\Controller;
6775

6876
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
77+
use Symfony\Component\Console\Output\BufferedOutput;
78+
use Symfony\Component\Console\Output\OutputInterface;
6979
// ...
7080

7181
class SpoolController extends Controller
7282
{
7383
public function sendSpoolAction($messages = 10)
7484
{
7585
// ...
86+
$output = new BufferedOutput(
87+
OutputInterface::VERBOSITY_NORMAL,
88+
true // true for decorated
89+
);
90+
// ...
7691

92+
// return the output
7793
$converter = new AnsiToHtmlConverter();
78-
return $converter->convert($content);
94+
$content = $output->fetch();
95+
96+
return new Response($converter->convert($content));
7997
}
8098
}
8199

0 commit comments

Comments
 (0)