2
2
3
3
namespace FQ \Query ;
4
4
5
- use FQ \Dirs \ChildDir ;
6
5
use FQ \Dirs \RootDir ;
6
+ use FQ \Dirs \ChildDir ;
7
7
use FQ \Exceptions \FileQueryException ;
8
8
use FQ \Files ;
9
9
use FQ \Query \Selection \ChildSelection ;
10
10
use FQ \Query \Selection \RootSelection ;
11
11
12
+ /**
13
+ * Class FilesQuery
14
+ * @package FQ\Query
15
+ *
16
+ * @todo Ability to lock selections if you have a reoccurring queries
17
+ */
12
18
class FilesQuery {
13
19
14
20
/**
@@ -40,6 +46,10 @@ class FilesQuery {
40
46
* @var RootDir[]
41
47
*/
42
48
private $ _cachedQueryRootDirs ;
49
+ /**
50
+ * @var ChildDir[]
51
+ */
52
+ private $ _cachedQueryChildDirs ;
43
53
44
54
/**
45
55
* @var array
@@ -98,7 +108,6 @@ class FilesQuery {
98
108
* @param Files $files
99
109
*/
100
110
function __construct (Files $ files ) {
101
- $ this ->_requirements = new FilesQueryRequirements ();
102
111
$ this ->_files = $ files ;
103
112
104
113
$ this ->reset ();
@@ -130,6 +139,9 @@ public function resetSelection() {
130
139
* @return FilesQueryRequirements
131
140
*/
132
141
public function requirements () {
142
+ if ($ this ->_requirements === null ) {
143
+ $ this ->_requirements = new FilesQueryRequirements ($ this );
144
+ }
133
145
return $ this ->_requirements ;
134
146
}
135
147
@@ -167,21 +179,29 @@ public function queryHasFilter($filter) {
167
179
return in_array ($ filter , $ this ->filters ());
168
180
}
169
181
170
- public function setRootDirSelection (RootSelection $ selection ) {
171
- $ this ->_rootDirSelection = $ selection ;
182
+ /**
183
+ * @param RootSelection $rootDirSelection
184
+ */
185
+ public function setRootDirSelection (RootSelection $ rootDirSelection ) {
186
+ $ this ->_cachedQueryRootDirs = null ;
187
+ $ this ->_rootDirSelection = $ rootDirSelection ;
172
188
}
173
- public function getRootDirSelection ($ createNewSelection = false ) {
174
- if ($ this ->_rootDirSelection === null || $ createNewSelection ) {
189
+ public function getRootDirSelection () {
190
+ if ($ this ->_rootDirSelection === null ) {
175
191
$ this ->_rootDirSelection = new RootSelection ();
176
192
}
177
193
return $ this ->_rootDirSelection ;
178
194
}
179
195
180
- public function setChildDirSelection (ChildSelection $ selection ) {
181
- $ this ->_childDirSelection = $ selection ;
196
+ /**
197
+ * @param ChildSelection $childDirSelection
198
+ */
199
+ public function setChildDirSelection (ChildSelection $ childDirSelection ) {
200
+ $ this ->_cachedQueryChildren = null ;
201
+ $ this ->_childDirSelection = $ childDirSelection ;
182
202
}
183
- public function getChildDirSelection ($ createNewSelection = false ) {
184
- if ($ this ->_childDirSelection === null || $ createNewSelection ) {
203
+ public function getChildDirSelection () {
204
+ if ($ this ->_childDirSelection === null ) {
185
205
$ this ->_childDirSelection = new ChildSelection ();
186
206
}
187
207
return $ this ->_childDirSelection ;
@@ -236,28 +256,43 @@ protected function _hasRunCheck() {
236
256
237
257
/**
238
258
* @param string $fileName The name of the file the query will be executing
239
- * @return null|string[]
259
+ * @return bool
240
260
*/
241
261
public function run ($ fileName ) {
242
262
if ($ this ->files ()->totalRootDirs () === 0 ) {
243
263
throw new FileQueryException (sprintf ('Query is trying to run with file "%s" but no root directories are configured. Make sure sure you have added at least one root directory with Files::addRootDir() before you run a query ' , $ fileName ));
244
264
}
245
265
246
266
$ this ->_queriedFileName = $ fileName ;
267
+
268
+ if ($ this ->getRootDirSelection ()->isInvalidated ()) {
269
+ $ this ->_cachedQueryRootDirs ;
270
+ }
271
+ if ($ this ->getChildDirSelection ()->isInvalidated ()) {
272
+ $ this ->_cachedQueryChildren ;
273
+ }
247
274
$ rootDirsSelection = $ this ->_getCachedRootDirSelection ();
248
275
249
276
$ this ->_currentQueryChildren = array ();
250
- foreach ($ this ->getCurrentChildDirSelection () as $ childDir ) {
251
- $ this ->_currentQueryChildren [$ childDir ->id ()] = $ this ->_processQueryChild ($ childDir , $ rootDirsSelection ); ;
277
+ foreach ($ this ->_getCachedChildDirSelection () as $ childDir ) {
278
+ $ this ->_currentQueryChildren [$ childDir ->id ()] = $ this ->_prepareQueryChild ($ childDir , $ rootDirsSelection );
252
279
}
253
280
$ this ->_hasRun = true ;
254
- $ meetsRequirements = $ this ->requirements ()->meetsRequirements ($ this , false );
281
+ $ meetsRequirements = $ this ->requirements ()->meetsRequirements (false );
255
282
if ($ meetsRequirements !== true ) {
256
283
$ this ->_queryError = $ meetsRequirements ;
257
284
}
258
285
return $ this ->_queryError === null ;
259
286
}
260
287
288
+ protected function _prepareQueryChild (ChildDir $ childDir , $ rootDirs ) {
289
+ $ queryChild = $ this ->_getQueryChild ($ childDir );
290
+ $ queryChild ->reset ();
291
+ $ queryChild ->setRootDirs ($ rootDirs );
292
+
293
+ return $ queryChild ;
294
+ }
295
+
261
296
public function load () {
262
297
$ this ->_hasRunCheck ();
263
298
@@ -279,26 +314,24 @@ public function load() {
279
314
public function getCurrentRootDirSelection () {
280
315
return $ this ->getRootDirSelection ()->getSelection ($ this ->files ()->rootDirs ());
281
316
}
317
+ protected function _getCachedRootDirSelection () {
318
+ if ($ this ->_cachedQueryRootDirs === null ) {
319
+ $ this ->_cachedQueryRootDirs = $ this ->getCurrentRootDirSelection ();
320
+ }
321
+ return $ this ->_cachedQueryRootDirs ;
322
+ }
282
323
283
324
/**
284
325
* @return ChildDir[]
285
326
*/
286
327
public function getCurrentChildDirSelection () {
287
328
return $ this ->getChildDirSelection ()->getSelection ($ this ->files ()->childDirs ());
288
329
}
289
-
290
- protected function _getCachedRootDirSelection () {
291
- if ($ this ->_cachedQueryRootDirs === null ) {
292
- $ this ->_cachedQueryRootDirs = $ this ->getCurrentRootDirSelection ();
330
+ protected function _getCachedChildDirSelection () {
331
+ if ($ this ->_cachedQueryChildDirs === null ) {
332
+ $ this ->_cachedQueryChildDirs = $ this ->getCurrentChildDirSelection ();
293
333
}
294
- return $ this ->_cachedQueryRootDirs ;
295
- }
296
-
297
- protected function _processQueryChild (ChildDir $ childDir , $ rootSelection ) {
298
- $ queryChild = $ this ->_getQueryChild ($ childDir );
299
- $ queryChild ->reset ();
300
- $ queryChild ->setRootDirs ($ rootSelection );
301
- return $ queryChild ;
334
+ return $ this ->_cachedQueryChildDirs ;
302
335
}
303
336
304
337
/**
@@ -418,6 +451,10 @@ public function listBasePaths() {
418
451
return $ paths ;
419
452
}
420
453
454
+ /**
455
+ * @param string[] $paths
456
+ * @return string[]
457
+ */
421
458
public function reversePaths ($ paths ) {
422
459
return array_reverse ($ paths );
423
460
}
0 commit comments