RecursiveDirectoryIterator.php 3.9 KB

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