Skip to content

Commit 3a5d4a8

Browse files
ro0NLfabpot
authored andcommitted
[Console] Improve Table performance
1 parent 7be416f commit 3a5d4a8

File tree

2 files changed

+71
-34
lines changed

2 files changed

+71
-34
lines changed

Helper/Table.php

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -275,29 +275,40 @@ public function setRow($column, array $row)
275275
*/
276276
public function render()
277277
{
278-
$this->calculateNumberOfColumns();
279-
$rows = $this->buildTableRows($this->rows);
280-
$headers = $this->buildTableRows($this->headers);
278+
$rows = array_merge($this->headers, array($divider = new TableSeparator()), $this->rows);
279+
$this->calculateNumberOfColumns($rows);
281280

282-
$this->calculateColumnsWidth(array_merge($headers, $rows));
281+
$rows = $this->buildTableRows($rows);
282+
$this->calculateColumnsWidth($rows);
283283

284-
$this->renderRowSeparator();
285-
if (!empty($headers)) {
286-
foreach ($headers as $header) {
287-
$this->renderRow($header, $this->style->getCellHeaderFormat());
288-
$this->renderRowSeparator();
289-
}
290-
}
284+
$isHeader = true;
285+
$isFirstRow = false;
291286
foreach ($rows as $row) {
287+
if ($divider === $row) {
288+
$isHeader = false;
289+
$isFirstRow = true;
290+
291+
continue;
292+
}
292293
if ($row instanceof TableSeparator) {
293294
$this->renderRowSeparator();
294-
} else {
295-
$this->renderRow($row, $this->style->getCellRowFormat());
295+
296+
continue;
296297
}
298+
if (!$row) {
299+
continue;
300+
}
301+
302+
if ($isHeader || $isFirstRow) {
303+
$this->renderRowSeparator();
304+
if ($isFirstRow) {
305+
$isFirstRow = false;
306+
}
307+
}
308+
309+
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
297310
}
298-
if (!empty($rows)) {
299-
$this->renderRowSeparator();
300-
}
311+
$this->renderRowSeparator();
301312

302313
$this->cleanup();
303314
}
@@ -340,10 +351,6 @@ private function renderColumnSeparator()
340351
*/
341352
private function renderRow(array $row, string $cellFormat)
342353
{
343-
if (empty($row)) {
344-
return;
345-
}
346-
347354
$rowContent = $this->renderColumnSeparator();
348355
foreach ($this->getRowColumns($row) as $column) {
349356
$rowContent .= $this->renderCell($row, $column, $cellFormat);
@@ -386,14 +393,10 @@ private function renderCell(array $row, int $column, string $cellFormat)
386393
/**
387394
* Calculate number of columns for this table.
388395
*/
389-
private function calculateNumberOfColumns()
396+
private function calculateNumberOfColumns($rows)
390397
{
391-
if (null !== $this->numberOfColumns) {
392-
return;
393-
}
394-
395398
$columns = array(0);
396-
foreach (array_merge($this->headers, $this->rows) as $row) {
399+
foreach ($rows as $row) {
397400
if ($row instanceof TableSeparator) {
398401
continue;
399402
}
@@ -429,15 +432,17 @@ private function buildTableRows($rows)
429432
}
430433
}
431434

432-
$tableRows = array();
433-
foreach ($rows as $rowKey => $row) {
434-
$tableRows[] = $this->fillCells($row);
435-
if (isset($unmergedRows[$rowKey])) {
436-
$tableRows = array_merge($tableRows, $unmergedRows[$rowKey]);
437-
}
438-
}
435+
return new TableRows(function () use ($rows, $unmergedRows) {
436+
foreach ($rows as $rowKey => $row) {
437+
yield $this->fillCells($row);
439438

440-
return $tableRows;
439+
if (isset($unmergedRows[$rowKey])) {
440+
foreach ($unmergedRows[$rowKey] as $row) {
441+
yield $row;
442+
}
443+
}
444+
}
445+
});
441446
}
442447

443448
/**

Helper/TableRows.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Helper;
13+
14+
/**
15+
* @internal
16+
*/
17+
class TableRows implements \IteratorAggregate
18+
{
19+
private $generator;
20+
21+
public function __construct(callable $generator)
22+
{
23+
$this->generator = $generator;
24+
}
25+
26+
public function getIterator()
27+
{
28+
$g = $this->generator;
29+
30+
return $g();
31+
}
32+
}

0 commit comments

Comments
 (0)