Debian Codegraph @

vendor/symfony/finder/Iterator/RecursiveDirectoryIterator.php

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