22
33namespace FQ \Query ;
44
5- use FQ \Dirs \ChildDir ;
65use FQ \Dirs \RootDir ;
6+ use FQ \Dirs \ChildDir ;
77use FQ \Exceptions \FileQueryException ;
88use FQ \Files ;
99use FQ \Query \Selection \ChildSelection ;
1010use FQ \Query \Selection \RootSelection ;
1111
12+ /**
13+ * Class FilesQuery
14+ * @package FQ\Query
15+ *
16+ * @todo Ability to lock selections if you have a reoccurring queries
17+ */
1218class FilesQuery {
1319
1420 /**
@@ -40,6 +46,10 @@ class FilesQuery {
4046 * @var RootDir[]
4147 */
4248 private $ _cachedQueryRootDirs ;
49+ /**
50+ * @var ChildDir[]
51+ */
52+ private $ _cachedQueryChildDirs ;
4353
4454 /**
4555 * @var array
@@ -98,7 +108,6 @@ class FilesQuery {
98108 * @param Files $files
99109 */
100110 function __construct (Files $ files ) {
101- $ this ->_requirements = new FilesQueryRequirements ();
102111 $ this ->_files = $ files ;
103112
104113 $ this ->reset ();
@@ -130,6 +139,9 @@ public function resetSelection() {
130139 * @return FilesQueryRequirements
131140 */
132141 public function requirements () {
142+ if ($ this ->_requirements === null ) {
143+ $ this ->_requirements = new FilesQueryRequirements ($ this );
144+ }
133145 return $ this ->_requirements ;
134146 }
135147
@@ -167,21 +179,29 @@ public function queryHasFilter($filter) {
167179 return in_array ($ filter , $ this ->filters ());
168180 }
169181
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 ;
172188 }
173- public function getRootDirSelection ($ createNewSelection = false ) {
174- if ($ this ->_rootDirSelection === null || $ createNewSelection ) {
189+ public function getRootDirSelection () {
190+ if ($ this ->_rootDirSelection === null ) {
175191 $ this ->_rootDirSelection = new RootSelection ();
176192 }
177193 return $ this ->_rootDirSelection ;
178194 }
179195
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 ;
182202 }
183- public function getChildDirSelection ($ createNewSelection = false ) {
184- if ($ this ->_childDirSelection === null || $ createNewSelection ) {
203+ public function getChildDirSelection () {
204+ if ($ this ->_childDirSelection === null ) {
185205 $ this ->_childDirSelection = new ChildSelection ();
186206 }
187207 return $ this ->_childDirSelection ;
@@ -236,28 +256,43 @@ protected function _hasRunCheck() {
236256
237257 /**
238258 * @param string $fileName The name of the file the query will be executing
239- * @return null|string[]
259+ * @return bool
240260 */
241261 public function run ($ fileName ) {
242262 if ($ this ->files ()->totalRootDirs () === 0 ) {
243263 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 ));
244264 }
245265
246266 $ this ->_queriedFileName = $ fileName ;
267+
268+ if ($ this ->getRootDirSelection ()->isInvalidated ()) {
269+ $ this ->_cachedQueryRootDirs ;
270+ }
271+ if ($ this ->getChildDirSelection ()->isInvalidated ()) {
272+ $ this ->_cachedQueryChildren ;
273+ }
247274 $ rootDirsSelection = $ this ->_getCachedRootDirSelection ();
248275
249276 $ 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 );
252279 }
253280 $ this ->_hasRun = true ;
254- $ meetsRequirements = $ this ->requirements ()->meetsRequirements ($ this , false );
281+ $ meetsRequirements = $ this ->requirements ()->meetsRequirements (false );
255282 if ($ meetsRequirements !== true ) {
256283 $ this ->_queryError = $ meetsRequirements ;
257284 }
258285 return $ this ->_queryError === null ;
259286 }
260287
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+
261296 public function load () {
262297 $ this ->_hasRunCheck ();
263298
@@ -279,26 +314,24 @@ public function load() {
279314 public function getCurrentRootDirSelection () {
280315 return $ this ->getRootDirSelection ()->getSelection ($ this ->files ()->rootDirs ());
281316 }
317+ protected function _getCachedRootDirSelection () {
318+ if ($ this ->_cachedQueryRootDirs === null ) {
319+ $ this ->_cachedQueryRootDirs = $ this ->getCurrentRootDirSelection ();
320+ }
321+ return $ this ->_cachedQueryRootDirs ;
322+ }
282323
283324 /**
284325 * @return ChildDir[]
285326 */
286327 public function getCurrentChildDirSelection () {
287328 return $ this ->getChildDirSelection ()->getSelection ($ this ->files ()->childDirs ());
288329 }
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 ();
293333 }
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 ;
302335 }
303336
304337 /**
@@ -418,6 +451,10 @@ public function listBasePaths() {
418451 return $ paths ;
419452 }
420453
454+ /**
455+ * @param string[] $paths
456+ * @return string[]
457+ */
421458 public function reversePaths ($ paths ) {
422459 return array_reverse ($ paths );
423460 }
0 commit comments