Monitoring: make use of Buffer while serving CSV

refs #13623
This commit is contained in:
Alexander A. Klimov 2017-10-20 11:06:12 +02:00
parent 2f46929ac7
commit 78ba3dc4d4
2 changed files with 52 additions and 16 deletions

View File

@ -3,12 +3,20 @@
namespace Icinga\File;
use Icinga\Util\Buffer;
use Traversable;
class Csv
{
protected $query;
/**
* Cache for {@link render()}
*
* @var Buffer|null
*/
protected $renderBuffer;
protected function __construct()
{
}
@ -22,26 +30,49 @@ class Csv
public function dump()
{
header('Content-type: text/csv');
echo (string) $this;
$this->render()->fpassthru();
}
public function __toString()
{
$first = true;
$csv = '';
foreach ($this->query as $row) {
if ($first) {
$csv .= implode(',', array_keys((array) $row)) . "\r\n";
$first = false;
return (string) $this->render();
}
/**
* Return the rendered CSV
*
* @return Buffer
*/
protected function render()
{
if ($this->renderBuffer === null) {
$this->renderBuffer = new Buffer();
$first = true;
foreach ($this->query as $row) {
if ($first) {
$this->renderBuffer->append($this->renderRow(array_keys((array)$row)));
$first = false;
}
$this->renderBuffer->append($this->renderRow(array_values((array)$row)));
}
$out = array();
foreach ($row as & $val) {
$out[] = '"' . $val . '"';
}
$csv .= implode(',', $out) . "\r\n";
}
return $csv;
return $this->renderBuffer;
}
/**
* Return a single CSV row string representing the given columns
*
* @param array $columns
*
* @return string
*/
protected function renderRow(array $columns)
{
$quoted = array();
foreach ($columns as $column) {
$quoted[] = '"' . str_replace('"', '""', $column) . '"';
}
return implode(',', $quoted) . "\r\n";
}
}

View File

@ -72,8 +72,13 @@ class Controller extends IcingaWebController
'Content-Disposition',
'attachment; filename=' . $this->getRequest()->getActionName() . '.csv'
)
->appendBody((string) Csv::fromQuery($query))
->sendResponse();
->sendHeaders();
while (ob_get_level()) {
ob_end_clean();
}
Csv::fromQuery($query)->dump();
exit;
}
}