-
-
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
Command controller tweaks to #5062 #5299
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -12,25 +12,26 @@ 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. | ||
their code. Instead, you can execute the command directly. | ||
|
||
.. 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. | ||
overhead. | ||
|
||
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:: | ||
Imagine you want to send spooled Swift Mailer messages by | ||
:doc:`using the swiftmailer:spool:send command </cookbook/email/spool>`. | ||
Run this command from inside your controller via:: | ||
|
||
// 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; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class SpoolController extends Controller | ||
{ | ||
|
@@ -44,38 +45,57 @@ allows you to directly execute a registered command inside your controller:: | |
'command' => 'swiftmailer:spool:send', | ||
'--message-limit' => $messages, | ||
)); | ||
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL); | ||
// our use NullOutput() if you don't need the outpu | ||
$output = new BufferedOutput(); | ||
$application->run($input, $output); | ||
|
||
rewind($output->getStream()); | ||
$content = stream_get_contents($output->getStream()); | ||
fclose($output->getStream()); | ||
// return the output | ||
$content = $output->fetch(); | ||
|
||
return $content; | ||
return new Response($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. missing use statement for response |
||
} | ||
} | ||
|
||
Showing Colorized Command Output | ||
-------------------------------- | ||
|
||
By telling the ``StreamOutput`` it is decorated via the third parameter, | ||
By telling the ``BufferedOutput`` it is decorated via the second parameter, | ||
it will return the Ansi color-coded content. The `SensioLabs AnsiToHtml converter`_ | ||
can be required using ``Composer`` and helps you getting colorful HTML:: | ||
can be used to convert this to colorful HTML. | ||
|
||
First, require the package: | ||
|
||
.. code-block:: bash | ||
|
||
$ composer require sensiolabs/ansi-to-html | ||
|
||
Now, use it in your controller:: | ||
|
||
// src/AppBundle/Controller/SpoolController.php | ||
namespace AppBundle\Controller; | ||
|
||
use SensioLabs\AnsiConverter\AnsiToHtmlConverter; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
// ... | ||
|
||
class SpoolController extends Controller | ||
{ | ||
public function sendSpoolAction($messages = 10) | ||
{ | ||
// ... | ||
$output = new BufferedOutput( | ||
OutputInterface::VERBOSITY_NORMAL, | ||
true // true for decorated | ||
); | ||
// ... | ||
|
||
// return the output | ||
$converter = new AnsiToHtmlConverter(); | ||
return $converter->convert($content); | ||
$content = $output->fetch(); | ||
|
||
return new Response($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. missing use statement for response |
||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this is wrong. BufferedOutput does not exist in 2.3. It is a 2.4+ feature