Skip to content

Instantly share code, notes, and snippets.

@ayalon
Last active December 29, 2015 15:19
Show Gist options
  • Save ayalon/7690311 to your computer and use it in GitHub Desktop.
Save ayalon/7690311 to your computer and use it in GitHub Desktop.
Streamed Output of Console Command Line per Line
<?php
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Command\Command;
class ExportController extends Controller
{
public function streamingAction(){
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
set_time_limit(0);
$application = new Application($this->get('kernel'));
$response = new StreamedResponse(function() use ($application) {
$name = 'ayalon:progress';
$params = array();
array_unshift($params, $name);
$application->setAutoExit(false);
$input = new ArrayInput($params);
$input->setInteractive(false);
$output = new StreamOutput(fopen('php://stdout', 'w'));
$application->run($input, $output);
});
return $response;
}
public function exportAction($store){
$name = 'ayalon:progress';
$params = array();
array_unshift($params, $name);
$application = new Application($this->get('kernel'));
$application->setAutoExit(false);
$input = new ArrayInput($params);
$input->setInteractive(false);
$fp = fopen('php://temp/maxmemory:5242880', 'r+');
$output = new StreamOutput($fp);
echo('start: ' . microtime() . '<br>');
$application->run($input, $output);
echo('run: ' . microtime(). '<br>');
$response = new StreamedResponse(function() use($fp, $output) {
while (!feof($fp)) {
echo('end: ' . microtime());
echo 'start';
//$line = stream_get_line($fp, 100000000, "\n");
$line = fgets($fp);
//$content = stream_get_contents($fp);
echo $line;
ob_flush();
flush();
//echo $content;
//print_r($fp);
echo 'end';
}
});
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment