Skip to content

Commit

Permalink
Merge pull request #343 from thephpleague/bugfix/infinite-looping-tim…
Browse files Browse the repository at this point in the history
…eout

Bugfix/infinite looping timeout issue #325
  • Loading branch information
nyamsprod authored Jun 7, 2019
2 parents 31c093c + 45b0349 commit b574a7d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
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

0 comments on commit b574a7d

Please sign in to comment.