Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Finder] Move check for unreachable dirs to hasChildren() #42469

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 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
private $subPath;
private $directorySeparator = '/';

/**
* @var \RecursiveDirectoryIterator|null
*/
private $children;

/**
* @throws \RuntimeException
*/
Expand All @@ -53,6 +58,17 @@ public function __construct(string $path, int $flags, bool $ignoreUnreadableDirs
}
}

/**
* @return void
*/
#[\ReturnTypeWillChange]
public function next()
{
$this->children = null;

parent::next();
}

/**
* Return an instance of SplFileInfo with support for relative paths.
*
Expand All @@ -79,15 +95,38 @@ public function current()
}

/**
* @return \RecursiveIterator
* {@inheritdoc}
*
* @return bool
*/
#[\ReturnTypeWillChange]
public function hasChildren($allowLinks = false)
{
$hasChildren = parent::hasChildren($allowLinks);

if (!$hasChildren || !$this->ignoreUnreadableDirs) {
return $hasChildren;
}

try {
$this->children = parent::getChildren();
} catch (\UnexpectedValueException $e) {
return false;
}

return true;
}

/**
* @return \RecursiveDirectoryIterator
*
* @throws AccessDeniedException
*/
#[\ReturnTypeWillChange]
public function getChildren()
{
try {
$children = parent::getChildren();
$children = $this->children ?? parent::getChildren();

if ($children instanceof self) {
// parent method will call the constructor with default arguments, so unreadable dirs won't be ignored anymore
Expand All @@ -100,12 +139,15 @@ public function getChildren()

return $children;
} catch (\UnexpectedValueException $e) {
// @deprecated The if block can be removed in 6.0.
if ($this->ignoreUnreadableDirs) {
trigger_deprecation('symfony/finder', '5.4', 'Calling "%s" if "hasChildren()" returns false is deprecated.', __METHOD__);

// If directory is unreadable and finder is set to ignore it, a fake empty content is returned.
return new \RecursiveArrayIterator([]);
} else {
throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
}

throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
}
}

Expand All @@ -117,13 +159,26 @@ public function getChildren()
#[\ReturnTypeWillChange]
public function rewind()
{
$this->children = null;

if (false === $this->isRewindable()) {
return;
}

parent::rewind();
}

/**
* @return void
*/
#[\ReturnTypeWillChange]
public function seek($offset)
{
$this->children = null;

parent::seek($offset);
}

/**
* Checks if the stream is rewindable.
*
Expand Down