@@ -30,6 +30,7 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator implements \Recursi
30
30
/** @var array<string, true> */
31
31
private array $ excludedDirs = [];
32
32
private ?string $ excludedPattern = null ;
33
+ private ?string $ excludedPatternAbsolute = null ;
33
34
/** @var list<callable(SplFileInfo):bool> */
34
35
private array $ pruneFilters = [];
35
36
@@ -42,6 +43,7 @@ public function __construct(\Iterator $iterator, array $directories)
42
43
$ this ->iterator = $ iterator ;
43
44
$ this ->isRecursive = $ iterator instanceof \RecursiveIterator;
44
45
$ patterns = [];
46
+ $ patternsAbsolute = [];
45
47
foreach ($ directories as $ directory ) {
46
48
if (!\is_string ($ directory )) {
47
49
if (!\is_callable ($ directory )) {
@@ -54,7 +56,13 @@ public function __construct(\Iterator $iterator, array $directories)
54
56
}
55
57
56
58
$ directory = rtrim ($ directory , '/ ' );
57
- if (!$ this ->isRecursive || str_contains ($ directory , '/ ' )) {
59
+ $ slashPos = strpos ($ directory , '/ ' );
60
+ if (false !== $ slashPos && \strlen ($ directory ) - 1 !== $ slashPos ) {
61
+ if (0 === $ slashPos ) {
62
+ $ directory = substr ($ directory , 1 );
63
+ }
64
+ $ patternsAbsolute [] = preg_quote ($ directory , '# ' );
65
+ } elseif (!$ this ->isRecursive || str_contains ($ directory , '/ ' )) {
58
66
$ patterns [] = preg_quote ($ directory , '# ' );
59
67
} else {
60
68
$ this ->excludedDirs [$ directory ] = true ;
@@ -64,6 +72,10 @@ public function __construct(\Iterator $iterator, array $directories)
64
72
$ this ->excludedPattern = '#(?:^|/)(?: ' .implode ('| ' , $ patterns ).')(?:/|$)# ' ;
65
73
}
66
74
75
+ if ($ patternsAbsolute ) {
76
+ $ this ->excludedPatternAbsolute = '#^( ' .implode ('| ' , $ patternsAbsolute ).')$# ' ;
77
+ }
78
+
67
79
parent ::__construct ($ iterator );
68
80
}
69
81
@@ -76,11 +88,15 @@ public function accept(): bool
76
88
return false ;
77
89
}
78
90
79
- if ($ this ->excludedPattern ) {
91
+ if ($ this ->excludedPattern || $ this -> excludedPatternAbsolute ) {
80
92
$ path = $ this ->isDir () ? $ this ->current ()->getRelativePathname () : $ this ->current ()->getRelativePath ();
81
93
$ path = str_replace ('\\' , '/ ' , $ path );
82
-
83
- return !preg_match ($ this ->excludedPattern , $ path );
94
+ }
95
+ if ($ this ->excludedPattern && preg_match ($ this ->excludedPattern , $ path )) {
96
+ return false ;
97
+ }
98
+ if ($ this ->excludedPatternAbsolute && preg_match ($ this ->excludedPatternAbsolute , $ path )) {
99
+ return false ;
84
100
}
85
101
86
102
if ($ this ->pruneFilters && $ this ->hasChildren ()) {
0 commit comments