@@ -26,6 +26,7 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator implements \Recursi
26
26
private $ isRecursive ;
27
27
private $ excludedDirs = [];
28
28
private $ excludedPattern ;
29
+ private $ excludedPatternAbsolute ;
29
30
30
31
/**
31
32
* @param \Iterator $iterator The Iterator to filter
@@ -36,9 +37,16 @@ public function __construct(\Iterator $iterator, array $directories)
36
37
$ this ->iterator = $ iterator ;
37
38
$ this ->isRecursive = $ iterator instanceof \RecursiveIterator;
38
39
$ patterns = [];
40
+ $ patternsAbsolute = [];
39
41
foreach ($ directories as $ directory ) {
40
42
$ directory = rtrim ($ directory , '/ ' );
41
- if (!$ this ->isRecursive || str_contains ($ directory , '/ ' )) {
43
+ $ slashPos = strpos ($ directory , '/ ' );
44
+ if (false !== $ slashPos && \strlen ($ directory ) - 1 !== $ slashPos ) {
45
+ if (0 === $ slashPos ) {
46
+ $ directory = substr ($ directory , 1 );
47
+ }
48
+ $ patternsAbsolute [] = preg_quote ($ directory , '# ' );
49
+ } elseif (!$ this ->isRecursive || str_contains ($ directory , '/ ' )) {
42
50
$ patterns [] = preg_quote ($ directory , '# ' );
43
51
} else {
44
52
$ this ->excludedDirs [$ directory ] = true ;
@@ -48,6 +56,10 @@ public function __construct(\Iterator $iterator, array $directories)
48
56
$ this ->excludedPattern = '#(?:^|/)(?: ' .implode ('| ' , $ patterns ).')(?:/|$)# ' ;
49
57
}
50
58
59
+ if ($ patternsAbsolute ) {
60
+ $ this ->excludedPatternAbsolute = '#^( ' .implode ('| ' , $ patternsAbsolute ).')$# ' ;
61
+ }
62
+
51
63
parent ::__construct ($ iterator );
52
64
}
53
65
@@ -63,11 +75,15 @@ public function accept()
63
75
return false ;
64
76
}
65
77
66
- if ($ this ->excludedPattern ) {
78
+ if ($ this ->excludedPattern || $ this -> excludedPatternAbsolute ) {
67
79
$ path = $ this ->isDir () ? $ this ->current ()->getRelativePathname () : $ this ->current ()->getRelativePath ();
68
80
$ path = str_replace ('\\' , '/ ' , $ path );
69
-
70
- return !preg_match ($ this ->excludedPattern , $ path );
81
+ }
82
+ if ($ this ->excludedPattern && preg_match ($ this ->excludedPattern , $ path )) {
83
+ return false ;
84
+ }
85
+ if ($ this ->excludedPatternAbsolute && preg_match ($ this ->excludedPatternAbsolute , $ path )) {
86
+ return false ;
71
87
}
72
88
73
89
return true ;
0 commit comments