Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/infinite looping timeout issue #325 #343

Merged
merged 3 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.idea
.php_cs.cache
.phpunit.result.cache
build
composer.lock
docs/_site
vendor
/nbproject/private/
.php_cs.cache
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ 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.
- Internal improve `HTMLConverter`.

### Removed

Expand Down
3 changes: 3 additions & 0 deletions phpstan.src.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
parameters:
ignoreErrors:
reportUnmatchedIgnoredErrors: false
1 change: 1 addition & 0 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/HTMLConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace League\Csv;

use DOMDocument;
use DOMElement;
use DOMException;
use Traversable;
use function preg_match;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,[email protected]\njane,doe,[email protected]\n";
$csv = Reader::createFromString($raw_csv);
iterator_to_array($csv->getRecords());
self::assertSame($raw_csv, $csv->getContent());
}

/**
* @covers ::__toString
* @covers ::getContent
Expand Down