Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
Locking/unlocking a selection so it
FilesQueryBuilder requires a FilesQuery instance instead of a Files
instance
  • Loading branch information
harm-less authored and harm-less committed May 22, 2015
1 parent 2109010 commit 40dcd7a
Show file tree
Hide file tree
Showing 16 changed files with 812 additions and 130 deletions.
12 changes: 6 additions & 6 deletions samples/FQ/Samples/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ function __construct() {
}

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

public function queryFile1FromRoot1AndFromChild1() {
$builder = new FilesQueryBuilder($this);
$builder = new FilesQueryBuilder($this->query());
return $builder->includeRootDirs('root1')->includeChildDirs('child1')->run('File1')->listPaths();
}
public function queryFile1InReverse() {
$builder = new FilesQueryBuilder($this);
$builder = new FilesQueryBuilder($this->query());
return $builder->reverse(true)->run('File1')->listPaths();
}

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

public function queryNonExistingFileWithRequirementLast() {
$builder = new FilesQueryBuilder($this);
$builder = new FilesQueryBuilder($this->query());
return $builder->addRequirement(FilesQueryRequirements::REQUIRE_LAST)->run('File1')->listPaths();
}
public function queryNonExistingFileWithRequirementAll() {
$builder = new FilesQueryBuilder($this);
$builder = new FilesQueryBuilder($this->query());
return $builder->addRequirement(FilesQueryRequirements::REQUIRE_ALL)->run('File1')->listPaths();
}
}
60 changes: 30 additions & 30 deletions src/FQ/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace FQ;

use FQ\Collections\ChildDirCollection;
use FQ\Collections\RootDirCollection;
use FQ\Dirs\ChildDir;
use FQ\Collections\ChildDirCollection;
use FQ\Dirs\RootDir;
use FQ\Dirs\ChildDir;
use FQ\Exceptions\FilesException;
use FQ\Query\FilesQuery;
use FQ\Query\FilesQueryRequirements;
Expand Down Expand Up @@ -274,6 +274,19 @@ public function getFullPath($rootDir, $childDir) {
return $childObj->fullAbsolutePath($rootDirObj);
}

public function prepareQuery(RootSelection $rootSelection = null, ChildSelection $childSelection = null, $reset = true, $resetSelection = true) {
$query = $this->query();
if ($reset) $query->reset();
if ($resetSelection) $query->resetSelection();
if ($rootSelection !== null) {
$query->setRootDirSelection($rootSelection);
}
if ($childSelection !== null) {
$query->setChildDirSelection($childSelection);
}
return $query;
}

