Skip to content
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 2 commits into from
May 23, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions cookbook/console/command_in_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

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

use Symfony\Component\HttpFoundation\Response;

class SpoolController extends Controller
{
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The 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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing use statement for response

}
}

Expand Down