Commit 6976d52a authored by alexpott's avatar alexpott

Issue #2096271 by chx: Make it easier to extend entity query.

parent 691a5762
......@@ -43,9 +43,11 @@ class Query extends QueryBase implements QueryInterface {
* The entity manager that stores all meta information.
* @param \Drupal\Core\Config\StorageInterface $config_storage
* The actual config storage which is used to list all config items.
* @param array $namespaces
* List of potential namespaces of the classes belonging to this query.
*/
function __construct($entity_type, $conjunction, EntityManager $entity_manager, StorageInterface $config_storage) {
parent::__construct($entity_type, $conjunction);
function __construct($entity_type, $conjunction, EntityManager $entity_manager, StorageInterface $config_storage, array $namespaces) {
parent::__construct($entity_type, $conjunction, $namespaces);
$this->entityManager = $entity_manager;
$this->configStorage = $config_storage;
}
......
......@@ -7,9 +7,9 @@
namespace Drupal\Core\Config\Entity\Query;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\Query\QueryBase;
use Drupal\Core\Entity\Query\QueryException;
use Drupal\Core\Entity\Query\QueryFactoryInterface;
......@@ -21,10 +21,17 @@ class QueryFactory implements QueryFactoryInterface {
/**
* The config storage used by the config entity query.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
* @var \Drupal\Core\Config\StorageInterface;
*/
protected $configStorage;
/**
* The namespace of this class, the parent class etc.
*
* @var array
*/
protected $namespaces;
/**
* Constructs a QueryFactory object.
*
......@@ -33,13 +40,14 @@ class QueryFactory implements QueryFactoryInterface {
*/
public function __construct(StorageInterface $config_storage) {
$this->configStorage = $config_storage;
$this->namespaces = QueryBase::getNamespaces($this);
}
/**
* {@inheritdoc}
*/
public function get($entity_type, $conjunction, EntityManager $entity_manager) {
return new Query($entity_type, $conjunction, $entity_manager, $this->configStorage);
return new Query($entity_type, $conjunction, $entity_manager, $this->configStorage, $this->namespaces);
}
/**
......
......@@ -118,12 +118,20 @@ abstract class QueryBase {
*/
protected $pager = array();
/**
* List of potential namespaces of the classes belonging to this query.
*
* @var array
*/
protected $namespaces = array();
/**
* Constructs this object.
*/
public function __construct($entity_type, $conjunction) {
public function __construct($entity_type, $conjunction, array $namespaces) {
$this->entityType = $entity_type;
$this->conjunction = $conjunction;
$this->namespaces = $namespaces;
$this->condition = $this->conditionGroupFactory($conjunction);
if ($this instanceof QueryAggregateInterface) {
$this->conditionAggregate = $this->conditionAggregateGroupFactory($conjunction);
......@@ -185,8 +193,10 @@ public function range($start = NULL, $length = NULL) {
* An object holding a group of conditions.
*/
protected function conditionGroupFactory($conjunction = 'AND') {
$class = static::getNamespace($this) . '\\Condition';
return new $class($conjunction, $this);
// As the factory classes hardwire QueryBase::getClass, it needs to be
// hardwired here too.
$class = QueryBase::getClass($this->namespaces, 'Condition');
return new $class($conjunction, $this, $this->namespaces);
}
/**
......@@ -422,17 +432,41 @@ protected function getAggregationAlias($field, $function) {
}
/**
* Returns the namespace of an object.
* Gets a list of namespaces of the ancestors of a class.
*
* @param $object
* The object.
* An object within a namespace.
*
* @return array
* A list containing the namespace of the class, the namespace of the
* parent of the class and so on and so on.
*/
public static function getNamespaces($object) {
$namespaces = array();
for ($class = get_class($object); $class; $class = get_parent_class($class)) {
$namespaces[] = substr($class, 0, strrpos($class, '\\'));
}
return $namespaces;
}
/**
* Finds a class in a list of namespaces.
*
* @param array $namespaces
* A list of namespaces.
* @param string $short_class_name
* A class name without namespace.
*
* @return string
* The namespace.
* The fully qualified name of the class.
*/
public static function getNamespace($object) {
$class = get_class($object);
return substr($class, 0, strrpos($class, '\\'));
public static function getClass(array $namespaces, $short_class_name) {
foreach ($namespaces as $namespace) {
$class = $namespace . '\\' . $short_class_name;
if (class_exists($class)) {
return $class;
}
}
}
}
......@@ -80,8 +80,8 @@ class Query extends QueryBase implements QueryInterface {
* @param \Drupal\Core\Database\Connection $connection
* The database connection to run the query against.
*/
public function __construct($entity_type, EntityManager $entity_manager, $conjunction, Connection $connection) {
parent::__construct($entity_type, $conjunction, $connection);
public function __construct($entity_type, EntityManager $entity_manager, $conjunction, Connection $connection, array $namespaces) {
parent::__construct($entity_type, $conjunction, $namespaces);
$this->entityManager = $entity_manager;
$this->connection = $connection;
}
......@@ -324,7 +324,7 @@ public function __clone() {
* The object that adds tables and fields to the SQL query object.
*/
public function getTables(SelectInterface $sql_query) {
$class = QueryBase::getNamespace($this) . '\\Tables';
$class = QueryBase::getClass($this->namespaces, 'Tables');
return new $class($sql_query);
}
......
......@@ -27,6 +27,13 @@ class QueryFactory implements QueryFactoryInterface {
*/
protected $connection;
/**
* The namespace of this class, the parent class etc.
*
* @var array
*/
protected $namespaces;
/**
* Constructs a QueryFactory object.
*
......@@ -35,6 +42,7 @@ class QueryFactory implements QueryFactoryInterface {
*/
public function __construct(Connection $connection) {
$this->connection = $connection;
$this->namespaces = QueryBase::getNamespaces($this);
}
/**
......@@ -50,8 +58,8 @@ public function __construct(Connection $connection) {
* The factored query.
*/
public function get($entity_type, $conjunction, EntityManager $entity_manager) {
$class = QueryBase::getNamespace($this) . '\\Query';
return new $class($entity_type, $entity_manager, $conjunction, $this->connection);
$class = QueryBase::getClass($this->namespaces, 'Query');
return new $class($entity_type, $entity_manager, $conjunction, $this->connection, $this->namespaces);
}
/**
......@@ -67,8 +75,8 @@ public function get($entity_type, $conjunction, EntityManager $entity_manager) {
* The factored aggregation query.
*/
public function getAggregate($entity_type, $conjunction, EntityManager $entity_manager) {
$class = QueryBase::getNamespace($this) . '\\QueryAggregate';
return new $class($entity_type, $entity_manager, $conjunction, $this->connection);
$class = QueryBase::getClass($this->namespaces, 'QueryAggregate');
return new $class($entity_type, $entity_manager, $conjunction, $this->connection, $this->namespaces);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment