-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cookbook about Command in Application with AnsiToHtml #5062
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
.. index:: | ||
single: Console; How to Call a Command from a controller | ||
|
||
How to Call a Command from a Controller | ||
======================================= | ||
|
||
The :doc:`Console component documentation </components/console/introduction>` covers | ||
how to create a console command. This cookbook article covers how to use a console command | ||
directly from your controller. | ||
|
||
You may have the need to execute some function that is only available in a console command. | ||
Usually, you should refactor the command and move some logic into a service that can be | ||
reused in the controller. However, when the command is part of a third-party library, you | ||
wouldn't want to modify or duplicate their code, but want to directly execute the command | ||
instead. | ||
|
||
.. caution:: | ||
|
||
In comparison with a direct call from the console, calling a command from a controller | ||
has a slight performance impact because of the request stack overhead. This way of | ||
calling a command is only useful for small tasks. | ||
|
||
An example of this is sending the emails that Swift Mailer spooled earlier | ||
:doc:`using the swiftmailer:spool:send command </cookbook/email/spool>`. Symfony | ||
allows you to directly execute a registered command inside your controller:: | ||
|
||
// src/AppBundle/Controller/SpoolController.php | ||
namespace AppBundle\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Output\StreamOutput; | ||
|
||
class SpoolController extends Controller | ||
{ | ||
public function sendSpoolAction($messages = 10) | ||
{ | ||
$kernel = $this->get('kernel'); | ||
$application = new Application($kernel); | ||
$application->setAutoExit(false); | ||
|
||
$input = new ArrayInput(array( | ||
'command' => 'swiftmailer:spool:send', | ||
'--message-limit' => $messages, | ||
)); | ||
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL); | ||
$application->run($input, $output); | ||
|
||
rewind($output->getStream()); | ||
$content = stream_get_contents($output->getStream()); | ||
fclose($output->getStream()); | ||
|
||
return $content; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this is wrong no? why are you not returning a valid HttpResponse instead? |
||
} | ||
} | ||
|
||
Showing Colorized Command Output | ||
-------------------------------- | ||
|
||
By telling the ``StreamOutput`` it is decorated via the third parameter, it will return | ||
the Ansi color-coded content. The `SensioLabs AnsiToHtml converter`_ can be required | ||
using ``Composer`` and helps you getting colorful HTML:: | ||
|
||
// src/AppBundle/Controller/SpoolController.php | ||
namespace AppBundle\Controller; | ||
|
||
use SensioLabs\AnsiConverter\AnsiToHtmlConverter; | ||
// ... | ||
|
||
class SpoolController extends Controller | ||
{ | ||
public function sendSpoolAction($messages = 10) | ||
{ | ||
// ... | ||
|
||
$converter = new AnsiToHtmlConverter(); | ||
return $converter->convert($content); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if it is a pretty html colored thing, i believe you want this into a Response object right? or am i wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. My first version didn't even return anything, because the Response is quite user-specific. What about changing it to
That way the developer knows it has to do something with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good, and you can do // ... as @javiereguiluz is saying. That rather takes out of the scope this action |
||
} | ||
} | ||
|
||
The ``AnsiToHtmlConverter`` can also be registered `as a Twig Extension`_, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps mention the name of the class that implements this extension? In particular https://github.com/sensiolabs/ansi-to-html/blob/master/SensioLabs/AnsiConverter/Bridge/Twig/AnsiExtension.php |
||
and supports optional themes. | ||
|
||
.. _`SensioLabs AnsiToHtml converter`: https://github.com/sensiolabs/ansi-to-html | ||
.. _`as a Twig Extension`: https://github.com/sensiolabs/ansi-to-html#twig-integration |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ Console | |
|
||
console_command | ||
usage | ||
command_in_controller | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to also add a reference to the document in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 👍 |
||
sending_emails | ||
logging | ||
commands_as_services |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this before the other
use
statements to order them alphabetically?