Skip to content

[Finder] Fix iteration fails with non-rewindable streams #8120

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

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 8 additions & 1 deletion src/Symfony/Component/Finder/Iterator/FilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ public function rewind()
{
$iterator = $this;
while ($iterator instanceof \OuterIterator) {
if ($iterator->getInnerIterator() instanceof \FilesystemIterator) {
$innerIterator = $iterator->getInnerIterator();

if ($innerIterator instanceof RecursiveDirectoryIterator) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ($innerIterator instanceof RecursiveDirectoryIterator && $innerIterator->isRewindable()) { 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RecursiveDirectoryIterator is a \FilesystemIterator so your fix does not have the same behaviour.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a real "fix" it CS fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stloyd your suggestion is not a CS fix as the suggested code does not have the same behavior than the current code of the PR. Combining both conditions together changes what goes through the elseif

if ($innerIterator->isRewindable()) {
$innerIterator->next();
$innerIterator->rewind();
}
} elseif ($iterator->getInnerIterator() instanceof \FilesystemIterator) {
$iterator->getInnerIterator()->next();
$iterator->getInnerIterator()->rewind();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
{
/**
* @var Boolean
*/
private $rewindable;

public function __construct($path, $flags)
{
if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) {
Expand All @@ -38,4 +43,39 @@ public function current()
{
return new SplFileInfo(parent::current()->getPathname(), $this->getSubPath(), $this->getSubPathname());
}

/**
* Do nothing for non rewindable stream
*/
public function rewind()
{
if (false === $this->isRewindable()) {
return;
}

parent::rewind();
}

/**
* Checks if the stream is rewindable.
*
* @return Boolean true when the stream is rewindable, false otherwise
*/
public function isRewindable()
{
if (null !== $this->rewindable) {
return $this->rewindable;
}

if (false !== $stream = @opendir($this->getPath())) {
$infos = stream_get_meta_data($stream);
closedir($stream);

if ($infos['seekable']) {
return $this->rewindable = true;
}
}

return $this->rewindable = false;
}
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,21 @@ public function testMultipleLocations()

$this->assertEquals(1, count($finder));
}

public function testNonSeekableStream()
{
try {
$i = Finder::create()->in('ftp://ftp.mozilla.org/')->depth(0)->getIterator();
} catch (\UnexpectedValueException $e) {
$this->markTestSkipped(sprintf('Unsupported stream "%s".', 'ftp'));
}

$contains = array(
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'README',
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'index.html',
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'pub',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the iterator really return ftp://ftp.mozilla.org\pub when running this test on Windows ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

);

$this->assertIteratorInForeach($contains, $i);
}
}
37 changes: 37 additions & 0 deletions src/Symfony/Component/Finder/Tests/Iterator/IteratorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,41 @@ protected function assertOrderedIterator($expected, \Traversable $iterator)

$this->assertEquals($expected, array_values($values));
}

/**
* Same as IteratorTestCase::assertIterator with foreach usage
*
* @param array $expected
* @param \Traversable $iterator
*/
protected function assertIteratorInForeach($expected, \Traversable $iterator)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why in ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, I do not really reflects on it name.

Have you a better naming?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When reading the method names in the test before seeing their definition, I thought it was asserting that the iterator contains these elements, not that it was exactly these elements, because of the In in the name.

{
$values = array();
foreach ($iterator as $file) {
$this->assertInstanceOf('Symfony\\Component\\Finder\\SplFileInfo', $file);
$values[] = $file->getPathname();
}

sort($values);
sort($expected);

$this->assertEquals($expected, array_values($values));
}

/**
* Same as IteratorTestCase::assertOrderedIterator with foreach usage
*
* @param array $expected
* @param \Traversable $iterator
*/
protected function assertOrderedIteratorInForeach($expected, \Traversable $iterator)
{
$values = array();
foreach ($iterator as $file) {
$this->assertInstanceOf('Symfony\\Component\\Finder\\SplFileInfo', $file);
$values[] = $file->getPathname();
}

$this->assertEquals($expected, array_values($values));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Finder\Tests\Iterator;

use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;

class RecursiveDirectoryIteratorTest extends IteratorTestCase
{
/**
* @dataProvider getPaths
*
* @param string $path
* @param Boolean $seekable
* @param Boolean $supports
* @param string $message
*/
public function testRewind($path, $seekable, $contains, $message = null)
{
try {
$i = new RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
} catch (\UnexpectedValueException $e) {
$this->markTestSkipped(sprintf('Unsupported stream "%s".', $path));
}

$i->rewind();

$this->assertTrue(true, $message);
}

/**
* @dataProvider getPaths
*
* @param string $path
* @param Boolean $seekable
* @param Boolean $supports
* @param string $message
*/
public function testSeek($path, $seekable, $contains, $message = null)
{
try {
$i = new RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
} catch (\UnexpectedValueException $e) {
$this->markTestSkipped(sprintf('Unsupported stream "%s".', $path));
}

$actual = array();

$i->seek(0);
$actual[] = $i->getPathname();

$i->seek(1);
$actual[] = $i->getPathname();

$i->seek(2);
$actual[] = $i->getPathname();

$this->assertEquals($contains, $actual);
}

public function getPaths()
{
$data = array();

// ftp
$contains = array(
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'README',
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'index.html',
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'pub',
);
$data[] = array('ftp://ftp.mozilla.org/', false, $contains);

return $data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A data provider with a single dataset is useless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed however it will be more easy to add others dataset. Moreover the data provider is used by two test method. So it avoids duplicate code.

}
}