CustomFilterIterator.php
4 years ago
DateRangeFilterIterator.php
4 years ago
DepthRangeFilterIterator.php
4 years ago
ExcludeDirectoryFilterIterator.php
4 years ago
FileTypeFilterIterator.php
4 years ago
FilecontentFilterIterator.php
4 years ago
FilenameFilterIterator.php
4 years ago
LazyIterator.php
4 years ago
MultiplePcreFilterIterator.php
4 years ago
PathFilterIterator.php
4 years ago
RecursiveDirectoryIterator.php
4 years ago
SizeRangeFilterIterator.php
4 years ago
SortableIterator.php
4 years ago
index.php
3 years ago
RecursiveDirectoryIterator.php
87 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Finder\Iterator; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Finder\Exception\AccessDeniedException; |
| 5 | use MailPoetVendor\Symfony\Component\Finder\SplFileInfo; |
| 6 | class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator |
| 7 | { |
| 8 | private $ignoreUnreadableDirs; |
| 9 | private $rewindable; |
| 10 | // these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations |
| 11 | private $rootPath; |
| 12 | private $subPath; |
| 13 | private $directorySeparator = '/'; |
| 14 | public function __construct(string $path, int $flags, bool $ignoreUnreadableDirs = \false) |
| 15 | { |
| 16 | if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) { |
| 17 | throw new \RuntimeException('This iterator only support returning current as fileinfo.'); |
| 18 | } |
| 19 | parent::__construct($path, $flags); |
| 20 | $this->ignoreUnreadableDirs = $ignoreUnreadableDirs; |
| 21 | $this->rootPath = $path; |
| 22 | if ('/' !== \DIRECTORY_SEPARATOR && !($flags & self::UNIX_PATHS)) { |
| 23 | $this->directorySeparator = \DIRECTORY_SEPARATOR; |
| 24 | } |
| 25 | } |
| 26 | #[\ReturnTypeWillChange] |
| 27 | public function current() |
| 28 | { |
| 29 | // the logic here avoids redoing the same work in all iterations |
| 30 | if (null === ($subPathname = $this->subPath)) { |
| 31 | $subPathname = $this->subPath = $this->getSubPath(); |
| 32 | } |
| 33 | if ('' !== $subPathname) { |
| 34 | $subPathname .= $this->directorySeparator; |
| 35 | } |
| 36 | $subPathname .= $this->getFilename(); |
| 37 | if ('/' !== ($basePath = $this->rootPath)) { |
| 38 | $basePath .= $this->directorySeparator; |
| 39 | } |
| 40 | return new SplFileInfo($basePath . $subPathname, $this->subPath, $subPathname); |
| 41 | } |
| 42 | #[\ReturnTypeWillChange] |
| 43 | public function getChildren() |
| 44 | { |
| 45 | try { |
| 46 | $children = parent::getChildren(); |
| 47 | if ($children instanceof self) { |
| 48 | // parent method will call the constructor with default arguments, so unreadable dirs won't be ignored anymore |
| 49 | $children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs; |
| 50 | // performance optimization to avoid redoing the same work in all children |
| 51 | $children->rewindable =& $this->rewindable; |
| 52 | $children->rootPath = $this->rootPath; |
| 53 | } |
| 54 | return $children; |
| 55 | } catch (\UnexpectedValueException $e) { |
| 56 | if ($this->ignoreUnreadableDirs) { |
| 57 | // If directory is unreadable and finder is set to ignore it, a fake empty content is returned. |
| 58 | return new \RecursiveArrayIterator([]); |
| 59 | } else { |
| 60 | throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | #[\ReturnTypeWillChange] |
| 65 | public function rewind() |
| 66 | { |
| 67 | if (\false === $this->isRewindable()) { |
| 68 | return; |
| 69 | } |
| 70 | parent::rewind(); |
| 71 | } |
| 72 | public function isRewindable() |
| 73 | { |
| 74 | if (null !== $this->rewindable) { |
| 75 | return $this->rewindable; |
| 76 | } |
| 77 | if (\false !== ($stream = @\opendir($this->getPath()))) { |
| 78 | $infos = \stream_get_meta_data($stream); |
| 79 | \closedir($stream); |
| 80 | if ($infos['seekable']) { |
| 81 | return $this->rewindable = \true; |
| 82 | } |
| 83 | } |
| 84 | return $this->rewindable = \false; |
| 85 | } |
| 86 | } |
| 87 |