@@ -21,6 +21,9 @@ class VcsIgnoredFilterIterator extends \FilterIterator
2121 */
2222private $ baseDir ;
2323
24+ /** @var array<string, string|null> */
25+ private $ gitignoreFilesCache = [];
26+
2427public function __construct (Iterator $ iterator ,string $ baseDir )
2528 {
2629$ this ->baseDir =$ baseDir ;
@@ -41,24 +44,32 @@ public function accept(): bool
4144
4245do {
4346$ parentDirectory =\dirname ($ parentDirectory );
44- $ gitignoreFilePath ="{$ parentDirectory }/.gitignore " ;
45-
46- if (!file_exists ($ gitignoreFilePath )) {
47- continue ;
48- }
49-
50- if (!is_file ($ gitignoreFilePath ) || !is_readable ($ gitignoreFilePath )) {
51- throw new \RuntimeException ("The \"ignoreVCSIgnored \" option cannot be used by the Finder as the \"{$ gitignoreFilePath }\" file is not readable. " );
52- }
53-
5447$ relativeFilePath =substr ($ fileRealPath ,\strlen ($ parentDirectory ) +1 );
5548
56- $ regex = Gitignore::toRegex (file_get_contents ($ gitignoreFilePath ));
57- if (preg_match ($ regex ,$ relativeFilePath )) {
49+ $ regex =$ this ->readGitignoreFile ("{$ parentDirectory }/.gitignore " );
50+
51+ if (null !==$ regex &&preg_match ($ regex ,$ relativeFilePath )) {
5852return false ;
5953 }
6054 }while ($ parentDirectory !==$ this ->baseDir );
6155
6256return true ;
6357 }
58+
59+ private function readGitignoreFile (string $ path ): ?string
60+ {
61+ if (array_key_exists ($ path ,$ this ->gitignoreFilesCache )) {
62+ return $ this ->gitignoreFilesCache [$ path ];
63+ }
64+
65+ if (!file_exists ($ path )) {
66+ return null ;
67+ }
68+
69+ if (!is_file ($ path ) || !is_readable ($ path )) {
70+ throw new \RuntimeException ("The \"ignoreVCSIgnored \" option cannot be used by the Finder as the \"{$ path }\" file is not readable. " );
71+ }
72+
73+ return $ this ->gitignoreFilesCache [$ path ] = Gitignore::toRegex (file_get_contents ($ path ));
74+ }
6475}