Skip to content

Commit

Permalink
Merge branch '5.4' into 6.2
Browse files Browse the repository at this point in the history
* 5.4:
  [Finder] Fix initial directory is opened twice
  typo fix
  Fix test
  Fix some return types in tests
  • Loading branch information
nicolas-grekas committed Jul 6, 2023
2 parents 20808dc + 96c05af commit e27875f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
35 changes: 11 additions & 24 deletions Iterator/RecursiveDirectoryIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
{
private bool $ignoreUnreadableDirs;
private bool $ignoreFirstRewind = true;
private ?bool $rewindable = null;

// these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations
Expand Down Expand Up @@ -102,7 +103,6 @@ public function getChildren(): \RecursiveDirectoryIterator
$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 @@ -112,36 +112,23 @@ public function getChildren(): \RecursiveDirectoryIterator
}
}

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

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

/**
* Checks if the stream is rewindable.
*/
public function isRewindable(): bool
public function rewind(): void
{
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 e27875f

Please sign in to comment.