Skip to content

Commit 40dcd7a

Browse files
harm-lessharm-less
harm-less
authored and
harm-less
committed
Improvements
Locking/unlocking a selection so it FilesQueryBuilder requires a FilesQuery instance instead of a Files instance
1 parent 2109010 commit 40dcd7a

16 files changed

+812
-130
lines changed

samples/FQ/Samples/Simple.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,30 @@ function __construct() {
2020
}
2121

2222
public function queryFile1FromChild1() {
23-
$builder = new FilesQueryBuilder($this);
23+
$builder = new FilesQueryBuilder($this->query());
2424
return $builder->includeChildDirs('child1')->run('File1')->listPaths();
2525
}
2626

2727
public function queryFile1FromRoot1AndFromChild1() {
28-
$builder = new FilesQueryBuilder($this);
28+
$builder = new FilesQueryBuilder($this->query());
2929
return $builder->includeRootDirs('root1')->includeChildDirs('child1')->run('File1')->listPaths();
3030
}
3131
public function queryFile1InReverse() {
32-
$builder = new FilesQueryBuilder($this);
32+
$builder = new FilesQueryBuilder($this->query());
3333
return $builder->reverse(true)->run('File1')->listPaths();
3434
}
3535

3636
public function queryNonExistingFileWithRequirementOne() {
37-
$builder = new FilesQueryBuilder($this);
37+
$builder = new FilesQueryBuilder($this->query());
3838
return $builder->addRequirement(FilesQueryRequirements::REQUIRE_ONE)->run('File1')->listPaths();
3939
}
4040

4141
public function queryNonExistingFileWithRequirementLast() {
42-
$builder = new FilesQueryBuilder($this);
42+
$builder = new FilesQueryBuilder($this->query());
4343
return $builder->addRequirement(FilesQueryRequirements::REQUIRE_LAST)->run('File1')->listPaths();
4444
}
4545
public function queryNonExistingFileWithRequirementAll() {
46-
$builder = new FilesQueryBuilder($this);
46+
$builder = new FilesQueryBuilder($this->query());
4747
return $builder->addRequirement(FilesQueryRequirements::REQUIRE_ALL)->run('File1')->listPaths();
4848
}
4949
}

src/FQ/Files.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace FQ;
44

5-
use FQ\Collections\ChildDirCollection;
65
use FQ\Collections\RootDirCollection;
7-
use FQ\Dirs\ChildDir;
6+
use FQ\Collections\ChildDirCollection;
87
use FQ\Dirs\RootDir;
8+
use FQ\Dirs\ChildDir;
99
use FQ\Exceptions\FilesException;
1010
use FQ\Query\FilesQuery;
1111
use FQ\Query\FilesQueryRequirements;
@@ -274,6 +274,19 @@ public function getFullPath($rootDir, $childDir) {
274274
return $childObj->fullAbsolutePath($rootDirObj);
275275
}
276276

