-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation about using Command in Controller
... and color coding Ansi output using SensioLabs AnsiToHtml Documentation about using Command in Controller ... and color coding Ansi output using SensioLabs AnsiToHtml Line syntax Change component link Update command_in_controller.rst with real world example about Swift Mailer. Update command_in_controller.rst Added command_in_controller to map.rst.inc Update map.rst.inc Documentation about using Command in Controller Update command_in_controller.rst
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
.. index:: | ||
single: Console; Use commands in your 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 in your application. | ||
|
||
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. | ||
|
||
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/AcmeBundle/Controller/SpoolController.php | ||
namespace AcmeBundle\Controller; | ||
|
||
use Symfony\Component\Console\Output\StreamOutput; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
|
||
class SpoolController | ||
{ | ||
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; | ||
} | ||
} | ||
|
||
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 to get colorful html:: | ||
|
||
// src/AppBundle/Controller/SpoolController.php | ||
namespace AppBundle\Controller; | ||
|
||
use Symfony\Component\Console\Output\StreamOutput; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use SensioLabs\AnsiConverter\AnsiToHtmlConverter; | ||
|
||
class SpoolController | ||
{ | ||
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, true); | ||
$application->run($input, $output); | ||
|
||
rewind($output->getStream()); | ||
$content = stream_get_contents($output->getStream()); | ||
fclose($output->getStream()); | ||
|
||
$converter = new AnsiToHtmlConverter(); | ||
return $converter->convert($content); | ||
} | ||
} | ||
|
||
The ``AnsiToHtmlConverter`` can also be registered `as a Twig Extension`_, | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ Console | |
|
||
console_command | ||
usage | ||
command_in_controller | ||
sending_emails | ||
logging | ||
commands_as_services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters