From e046b27deabf0d7bb3b0ec5c40833eeea053c208 Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Tue, 4 Jun 2019 22:03:48 +0200 Subject: [PATCH 1/3] Remove CSV flags before outputting the file content #325 --- CHANGELOG.md | 2 +- src/AbstractCsv.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e41bc898..bf4b03d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ All Notable changes to `Csv` will be documented in this file ### Fixed -- Nothing +- `AbstractCSV::chunk` see [#325](https://github.com/thephpleague/csv/pull/325) remove CSV flags from the Stream class to avoid infinite loop. ### Removed diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 1eae1d73..05f58844 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -311,6 +311,7 @@ public function output(string $filename = null): int } $input_bom = $this->getInputBOM(); $this->document->rewind(); + $this->document->setFlags(0); $this->document->fseek(strlen($input_bom)); echo $this->output_bom; From 9fa6a86a59fbdf9b14e685943e0f95b7eb51e34c Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Tue, 4 Jun 2019 22:04:35 +0200 Subject: [PATCH 2/3] Improve HTMLConverter internal code --- .gitignore | 3 ++- CHANGELOG.md | 1 + phpstan.src.neon | 3 +++ src/HTMLConverter.php | 12 +++++++++--- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index a73932c0..42894e5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ .idea +.php_cs.cache +.phpunit.result.cache build composer.lock docs/_site vendor /nbproject/private/ -.php_cs.cache diff --git a/CHANGELOG.md b/CHANGELOG.md index bf4b03d1..e78a4b2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All Notable changes to `Csv` will be documented in this file ### Fixed - `AbstractCSV::chunk` see [#325](https://github.com/thephpleague/csv/pull/325) remove CSV flags from the Stream class to avoid infinite loop. +- Internal improve `HTMLConverter`. ### Removed diff --git a/phpstan.src.neon b/phpstan.src.neon index 96d0543a..87cca5c6 100644 --- a/phpstan.src.neon +++ b/phpstan.src.neon @@ -1,2 +1,5 @@ includes: - vendor/phpstan/phpstan-strict-rules/rules.neon +parameters: + ignoreErrors: + reportUnmatchedIgnoredErrors: false \ No newline at end of file diff --git a/src/HTMLConverter.php b/src/HTMLConverter.php index 4490919f..27b7c644 100644 --- a/src/HTMLConverter.php +++ b/src/HTMLConverter.php @@ -13,6 +13,8 @@ namespace League\Csv; +use DOMDocument; +use DOMElement; use DOMException; use Traversable; use function preg_match; @@ -60,11 +62,15 @@ public function __construct() */ public function convert($records): string { + /** @var DOMDocument $doc */ $doc = $this->xml_converter->convert($records); - $doc->documentElement->setAttribute('class', $this->class_name); - $doc->documentElement->setAttribute('id', $this->id_value); - return $doc->saveHTML($doc->documentElement); + /** @var DOMElement $table */ + $table = $doc->getElementsByTagName('table')->item(0); + $table->setAttribute('class', $this->class_name); + $table->setAttribute('id', $this->id_value); + + return $doc->saveHTML($table); } /** From 45b034972fba74384edbdbe8e0506456a096ac90 Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Thu, 6 Jun 2019 22:32:16 +0200 Subject: [PATCH 3/3] Patch moved to the AbstractCsv::chunk method --- src/AbstractCsv.php | 2 +- tests/CsvTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 05f58844..e7e33520 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -262,6 +262,7 @@ public function chunk(int $length): Generator $input_bom = $this->getInputBOM(); $this->document->rewind(); + $this->document->setFlags(0); $this->document->fseek(strlen($input_bom)); foreach (str_split($this->output_bom.$this->document->fread($length), $length) as $chunk) { yield $chunk; @@ -311,7 +312,6 @@ public function output(string $filename = null): int } $input_bom = $this->getInputBOM(); $this->document->rewind(); - $this->document->setFlags(0); $this->document->fseek(strlen($input_bom)); echo $this->output_bom; diff --git a/tests/CsvTest.php b/tests/CsvTest.php index 420aba49..a0fa3a3b 100644 --- a/tests/CsvTest.php +++ b/tests/CsvTest.php @@ -172,6 +172,18 @@ public function testOutputHeaders() self::assertContains('Content-Disposition: attachment; filename="tst.csv"; filename*=utf-8\'\'t%C3%A9st.csv', $headers[3]); } + /** + * @covers ::chunk + * @covers ::getContent + */ + public function testChunkDoesNotTimeoutAfterReading() + { + $raw_csv = "john,doe,john.doe@example.com\njane,doe,jane.doe@example.com\n"; + $csv = Reader::createFromString($raw_csv); + iterator_to_array($csv->getRecords()); + self::assertSame($raw_csv, $csv->getContent()); + } + /** * @covers ::__toString * @covers ::getContent