277+
public function prepareQuery(RootSelection $rootSelection = null, ChildSelection $childSelection = null, $reset = true, $resetSelection = true) {
278+
$query = $this->query();
279+
if ($reset) $query->reset();
280+
if ($resetSelection) $query->resetSelection();
281+
if ($rootSelection !== null) {
282+
$query->setRootDirSelection($rootSelection);
283+
}
284+
if ($childSelection !== null) {
285+
$query->setChildDirSelection($childSelection);
286+
}
287+
return $query;
288+
}
289+
277290
/**
278291
* @param string $fileName
279292
* @param ChildSelection $childSelection
@@ -282,7 +295,7 @@ public function getFullPath($rootDir, $childDir) {
282295
* @return false|string
283296
*/
284297
public function queryPath($fileName, ChildSelection $childSelection = null, RootSelection $rootSelection = null, $reverseLoad = false) {
285-
$query = $this->query($rootSelection, $childSelection, true, true);
298+
$query = $this->prepareQuery($rootSelection, $childSelection);
286299
$query->reverse($reverseLoad);
287300
$query->requirements(FilesQueryRequirements::REQUIRE_ONE);
288301
if ($query->run($fileName)) {
@@ -309,15 +322,15 @@ public function loadFile($fileName, ChildSelection $childSelection = null, RootS
309322

310323
/**
311324
* @param string $fileName
312-
* @param RootSelection $rootDirs
313-
* @param ChildSelection $children
325+
* @param RootSelection $rootSelection
326+
* @param ChildSelection $childSelection
314327
* @param string $requiredLevels
315328
* @param bool $reverseLoad
316329
* @return bool
317330
*/
318-
public function loadFiles($fileName, RootSelection $rootDirs = null, ChildSelection $children = null, $requiredLevels = FilesQueryRequirements::REQUIRE_ONE, $reverseLoad = false)
331+
public function loadFiles($fileName, RootSelection $rootSelection = null, ChildSelection $childSelection = null, $requiredLevels = FilesQueryRequirements::REQUIRE_ONE, $reverseLoad = false)
319332
{
320-
$query = $this->query($rootDirs, $children, true, true);
333+
$query = $this->prepareQuery($rootSelection, $childSelection);
321334
$query->reverse($reverseLoad);
322335
$query->requirements($requiredLevels);
323336
if ($query->run($fileName)) {
@@ -334,46 +347,33 @@ public function loadFiles($fileName, RootSelection $rootDirs = null, ChildSelect
334347

335348
/**
336349
* @param string $fileName
337-
* @param RootSelection $rootDirs
338-
* @param ChildSelection $children
350+
* @param RootSelection $rootSelection
351+
* @param ChildSelection $childSelection
339352
* @return array
340353
*/
341-
public function queryPaths($fileName, RootSelection $rootDirs = null, ChildSelection $children = null) {
342-
$query = $this->query($rootDirs, $children, true, true);
354+
public function queryPaths($fileName, RootSelection $rootSelection = null, ChildSelection $childSelection = null) {
355+
$query = $this->prepareQuery($rootSelection, $childSelection);
343356
$query->run($fileName);
344357
return $query->listPaths();
345358
}
346359

347360
/**
348361
* @param string $fileName
349-
* @param RootSelection $rootDirs
350-
* @param ChildSelection $children
362+
* @param RootSelection $rootSelection
363+
* @param ChildSelection $childSelection
351364
* @return bool
352365
*/
353-
public function fileExists($fileName, RootSelection $rootDirs = null, ChildSelection $children = null) {
354-
$query = $this->query($rootDirs, $children, true, true);
366+
public function fileExists($fileName, RootSelection $rootSelection = null, ChildSelection $childSelection = null) {
367+
$query = $this->prepareQuery($rootSelection, $childSelection);
355368
$query->run($fileName);
356369
return $query->hasPaths();
357370
}
358371

359372
/**
360-
* @param RootSelection $rootDirs
361-
* @param ChildSelection $children
362-
* @param bool $resetQuery
363-
* @param bool $resetSelection
364373
* @return FilesQuery
365374
*/
366-
public function query(RootSelection $rootDirs = null, ChildSelection $children = null, $resetQuery = true, $resetSelection = false) {
367-
$query = $this->_query;
368-
if ($resetQuery) {
369-
$query->reset();
370-
}
371-
if ($resetSelection) {
372-
$query->resetSelection();
373-
}
374-
if ($rootDirs) $query->setRootDirSelection($rootDirs);
375-
if ($children) $query->setChildDirSelection($children);
376-
return $query;
375+
public function query() {
376+
return $this->_query;
377377
}
378378

379379
/**

src/FQ/query/FilesQuery.php

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
namespace FQ\Query;
44

5-
use FQ\Dirs\ChildDir;
65
use FQ\Dirs\RootDir;
6+
use FQ\Dirs\ChildDir;
77
use FQ\Exceptions\FileQueryException;
88
use FQ\Files;
99
use FQ\Query\Selection\ChildSelection;
1010
use 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+
*/
1218
class 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

Comments
 (0)