Skip to content

Commit 69c94d8

Browse files
committed
bug #5299 Command controller tweaks to #5062 (weaverryan)
This PR was merged into the 2.3 branch. Discussion ---------- Command controller tweaks to #5062 | Q | A | ------------- | --- | Doc fix? | yes #5062 | New docs? | no | Applies to | 2.3+ | Fixed tickets | #5062 Hi guys! This is a proofread and bug fix after merging #5062. Notably, I changed to use the `BufferedOutput` and returned a Response from the controller instead of the string. Please let me know if you see any errors - I was coding this right inside the docs 😇. Thanks! Commits ------- aad7277 Fixing things thanks to Wouter 59f8c95 Tweaks to the new using commands in a controller article
2 parents f712586 + aad7277 commit 69c94d8

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

cookbook/console/command_in_controller.rst

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,26 @@ 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;
34+
use Symfony\Component\HttpFoundation\Response;
3435

3536
class SpoolController extends Controller
3637
{
@@ -44,38 +45,57 @@ allows you to directly execute a registered command inside your controller::
4445
'command' => 'swiftmailer:spool:send',
4546
'--message-limit' => $messages,
4647
));
47-
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL);
48+
// our use NullOutput() if you don't need the outpu
49+
$output = new BufferedOutput();
4850
$application->run($input, $output);
4951

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

54-
return $content;
55+
return new Response($content);
5556
}
5657
}
5758

5859
Showing Colorized Command Output
5960
--------------------------------
6061

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

6574
// src/AppBundle/Controller/SpoolController.php
6675
namespace AppBundle\Controller;
6776

6877
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
78+
use Symfony\Component\Console\Output\BufferedOutput;
79+
use Symfony\Component\Console\Output\OutputInterface;
80+
use Symfony\Component\HttpFoundation\Response;
6981
// ...
7082

7183
class SpoolController extends Controller
7284
{
7385
public function sendSpoolAction($messages = 10)
7486
{
7587
// ...
88+
$output = new BufferedOutput(
89+
OutputInterface::VERBOSITY_NORMAL,
90+
true // true for decorated
91+
);
92+
// ...
7693

94+
// return the output
7795
$converter = new AnsiToHtmlConverter();
78-
return $converter->convert($content);
96+
$content = $output->fetch();
97+
98+
return new Response($converter->convert($content));
7999
}
80100
}
81101

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy