Skip to content

Commit

Permalink
Tweaks to the new using commands in a controller article
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed May 23, 2015
1 parent 41bf9d2 commit 59f8c95
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions cookbook/console/command_in_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ 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;

class SpoolController extends Controller
{
Expand All @@ -44,38 +44,56 @@ 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);
}
}

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;
// ...

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));
}
}

Expand Down

0 comments on commit 59f8c95

Please sign in to comment.