18
18
*/
19
19
class ExcludeDirectoryFilterIterator extends FilterIterator implements \RecursiveIterator
20
20
{
21
+ private $ iterator ;
21
22
private $ isRecursive ;
22
- private $ patterns ;
23
+ private $ excludedDirs = array ();
24
+ private $ excludedPattern ;
23
25
24
26
/**
25
27
* Constructor.
@@ -29,10 +31,18 @@ class ExcludeDirectoryFilterIterator extends FilterIterator implements \Recursiv
29
31
*/
30
32
public function __construct (\Iterator $ iterator , array $ directories )
31
33
{
34
+ $ this ->iterator = $ iterator;
32
35
$ this ->isRecursive = $ iterator instanceof \RecursiveIterator;
33
- $ this -> patterns = array ();
36
+ $ patterns = array ();
34
37
foreach ($ directories as $ directory ) {
35
- $ this ->patterns [] = '#(^|/) ' .preg_quote ($ directory , '# ' ).'(/|$)# ' ;
38
+ if (!$ this ->isRecursive || false !== strpos ($ directory , '/ ' )) {
39
+ $ patterns [] = preg_quote ($ directory , '# ' );
40
+ } else {
41
+ $ this ->excludedDirs [$ directory ] = true ;
42
+ }
43
+ }
44
+ if ($ patterns ) {
45
+ $ this ->excludedPattern = '#(?:^|/)(?: ' .implode ('| ' , $ patterns ).')(?:/|$)# ' ;
36
46
}
37
47
38
48
parent ::__construct ($ iterator );
@@ -45,26 +55,30 @@ public function __construct(\Iterator $iterator, array $directories)
45
55
*/
46
56
public function accept ()
47
57
{
48
- $ path = $ this ->isDir () ? $ this ->current ()->getRelativePathname () : $ this ->current ()->getRelativePath ();
49
- $ path = str_replace ('\\' , '/ ' , $ path );
50
- foreach ($ this ->patterns as $ pattern ) {
51
- if (preg_match ($ pattern , $ path )) {
52
- return false ;
53
- }
58
+ if ($ this ->isRecursive && isset ($ this ->excludedDirs [$ this ->getFilename ()]) && $ this ->isDir ()) {
59
+ return false ;
60
+ }
61
+
62
+ if ($ this ->excludedPattern ) {
63
+ $ path = $ this ->isDir () ? $ this ->current ()->getRelativePathname () : $ this ->current ()->getRelativePath ();
64
+ $ path = str_replace ('\\' , '/ ' , $ path );
65
+
66
+ return !preg_match ($ this ->excludedPattern , $ path );
54
67
}
55
68
56
69
return true ;
57
70
}
58
71
59
72
public function hasChildren ()
60
73
{
61
- return $ this ->isRecursive && $ this ->getInnerIterator () ->hasChildren ();
74
+ return $ this ->isRecursive && $ this ->iterator ->hasChildren ();
62
75
}
63
76
64
77
public function getChildren ()
65
78
{
66
- $ children = new self ($ this ->getInnerIterator ()->getChildren (), array ());
67
- $ children ->patterns = $ this ->patterns ;
79
+ $ children = new self ($ this ->iterator ->getChildren (), array ());
80
+ $ children ->excludedDirs = $ this ->excludedDirs ;
81
+ $ children ->excludedPattern = $ this ->excludedPattern ;
68
82
69
83
return $ children ;
70
84
}
0 commit comments