@@ -36,29 +36,15 @@ public function __construct($resource, $pattern = null)
36
36
/**
37
37
* Returns the list of filtered file and directory childs of directory resource.
38
38
*
39
- * @param Boolean $recursive search for files recursive
40
- *
41
39
* @return array An array of files
42
40
*/
43
- public function getFilteredChilds ($ recursive = true )
41
+ public function getFilteredChilds ()
44
42
{
45
- $ iterator = $ recursive
46
- ? new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator ($ this ->resource ), \RecursiveIteratorIterator::SELF_FIRST )
47
- : new \DirectoryIterator ($ this ->resource );
48
-
49
43
$ childs = array ();
50
- foreach ($ iterator as $ file ) {
44
+ foreach (new \ RecursiveIteratorIterator ( new \ RecursiveDirectoryIterator ( $ this -> resource ), \RecursiveIteratorIterator:: SELF_FIRST ) as $ file ) {
51
45
// if regex filtering is enabled only return matching files
52
- if (isset ($ this ->filterRegexList ) && $ file ->isFile ()) {
53
- $ regexMatched = false ;
54
- foreach ($ this ->filterRegexList as $ regex ) {
55
- if (preg_match ($ regex , (string ) $ file )) {
56
- $ regexMatched = true ;
57
- }
58
- }
59
- if (!$ regexMatched ) {
60
- continue ;
61
- }
46
+ if (!$ this ->isFileMatchesFilters ($ file )) {
47
+ continue ;
62
48
}
63
49
64
50
// always monitor directories for changes, except the .. entries
@@ -73,6 +59,36 @@ public function getFilteredChilds($recursive = true)
73
59
return $ childs ;
74
60
}
75
61
62
+ /**
63
+ * Returns child resources that matches directory filters.
64
+ *
65
+ * @return array
66
+ */
67
+ public function getFilteredChildResources ()
68
+ {
69
+ $ iterator = new \DirectoryIterator ($ this ->resource );
70
+
71
+ $ resources = array ();
72
+ foreach ($ iterator as $ file ) {
73
+ // if regex filtering is enabled only return matching files
74
+ if (!$ this ->isFileMatchesFilters ($ file )) {
75
+ continue ;
76
+ }
77
+
78
+ // always monitor directories for changes, except the .. entries
79
+ // (otherwise deleted files wouldn't get detected)
80
+ if ($ file ->isDir () && '/.. ' === substr ($ file , -3 )) {
81
+ continue ;
82
+ }
83
+
84
+ $ resources [] = $ file ->isDir ()
85
+ ? new DirectoryResource ((string ) $ file )
86
+ : new FileResource ((string ) $ file );
87
+ }
88
+
89
+ return $ resources ;
90
+ }
91
+
76
92
/**
77
93
* Returns a string representation of the Resource.
78
94
*
@@ -151,4 +167,28 @@ public function unserialize($serialized)
151
167
{
152
168
list ($ this ->resource , $ this ->pattern ) = unserialize ($ serialized );
153
169
}
170
+
171
+ /**
172
+ * Checks that passed file matches specified in resource filters.
173
+ *
174
+ * @param \SplFileInfo $file
175
+ *
176
+ * @return Boolean
177
+ */
178
+ private function isFileMatchesFilters (\SplFileInfo $ file )
179
+ {
180
+ if (isset ($ this ->filterRegexList ) && $ file ->isFile ()) {
181
+ $ regexMatched = false ;
182
+ foreach ($ this ->filterRegexList as $ regex ) {
183
+ if (preg_match ($ regex , (string ) $ file )) {
184
+ $ regexMatched = true ;
185
+ }
186
+ }
187
+ if (!$ regexMatched ) {
188
+ return false ;
189
+ }
190
+ }
191
+
192
+ return true ;
193
+ }
154
194
}
0 commit comments