Skip to content

Commit ad49689

Browse files
committed
Fix initial directory is opened twice
1 parent 39d95e2 commit ad49689

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
2929
/**
3030
* @var bool
3131
*/
32-
private $rewindable;
32+
private $ignoreFirstRewind = true;
3333

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

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

@@ -129,40 +128,30 @@ public function getChildren()
129128
}
130129

131130
/**
132-
* Do nothing for non rewindable stream.
133-
*
134131
* @return void
135132
*/
136133
#[\ReturnTypeWillChange]
137-
public function rewind()
134+
public function next()
138135
{
139-
if (false === $this->isRewindable()) {
140-
return;
141-
}
136+
$this->ignoreFirstRewind = false;
142137

143-
parent::rewind();
138+
parent::next();
144139
}
145140

146141
/**
147-
* Checks if the stream is rewindable.
148-
*
149-
* @return bool
142+
* @return void
150143
*/
151-
public function isRewindable()
144+
#[\ReturnTypeWillChange]
145+
public function rewind()
152146
{
153-
if (null !== $this->rewindable) {
154-
return $this->rewindable;
155-
}
156-
157-
if (false !== $stream = @opendir($this->getPath())) {
158-
$infos = stream_get_meta_data($stream);
159-
closedir($stream);
147+
// some streams like FTP are not rewindable, ignore the first rewind after creation,
148+
// as newly created DirectoryIterator does not need to be rewind
149+
if (true === $this->ignoreFirstRewind) {
150+
$this->ignoreFirstRewind = false;
160151

161-
if ($infos['seekable']) {
162-
return $this->rewindable = true;
163-
}
152+
return;
164153
}
165154

166-
return $this->rewindable = false;
155+
parent::rewind();
167156
}
168157
}

0 commit comments

Comments
 (0)