@@ -707,6 +707,34 @@ public function count()
707
707
return iterator_count ($ this ->getIterator ());
708
708
}
709
709
710
+ private function searchGitignoreFiles (string $ dir ): self
711
+ {
712
+ $ finder = self ::create ()
713
+ ->in ($ dir )
714
+ ->ignoreDotFiles (false )
715
+ ->name ('.gitignore ' );
716
+ if ($ this ->followLinks ) {
717
+ $ finder ->followLinks ();
718
+ }
719
+
720
+ return $ finder ;
721
+ }
722
+
723
+ private function buildGitignoreRegexes (string $ dir ): array
724
+ {
725
+ return array_map (function (SplFileInfo $ file ) {
726
+ try {
727
+ $ data = $ file ->getContents ();
728
+ } catch (\RuntimeException $ e ) {
729
+ throw new \RuntimeException (sprintf ('Failed to read ".gitignore" "%s" file. ' , $ file ->getPathname ()), 0 , $ e );
730
+ }
731
+
732
+ $ dataRelativized = Gitignore::relativize ($ data , $ file ->getRelativePath ());
733
+
734
+ return Gitignore::toRegex ($ dataRelativized );
735
+ }, iterator_to_array ($ this ->searchGitignoreFiles ($ dir )));
736
+ }
737
+
710
738
private function searchInDirectory (string $ dir ): \Iterator
711
739
{
712
740
$ exclude = $ this ->exclude ;
@@ -721,11 +749,7 @@ private function searchInDirectory(string $dir): \Iterator
721
749
}
722
750
723
751
if (static ::IGNORE_VCS_IGNORED_FILES === (static ::IGNORE_VCS_IGNORED_FILES & $ this ->ignore )) {
724
- $ gitignoreFilePath = sprintf ('%s/.gitignore ' , $ dir );
725
- if (!is_readable ($ gitignoreFilePath )) {
726
- throw new \RuntimeException (sprintf ('The "ignoreVCSIgnored" option cannot be used by the Finder as the "%s" file is not readable. ' , $ gitignoreFilePath ));
727
- }
728
- $ notPaths = array_merge ($ notPaths , [Gitignore::toRegex (file_get_contents ($ gitignoreFilePath ))]);
752
+ $ notPaths = array_merge ($ notPaths , $ this ->buildGitignoreRegexes ($ dir ));
729
753
}
730
754
731
755
$ minDepth = 0 ;
0 commit comments