16
16
*
17
17
* @author Fabien Potencier <fabien@symfony.com>
18
18
*/
19
- class DirectoryResource implements ResourceInterface
19
+ class DirectoryResource implements ResourceInterface, \Serializable
20
20
{
21
21
private $ resource ;
22
22
private $ pattern ;
@@ -33,82 +33,6 @@ public function __construct($resource, $pattern = null)
33
33
$ this ->pattern = $ pattern ;
34
34
}
35
35
36
- /**
37
- * Returns the list of filtered file and directory childs of directory resource.
38
- *
39
- * @return array An array of files
40
- */
41
- public function getFilteredChilds ()
42
- {
43
- if (!$ this ->exists ()) {
44
- return array ();
45
- }
46
-
47
- $ iterator = new \RecursiveIteratorIterator (
48
- new \RecursiveDirectoryIterator ($ this ->resource , \FilesystemIterator::SKIP_DOTS ),
49
- \RecursiveIteratorIterator::SELF_FIRST
50
- );
51
-
52
- $ childs = array ();
53
- foreach ($ iterator as $ file ) {
54
- // if regex filtering is enabled only return matching files
55
- if ($ file ->isFile () && !$ this ->hasFile ($ file )) {
56
- continue ;
57
- }
58
-
59
- // always monitor directories for changes, except the .. entries
60
- // (otherwise deleted files wouldn't get detected)
61
- if ($ file ->isDir () && '/.. ' === substr ($ file , -3 )) {
62
- continue ;
63
- }
64
-
65
- $ childs [] = $ file ;
66
- }
67
-
68
- return $ childs ;
69
- }
70
-
71
- /**
72
- * Returns child resources that matches directory filters.
73
- *
74
- * @return array
75
- */
76
- public function getFilteredResources ()
77
- {
78
- if (!$ this ->exists ()) {
79
- return array ();
80
- }
81
-
82
- $ iterator = new \DirectoryIterator ($ this ->resource );
83
-
84
- $ resources = array ();
85
- foreach ($ iterator as $ file ) {
86
- // if regex filtering is enabled only return matching files
87
- if ($ file ->isFile () && !$ this ->hasFile ($ file )) {
88
- continue ;
89
- }
90
-
91
- // always monitor directories for changes, except the .. entries
92
- // (otherwise deleted files wouldn't get detected)
93
- if ($ file ->isDir () && '/.. ' === substr ($ file , -3 )) {
94
- continue ;
95
- }
96
-
97
- // if file is dot - continue
98
- if ($ file ->isDot ()) {
99
- continue ;
100
- }
101
-
102
- if ($ file ->isFile ()) {
103
- $ resources [] = new FileResource ($ file ->getRealPath ());
104
- } elseif ($ file ->isDir ()) {
105
- $ resources [] = new DirectoryResource ($ file ->getRealPath ());
106
- }
107
- }
108
-
109
- return $ resources ;
110
- }
111
-
112
36
/**
113
37
* Returns a string representation of the Resource.
114
38
*
@@ -129,62 +53,11 @@ public function getResource()
129
53
return $ this ->resource ;
130
54
}
131
55
132
- /**
133
- * Returns check pattern.
134
- *
135
- * @return mixed
136
- */
137
56
public function getPattern ()
138
57
{
139
58
return $ this ->pattern ;
140
59
}
141
60
142
- /**
143
- * Checks that passed file exists in resource and matches resource filters.
144
- *
145
- * @param SplFileInfo|string $file
146
- *
147
- * @return Boolean
148
- */
149
- public function hasFile ($ file )
150
- {
151
- if (!$ file instanceof \SplFileInfo) {
152
- $ file = new \SplFileInfo ($ file );
153
- }
154
-
155
- if (0 !== strpos ($ file ->getRealPath (), realpath ($ this ->resource ))) {
156
- return false ;
157
- }
158
-
159
- if ($ this ->pattern ) {
160
- return (bool ) preg_match ($ this ->pattern , $ file ->getBasename ());
161
- }
162
-
163
- return true ;
164
- }
165
-
166
- /**
167
- * Returns resource mtime.
168
- *
169
- * @return integer
170
- */
171
- public function getModificationTime ()
172
- {
173
- if (!$ this ->exists ()) {
174
- return -1 ;
175
- }
176
-
177
- clearstatcache (true , $ this ->resource );
178
- $ newestMTime = filemtime ($ this ->resource );
179
-
180
- foreach ($ this ->getFilteredChilds () as $ file ) {
181
- clearstatcache (true , (string ) $ file );
182
- $ newestMTime = max ($ file ->getMTime (), $ newestMTime );
183
- }
184
-
185
- return $ newestMTime ;
186
- }
187
-
188
61
/**
189
62
* Returns true if the resource has not been updated since the given timestamp.
190
63
*
@@ -194,31 +67,27 @@ public function getModificationTime()
194
67
*/
195
68
10000
public function isFresh ($ timestamp )
196
69
{
197
- if (!$ this ->exists ( )) {
70
+ if (!is_dir ( $ this ->resource )) {
198
71
return false ;
199
72
}
200
73
201
- return $ this ->getModificationTime () < $ timestamp ;
202
- }
74
+ $ newestMTime = filemtime ($ this ->resource );
75
+ foreach (new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator ($ this ->resource ), \RecursiveIteratorIterator::SELF_FIRST ) as $ file ) {
76
+ // if regex filtering is enabled only check matching files
77
+ if ($ this ->pattern && $ file ->isFile () && !preg_match ($ this ->pattern , $ file ->getBasename ())) {
78
+ continue ;
79
+ }
203
80
204
- /**
205
- * Returns true if the resource exists in the filesystem.
206
- *
207
- * @return Boolean
208
- */
209
- public function exists ()
210
- {
211
- return is_dir ($ this ->resource );
212
- }
81
+ // always monitor directories for changes, except the .. entries
82
+ // (otherwise deleted files wouldn't get detected)
83
+ if ($ file ->isDir () && '/.. ' === substr ($ file , -3 )) {
84
+ continue ;
85
+ }
213
86
214
- /**
215
- * Returns unique resource ID.
216
- *
217
- * @return string
218
- */
219
- public function getId ()
220
- {
221
- return md5 ('d ' .$ this ->resource .$ this ->pattern );
87
+ $ newestMTime = max ($ file ->getMTime (), $ newestMTime );
88
+ }
89
+
90
+ return $ newestMTime < $ timestamp ;
222
91
}
223
92
224
93
public function serialize ()
0 commit comments