/**
* @param string $fileName
* @param ChildSelection $childSelection
Expand All @@ -282,7 +295,7 @@ public function getFullPath($rootDir, $childDir) {
* @return false|string
*/
public function queryPath($fileName, ChildSelection $childSelection = null, RootSelection $rootSelection = null, $reverseLoad = false) {
$query = $this->query($rootSelection, $childSelection, true, true);
$query = $this->prepareQuery($rootSelection, $childSelection);
$query->reverse($reverseLoad);
$query->requirements(FilesQueryRequirements::REQUIRE_ONE);
if ($query->run($fileName)) {
Expand All @@ -309,15 +322,15 @@ public function loadFile($fileName, ChildSelection $childSelection = null, RootS

/**
* @param string $fileName
* @param RootSelection $rootDirs
* @param ChildSelection $children
* @param RootSelection $rootSelection
* @param ChildSelection $childSelection
* @param string $requiredLevels
* @param bool $reverseLoad
* @return bool
*/
public function loadFiles($fileName, RootSelection $rootDirs = null, ChildSelection $children = null, $requiredLevels = FilesQueryRequirements::REQUIRE_ONE, $reverseLoad = false)
public function loadFiles($fileName, RootSelection $rootSelection = null, ChildSelection $childSelection = null, $requiredLevels = FilesQueryRequirements::REQUIRE_ONE, $reverseLoad = false)
{
$query = $this->query($rootDirs, $children, true, true);
$query = $this->prepareQuery($rootSelection, $childSelection);
$query->reverse($reverseLoad);
$query->requirements($requiredLevels);
if ($query->run($fileName)) {
Expand All @@ -334,46 +347,33 @@ public function loadFiles($fileName, RootSelection $rootDirs = null, ChildSelect

/**
* @param string $fileName
* @param RootSelection $rootDirs
* @param ChildSelection $children
* @param RootSelection $rootSelection
* @param ChildSelection $childSelection
* @return array
*/
public function queryPaths($fileName, RootSelection $rootDirs = null, ChildSelection $children = null) {
$query = $this->query($rootDirs, $children, true, true);
public function queryPaths($fileName, RootSelection $rootSelection = null, ChildSelection $childSelection = null) {
$query = $this->prepareQuery($rootSelection, $childSelection);
$query->run($fileName);
return $query->listPaths();
}

/**
* @param string $fileName
* @param RootSelection $rootDirs
* @param ChildSelection $children
* @param RootSelection $rootSelection
* @param ChildSelection $childSelection
* @return bool
*/
public function fileExists($fileName, RootSelection $rootDirs = null, ChildSelection $children = null) {
$query = $this->query($rootDirs, $children, true, true);
public function fileExists($fileName, RootSelection $rootSelection = null, ChildSelection $childSelection = null) {
$query = $this->prepareQuery($rootSelection, $childSelection);
$query->run($fileName);
return $query->hasPaths();
}

/**
* @param RootSelection $rootDirs
* @param ChildSelection $children
* @param bool $resetQuery
* @param bool $resetSelection
* @return FilesQuery
*/
public function query(RootSelection $rootDirs = null, ChildSelection $children = null, $resetQuery = true, $resetSelection = false) {
$query = $this->_query;
if ($resetQuery) {
$query->reset();
}
if ($resetSelection) {
$query->resetSelection();
}
if ($rootDirs) $query->setRootDirSelection($rootDirs);
if ($children) $query->setChildDirSelection($children);
return $query;
public function query() {
return $this->_query;
}

/**
Expand Down
89 changes: 63 additions & 26 deletions src/FQ/query/FilesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

namespace FQ\Query;

use FQ\Dirs\ChildDir;
use FQ\Dirs\RootDir;
use FQ\Dirs\ChildDir;
use FQ\Exceptions\FileQueryException;
use FQ\Files;
use FQ\Query\Selection\ChildSelection;
use FQ\Query\Selection\RootSelection;

/**
* Class FilesQuery
* @package FQ\Query
*
* @todo Ability to lock selections if you have a reoccurring queries
*/
class FilesQuery {

/**
Expand Down Expand Up @@ -40,6 +46,10 @@ class FilesQuery {
* @var RootDir[]
*/
private $_cachedQueryRootDirs;
/**
* @var ChildDir[]
*/
private $_cachedQueryChildDirs;

/**
* @var array
Expand Down Expand Up @@ -98,7 +108,6 @@ class FilesQuery {
* @param Files $files
*/
function __construct(Files $files) {
$this->_requirements = new FilesQueryRequirements();
$this->_files = $files;

$this->reset();
Expand Down Expand Up @@ -130,6 +139,9 @@ public function resetSelection() {
* @return FilesQueryRequirements
*/
public function requirements() {
if ($this->_requirements === null) {
$this->_requirements = new FilesQueryRequirements($this);
}
return $this->_requirements;
}

Expand Down Expand Up @@ -167,21 +179,29 @@ public function queryHasFilter($filter) {
return in_array($filter, $this->filters());
}

public function setRootDirSelection(RootSelection $selection) {
$this->_rootDirSelection = $selection;
/**
* @param RootSelection $rootDirSelection
*/
public function setRootDirSelection(RootSelection $rootDirSelection) {
$this->_cachedQueryRootDirs = null;
$this->_rootDirSelection = $rootDirSelection;
}
public function getRootDirSelection($createNewSelection = false) {
if ($this->_rootDirSelection === null || $createNewSelection) {
public function getRootDirSelection() {
if ($this->_rootDirSelection === null) {
$this->_rootDirSelection = new RootSelection();
}
return $this->_rootDirSelection;
}

public function setChildDirSelection(ChildSelection $selection) {
$this->_childDirSelection = $selection;
/**
* @param ChildSelection $childDirSelection
*/
public function setChildDirSelection(ChildSelection $childDirSelection) {
$this->_cachedQueryChildren = null;
$this->_childDirSelection = $childDirSelection;
}
public function getChildDirSelection($createNewSelection = false) {
if ($this->_childDirSelection === null || $createNewSelection) {
public function getChildDirSelection() {
if ($this->_childDirSelection === null) {
$this->_childDirSelection = new ChildSelection();
}
return $this->_childDirSelection;
Expand Down Expand Up @@ -236,28 +256,43 @@ protected function _hasRunCheck() {

/**
* @param string $fileName The name of the file the query will be executing
* @return null|string[]
* @return bool
*/
public function run($fileName) {
if ($this->files()->totalRootDirs() === 0) {
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));
}

$this->_queriedFileName = $fileName;

if ($this->getRootDirSelection()->isInvalidated()) {
$this->_cachedQueryRootDirs;
}
if ($this->getChildDirSelection()->isInvalidated()) {
$this->_cachedQueryChildren;
}
$rootDirsSelection = $this->_getCachedRootDirSelection();

$this->_currentQueryChildren = array();
foreach ($this->getCurrentChildDirSelection() as $childDir) {
$this->_currentQueryChildren[$childDir->id()] = $this->_processQueryChild($childDir, $rootDirsSelection);;
foreach ($this->_getCachedChildDirSelection() as $childDir) {
$this->_currentQueryChildren[$childDir->id()] = $this->_prepareQueryChild($childDir, $rootDirsSelection);
}
$this->_hasRun = true;
$meetsRequirements = $this->requirements()->meetsRequirements($this, false);
$meetsRequirements = $this->requirements()->meetsRequirements(false);
if ($meetsRequirements !== true) {
$this->_queryError = $meetsRequirements;
}
return $this->_queryError === null;
}

protected function _prepareQueryChild(ChildDir $childDir, $rootDirs) {
$queryChild = $this->_getQueryChild($childDir);
$queryChild->reset();
$queryChild->setRootDirs($rootDirs);

return $queryChild;
}

public function load() {
$this->_hasRunCheck();

Expand All @@ -279,26 +314,24 @@ public function load() {
public function getCurrentRootDirSelection() {
return $this->getRootDirSelection()->getSelection($this->files()->rootDirs());
}
protected function _getCachedRootDirSelection() {
if ($this->_cachedQueryRootDirs === null) {
$this->_cachedQueryRootDirs = $this->getCurrentRootDirSelection();
}
return $this->_cachedQueryRootDirs;
}

/**
* @return ChildDir[]
*/
public function getCurrentChildDirSelection() {
return $this->getChildDirSelection()->getSelection($this->files()->childDirs());
}

protected function _getCachedRootDirSelection() {
if ($this->_cachedQueryRootDirs === null) {
$this->_cachedQueryRootDirs = $this->getCurrentRootDirSelection();
protected function _getCachedChildDirSelection() {
if ($this->_cachedQueryChildDirs === null) {
$this->_cachedQueryChildDirs = $this->getCurrentChildDirSelection();
}
return $this->_cachedQueryRootDirs;
}

protected function _processQueryChild(ChildDir $childDir, $rootSelection) {
$queryChild = $this->_getQueryChild($childDir);
$queryChild->reset();
$queryChild->setRootDirs($rootSelection);
return $queryChild;
return $this->_cachedQueryChildDirs;
}

/**
Expand Down Expand Up @@ -418,6 +451,10 @@ public function listBasePaths() {
return $paths;
}

/**
* @param string[] $paths
* @return string[]
*/
public function reversePaths($paths) {
return array_reverse($paths);
}
Expand Down
Loading

0 comments on commit 40dcd7a

Please sign in to comment.