Skip to content

Commit 84d58ed

Browse files
Merge branch '6.2' into 6.3
* 6.2: [Finder] Fix initial directory is opened twice typo fix Fix test Fix some return types in tests
2 parents d9b01ba + e27875f commit 84d58ed

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

Iterator/RecursiveDirectoryIterator.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
2525
{
2626
private bool $ignoreUnreadableDirs;
27+
private bool $ignoreFirstRewind = true;
2728
private ?bool $rewindable = null;
2829

2930
// these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations
@@ -102,7 +103,6 @@ public function getChildren(): \RecursiveDirectoryIterator
102103
$children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs;
103104

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

@@ -112,36 +112,23 @@ public function getChildren(): \RecursiveDirectoryIterator
112112
}
113113
}
114114

115-
/**
116-
* Do nothing for non rewindable stream.
117-
*/
118-
public function rewind(): void
115+
public function next(): void
119116
{
120-
if (false === $this->isRewindable()) {
121-
return;
122-
}
117+
$this->ignoreFirstRewind = false;
123118

124-
parent::rewind();
119+
parent::next();
125120
}
126121

127-
/**
128-
* Checks if the stream is rewindable.
129-
*/
130-
public function isRewindable(): bool
122+
public function rewind(): void
131123
{
132-
if (null !== $this->rewindable) {
133-
return $this->rewindable;
134-
}
135-
136-
if (false !== $stream = @opendir($this->getPath())) {
137-
$infos = stream_get_meta_data($stream);
138-
closedir($stream);
124+
// some streams like FTP are not rewindable, ignore the first rewind after creation,
125+
// as newly created DirectoryIterator does not need to be rewound
126+
if ($this->ignoreFirstRewind) {
127+
$this->ignoreFirstRewind = false;
139128

140-
if ($infos['seekable']) {
141-
return $this->rewindable = true;
142-
}
129+
return;
143130
}
144131

145-
return $this->rewindable = false;
132+
parent::rewind();
146133
}
147134
}

Tests/Iterator/RecursiveDirectoryIteratorTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515

1616
class RecursiveDirectoryIteratorTest extends IteratorTestCase
1717
{
18+
protected function setUp(): void
19+
{
20+
if (!\in_array('ftp', stream_get_wrappers(), true) || !\ini_get('allow_url_fopen')) {
21+
$this->markTestSkipped('Unsupported stream "ftp".');
22+
}
23+
}
24+
1825
/**
1926
* @group network
2027
*/
2128
public function testRewindOnFtp()
2229
{
23-
try {
24-
$i = new RecursiveDirectoryIterator('ftp://speedtest:[email protected]/', \RecursiveDirectoryIterator::SKIP_DOTS);
25-
} catch (\UnexpectedValueException $e) {
26-
$this->markTestSkipped('Unsupported stream "ftp".');
27-
}
30+
$i = new RecursiveDirectoryIterator('ftp://speedtest:[email protected]/', \RecursiveDirectoryIterator::SKIP_DOTS);
2831

2932
$i->rewind();
3033

@@ -36,11 +39,7 @@ public function testRewindOnFtp()
3639
*/
3740
public function testSeekOnFtp()
3841
{
39-
try {
40-
$i = new RecursiveDirectoryIterator('ftp://speedtest:[email protected]/', \RecursiveDirectoryIterator::SKIP_DOTS);
41-
} catch (\UnexpectedValueException $e) {
42-
$this->markTestSkipped('Unsupported stream "ftp".');
43-
}
42+
$i = new RecursiveDirectoryIterator('ftp://speedtest:[email protected]/', \RecursiveDirectoryIterator::SKIP_DOTS);
4443

4544
$contains = [
4645
'ftp://speedtest:[email protected]'.\DIRECTORY_SEPARATOR.'test100Mb.db',

0 commit comments

Comments
 (0)