title: How to call Symfony2 console command within controller and have streamed output?
body: <p>So I have a basic requirement: I need to call a Symfony2's custom console command from a controller (the script is also called by a CRON job but I want it to be callable from a web-browser).</p>
  <p>I followed <a href="http://symfony.com/doc/current/cookbook/console/command_in_controller.html" rel="nofollow noreferrer">this tutorial</a> to make it work and here it is:</p>
  <pre><code>&lt;?php
  
  namespace AppBundle\Controller\Admin;
  
  use Symfony\Bundle\FrameworkBundle\Console\Application;
  use Symfony\Component\Console\Input\ArrayInput;
  use Symfony\Component\Console\Output\BufferedOutput;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  
  class TacheController extends Controller
  {
      /**
       * @Route(&quot;/admin/taches/facebook&quot;, name=&quot;admin_tache_facebook&quot;)
       *
       * @return Response
       */
      public function facebookAction(Request $request)
      {
          ini_set('max_execution_time', -1);
          $kernel = $this-&gt;get('kernel');
          $application = new Application($kernel);
          $application-&gt;setAutoExit(false);
  
          $input = new ArrayInput(array(
              'command' =&gt; 'nrv:fetch:facebook',
          ));
          // You can use NullOutput() if you don't need the output
          $output = new BufferedOutput();
          $application-&gt;run($input, $output);
  
          // return the output, don't use if you used NullOutput()
          $content = $output-&gt;fetch();
  
          // return new Response(&quot;&quot;), if you used NullOutput()
          return new Response($content);
      }
  }
  </code></pre>
  <p>However, the console command is quite long to run (~2mn) and therefore, the page hangs for that time until it displays all the output from the command.</p>
  <p>My goal is to have the output as it is displayed in the console like when using the web console with <code>ConsoleBundle</code>. I thought of the use of <code>ob_start()</code> and <code>ob_end_flush()</code> but I don't know how to use them in this context.</p>
  <p>Is what I'm trying to achieve even possible? How can I make it so?</p>
  <hr />
  <h2>Solution</h2>
  <p>As per <a href="https://stackoverflow.com/a/37423416/2788131">the answer provided by @MichałSznurawa</a>, I had to extend <code>\Symfony\Component\Console\Output\BufferedOutput</code> and implements <code>doWrite()</code> method. Here it is:</p>
  <pre><code>&lt;?php
  
  namespace AppBundle\Console;
  
  use Symfony\Component\Console\Output\BufferedOutput;
  use Symfony\Component\HttpFoundation\StreamedResponse;
  
  class StreamedOutput extends BufferedOutput
  {
      public function doWrite($message, $newline)
      {
          $response = new StreamedResponse();
          $response-&gt;setCallback(function() use($message) {
              echo $message;
              flush();
          });
  
          $response-&gt;send();
      }
  }
  </code></pre>
  <p>And amend the controller as per the following:</p>
  <pre><code>$output = new StreamedOutput();
  </code></pre>
  <p>The result is the page streaming the output of the command as soon as it get executed (instead of waiting for it to finish).</p>