Skip to content

Commit

Permalink
[Finder] Fix initial directory is opened twice
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored and nicolas-grekas committed Jul 6, 2023
1 parent 078e9a5 commit 96c05af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
37 changes: 13 additions & 24 deletions Iterator/RecursiveDirectoryIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
/**
* @var bool
*/
private $rewindable;
private $ignoreFirstRewind = true;

// these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations
private $rootPath;
Expand Down Expand Up @@ -118,7 +118,6 @@ public function getChildren()
$children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs;

// performance optimization to avoid redoing the same work in all children
$children->rewindable = &$this->rewindable;
$children->rootPath = $this->rootPath;
}

Expand All @@ -129,40 +128,30 @@ public function getChildren()
}

/**
* Do nothing for non rewindable stream.
*
* @return void
*/
#[\ReturnTypeWillChange]
public function rewind()
public function next()
{
if (false === $this->isRewindable()) {
return;
}
$this->ignoreFirstRewind = false;

parent::rewind();
parent::next();
}

/**
* Checks if the stream is rewindable.
*
* @return bool
* @return void
*/
public function isRewindable()
#[\ReturnTypeWillChange]
public function rewind()
{
if (null !== $this->rewindable) {
return $this->rewindable;
}

if (false !== $stream = @opendir($this->getPath())) {
$infos = stream_get_meta_data($stream);
closedir($stream);
// some streams like FTP are not rewindable, ignore the first rewind after creation,
// as newly created DirectoryIterator does not need to be rewound
if ($this->ignoreFirstRewind) {
$this->ignoreFirstRewind = false;

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

return $this->rewindable = false;
parent::rewind();
}
}
19 changes: 9 additions & 10 deletions Tests/Iterator/RecursiveDirectoryIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@

class RecursiveDirectoryIteratorTest extends IteratorTestCase
{
protected function setUp(): void
{
if (!\in_array('ftp', stream_get_wrappers(), true) || !\ini_get('allow_url_fopen')) {
$this->markTestSkipped('Unsupported stream "ftp".');
}
}

/**
* @group network
*/
public function testRewindOnFtp()
{
try {
$i = new RecursiveDirectoryIterator('ftp://speedtest:speedtest@ftp.otenet.gr/', \RecursiveDirectoryIterator::SKIP_DOTS);
} catch (\UnexpectedValueException $e) {
$this->markTestSkipped('Unsupported stream "ftp".');
}
$i = new RecursiveDirectoryIterator('ftp://speedtest:speedtest@ftp.otenet.gr/', \RecursiveDirectoryIterator::SKIP_DOTS);

$i->rewind();

Expand All @@ -36,11 +39,7 @@ public function testRewindOnFtp()
*/
public function testSeekOnFtp()
{
try {
$i = new RecursiveDirectoryIterator('ftp://speedtest:speedtest@ftp.otenet.gr/', \RecursiveDirectoryIterator::SKIP_DOTS);
} catch (\UnexpectedValueException $e) {
$this->markTestSkipped('Unsupported stream "ftp".');
}
$i = new RecursiveDirectoryIterator('ftp://speedtest:speedtest@ftp.otenet.gr/', \RecursiveDirectoryIterator::SKIP_DOTS);

$contains = [
'ftp://speedtest:speedtest@ftp.otenet.gr'.\DIRECTORY_SEPARATOR.'test100Mb.db',
Expand Down

0 comments on commit 96c05af

Please sign in to comment.