| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace WindPressDeps\Symfony\Component\Finder\Iterator; |
| 12 |
|
| 13 |
use WindPressDeps\Symfony\Component\Finder\Exception\AccessDeniedException; |
| 14 |
use WindPressDeps\Symfony\Component\Finder\SplFileInfo; |
| 15 |
/** |
| 16 |
* Extends the \RecursiveDirectoryIterator to support relative paths. |
| 17 |
* |
| 18 |
* @author Victor Berchet <victor@suumit.com> |
| 19 |
*/ |
| 20 |
class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var bool |
| 24 |
*/ |
| 25 |
private $ignoreUnreadableDirs; |
| 26 |
/** |
| 27 |
* @var bool |
| 28 |
*/ |
| 29 |
private $ignoreFirstRewind = \true; |
| 30 |
// these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations |
| 31 |
private $rootPath; |
| 32 |
private $subPath; |
| 33 |
private $directorySeparator = '/'; |
| 34 |
/** |
| 35 |
* @throws \RuntimeException |
| 36 |
*/ |
| 37 |
public function __construct(string $path, int $flags, bool $ignoreUnreadableDirs = \false) |
| 38 |
{ |
| 39 |
if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) { |
| 40 |
throw new \RuntimeException('This iterator only support returning current as fileinfo.'); |
| 41 |
} |
| 42 |
parent::__construct($path, $flags); |
| 43 |
$this->ignoreUnreadableDirs = $ignoreUnreadableDirs; |
| 44 |
$this->rootPath = $path; |
| 45 |
if ('/' !== \DIRECTORY_SEPARATOR && !($flags & self::UNIX_PATHS)) { |
| 46 |
$this->directorySeparator = \DIRECTORY_SEPARATOR; |
| 47 |
} |
| 48 |
} |
| 49 |
/** |
| 50 |
* Return an instance of SplFileInfo with support for relative paths. |
| 51 |
* |
| 52 |
* @return SplFileInfo |
| 53 |
*/ |
| 54 |
#[\ReturnTypeWillChange] |
| 55 |
public function current() |
| 56 |
{ |
| 57 |
// the logic here avoids redoing the same work in all iterations |
| 58 |
if (null === $subPathname = $this->subPath) { |
| 59 |
$subPathname = $this->subPath = $this->getSubPath(); |
| 60 |
} |
| 61 |
if ('' !== $subPathname) { |
| 62 |
$subPathname .= $this->directorySeparator; |
| 63 |
} |
| 64 |
$subPathname .= $this->getFilename(); |
| 65 |
$basePath = $this->rootPath; |
| 66 |
if ('/' !== $basePath && !str_ends_with($basePath, $this->directorySeparator) && !str_ends_with($basePath, '/')) { |
| 67 |
$basePath .= $this->directorySeparator; |
| 68 |
} |
| 69 |
return new SplFileInfo($basePath . $subPathname, $this->subPath, $subPathname); |
| 70 |
} |
| 71 |
/** |
| 72 |
* @param bool $allowLinks |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
#[\ReturnTypeWillChange] |
| 77 |
public function hasChildren($allowLinks = \false) |
| 78 |
{ |
| 79 |
$hasChildren = parent::hasChildren($allowLinks); |
| 80 |
if (!$hasChildren || !$this->ignoreUnreadableDirs) { |
| 81 |
return $hasChildren; |
| 82 |
} |
| 83 |
try { |
| 84 |
parent::getChildren(); |
| 85 |
return \true; |
| 86 |
} catch (\UnexpectedValueException $e) { |
| 87 |
// If directory is unreadable and finder is set to ignore it, skip children |
| 88 |
return \false; |
| 89 |
} |
| 90 |
} |
| 91 |
/** |
| 92 |
* @return \RecursiveDirectoryIterator |
| 93 |
* |
| 94 |
* @throws AccessDeniedException |
| 95 |
*/ |
| 96 |
#[\ReturnTypeWillChange] |
| 97 |
public function getChildren() |
| 98 |
{ |
| 99 |
try { |
| 100 |
$children = parent::getChildren(); |
| 101 |
if ($children instanceof self) { |
| 102 |
// parent method will call the constructor with default arguments, so unreadable dirs won't be ignored anymore |
| 103 |
$children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs; |
| 104 |
// performance optimization to avoid redoing the same work in all children |
| 105 |
$children->rootPath = $this->rootPath; |
| 106 |
} |
| 107 |
return $children; |
| 108 |
} catch (\UnexpectedValueException $e) { |
| 109 |
throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e); |
| 110 |
} |
| 111 |
} |
| 112 |
/** |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
#[\ReturnTypeWillChange] |
| 116 |
public function next() |
| 117 |
{ |
| 118 |
$this->ignoreFirstRewind = \false; |
| 119 |
parent::next(); |
| 120 |
} |
| 121 |
/** |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
#[\ReturnTypeWillChange] |
| 125 |
public function rewind() |
| 126 |
{ |
| 127 |
// some streams like FTP are not rewindable, ignore the first rewind after creation, |
| 128 |
// as newly created DirectoryIterator does not need to be rewound |
| 129 |
if ($this->ignoreFirstRewind) { |
| 130 |
$this->ignoreFirstRewind = \false; |
| 131 |
return; |
| 132 |
} |
| 133 |
parent::rewind(); |
| 134 |
} |
| 135 |
} |
| 136 |
|