-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QueryCollection and performance gains
- Added a QueryCollection that holds multiple queries you can retrieved by an ID - The root -and child dirs selection can now be cached to gain performance
- Loading branch information
harm-less
authored and
harm-less
committed
May 27, 2015
1 parent
40dcd7a
commit 82716ae
Showing
12 changed files
with
402 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace FQ\Collections\Query; | ||
|
||
use FQ\Exceptions\QueryCollectionException; | ||
use FQ\Files; | ||
use FQ\Query\FilesQuery; | ||
|
||
class QueryCollection { | ||
|
||
/** | ||
* @var FilesQuery[] | ||
*/ | ||
private $_queries; | ||
|
||
function __construct() { | ||
$this->_queries = array(); | ||
} | ||
|
||
/** | ||
* @param $id | ||
* @param FilesQuery $query | ||
* @return FilesQuery | ||
*/ | ||
public function addQuery($id, FilesQuery $query) { | ||
if ($this->queryExists($id)) { | ||
throw new QueryCollectionException(sprintf('Query with id "%s" already defined', $id)); | ||
} | ||
$this->_queries[$id] = $query; | ||
return $query; | ||
} | ||
|
||
/** | ||
* @param string|FilesQuery $query | ||
* @return FilesQuery|null | ||
*/ | ||
public function getQuery($query) { | ||
if (is_string($query)) { | ||
return $this->getQueryById($query); | ||
} | ||
else if (is_object($query) && $this->isInCollection($query)) { | ||
return $query; | ||
} | ||
return null; | ||
} | ||
|
||
public function getQueryById($id) { | ||
if ($this->queryExists($id)) { | ||
return $this->_queries[$id]; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @return bool | ||
*/ | ||
public function removeQuery($id) { | ||
if ($this->queryExists($id)) { | ||
unset($this->_queries[$id]); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function removeAllQueries() { | ||
$this->_queries = array(); | ||
} | ||
|
||
/** | ||
* @param FilesQuery $query Query that will be checked | ||
* @return bool Returns true if dir is in the collection and false when it's not | ||
*/ | ||
public function isInCollection(FilesQuery $query) { | ||
foreach ($this->_queries as $queryTemp) { | ||
if ($query === $queryTemp) return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @param $id | ||
* @return bool | ||
*/ | ||
public function queryExists($id) { | ||
return isset($this->_queries[$id]); | ||
} | ||
|
||
/** | ||
* @return FilesQuery[] | ||
*/ | ||
public function queries() { | ||
return $this->_queries; | ||
} | ||
|
||
/** | ||
* @return int Total amount of queries in this collection | ||
*/ | ||
public function totalQueries() { | ||
return count($this->queries()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace FQ\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
class QueryCollectionException extends RuntimeException implements FQExceptionInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace FQ\Utils; | ||
|
||
use FQ\Dirs\Dir; | ||
|
||
class Dirs { | ||
/** | ||
* @param Dir[] $array1 | ||
* @param Dir[] $array2 | ||
* @return bool Returns true if the array contains equal dirs in the same order. Otherwise returns false. | ||
*/ | ||
public static function equalDirs($array1, $array2) { | ||
if (count($array1) !== count($array2)) return false; | ||
foreach ($array1 as $index => $dir) { | ||
if ($array2[$index] !== $dir) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
tests/FQ/Tests/Collections/Query/AbstractQueryCollectionTests.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace FQ\Tests\Collections\Query; | ||
|
||
use FQ\Collections\Query\QueryCollection; | ||
use FQ\Query\FilesQuery; | ||
use FQ\Tests\AbstractFQTest; | ||
|
||
class AbstractQueryCollectionTests extends AbstractFQTest | ||
{ | ||
protected $_queryCollection; | ||
|
||
const DEFAULT_QUERY_ID = 'query1'; | ||
const SECOND_QUERY_ID = 'query2'; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
// Create a new FQ app, | ||
// since we need one pretty much everywhere | ||
$this->_queryCollection = $this->_createNewQueryCollection(); | ||
} | ||
|
||
/** | ||
* @return QueryCollection | ||
*/ | ||
protected function queryCollection() | ||
{ | ||
return $this->_queryCollection; | ||
} | ||
|
||
/** | ||
* @return QueryCollection | ||
*/ | ||
protected function _createNewQueryCollection() | ||
{ | ||
return new QueryCollection($this->_fqApp); | ||
} | ||
|
||
protected function _addQueryToCollection($id = self::DEFAULT_QUERY_ID, FilesQuery $query = null) | ||
{ | ||
$query = $query === null ? new FilesQuery($this->_fqApp) : $query; | ||
$collection = $this->queryCollection(); | ||
return $collection->addQuery($id, $query); | ||
} | ||
|
||
function testIgnore() { | ||
|
||
} | ||
} |
Oops, something went wrong.