Skip to content

Commit b574a7d

Browse files
authored
Merge pull request #343 from thephpleague/bugfix/infinite-looping-timeout
Bugfix/infinite looping timeout issue #325
2 parents 31c093c + 45b0349 commit b574a7d

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.idea
2+
.php_cs.cache
3+
.phpunit.result.cache
24
build
35
composer.lock
46
docs/_site
57
vendor
68
/nbproject/private/
7-
.php_cs.cache

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ All Notable changes to `Csv` will be documented in this file
1414

1515
### Fixed
1616

17-
- Nothing
17+
- `AbstractCSV::chunk` see [#325](https://github.com/thephpleague/csv/pull/325) remove CSV flags from the Stream class to avoid infinite loop.
18+
- Internal improve `HTMLConverter`.
1819

1920
### Removed
2021

phpstan.src.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
includes:
22
- vendor/phpstan/phpstan-strict-rules/rules.neon
3+
parameters:
4+
ignoreErrors:
5+
reportUnmatchedIgnoredErrors: false

src/AbstractCsv.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ public function chunk(int $length): Generator
262262

263263
$input_bom = $this->getInputBOM();
264264
$this->document->rewind();
265+
$this->document->setFlags(0);
265266
$this->document->fseek(strlen($input_bom));
266267
foreach (str_split($this->output_bom.$this->document->fread($length), $length) as $chunk) {
267268
yield $chunk;

src/HTMLConverter.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace League\Csv;
1515

16+
use DOMDocument;
17+
use DOMElement;
1618
use DOMException;
1719
use Traversable;
1820
use function preg_match;
@@ -60,11 +62,15 @@ public function __construct()
6062
*/
6163
public function convert($records): string
6264
{
65+
/** @var DOMDocument $doc */
6366
$doc = $this->xml_converter->convert($records);
64-
$doc->documentElement->setAttribute('class', $this->class_name);
65-
$doc->documentElement->setAttribute('id', $this->id_value);
6667

67-
return $doc->saveHTML($doc->documentElement);
68+
/** @var DOMElement $table */
69+
$table = $doc->getElementsByTagName('table')->item(0);
70+
$table->setAttribute('class', $this->class_name);
71+
$table->setAttribute('id', $this->id_value);
72+
73+
return $doc->saveHTML($table);
6874
}
6975

7076
/**

tests/CsvTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ public function testOutputHeaders()
172172
self::assertContains('Content-Disposition: attachment; filename="tst.csv"; filename*=utf-8\'\'t%C3%A9st.csv', $headers[3]);
173173
}
174174

175+
/**
176+
* @covers ::chunk
177+
* @covers ::getContent
178+
*/
179+
public function testChunkDoesNotTimeoutAfterReading()
180+
{
181+
$raw_csv = "john,doe,john.doe@example.com\njane,doe,jane.doe@example.com\n";
182+
$csv = Reader::createFromString($raw_csv);
183+
iterator_to_array($csv->getRecords());
184+
self::assertSame($raw_csv, $csv->getContent());
185+
}
186+
175187
/**
176188
* @covers ::__toString
177189
* @covers ::getContent

0 commit comments

Comments
 (0)