Commit 1745bd21 authored by alexpott's avatar alexpott

Issue #1978870 by damiankloip, andypost, dawehner: Add an EntityStorageControllerBase.

parent 40746685
......@@ -9,9 +9,8 @@
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityControllerInterface;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Entity\EntityStorageControllerBase;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Config\StorageInterface;
......@@ -32,39 +31,7 @@
* after the config_prefix in a config name forms the entity ID. Additional or
* custom suffixes are not possible.
*/
class ConfigStorageController implements EntityStorageControllerInterface, EntityControllerInterface {
/**
* Entity type for this controller instance.
*
* @var string
*/
protected $entityType;
/**
* Array of information about the entity.
*
* @var array
*
* @see entity_get_info()
*/
protected $entityInfo;
/**
* Additional arguments to pass to hook_TYPE_load().
*
* Set before calling Drupal\Core\Config\Entity\ConfigStorageController::attachLoad().
*
* @var array
*/
protected $hookLoadArguments;
/**
* Name of the entity's ID field in the entity database table.
*
* @var string
*/
protected $idKey;
class ConfigStorageController extends EntityStorageControllerBase {
/**
* Name of the entity's UUID property.
......@@ -107,9 +74,8 @@ class ConfigStorageController implements EntityStorageControllerInterface, Entit
* The config storage service.
*/
public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage) {
$this->entityType = $entity_type;
$this->entityInfo = $entity_info;
$this->hookLoadArguments = array();
parent::__construct($entity_type, $entity_info);
$this->idKey = $this->entityInfo['entity_keys']['id'];
if (isset($this->entityInfo['entity_keys']['status'])) {
......@@ -135,14 +101,6 @@ public static function createInstance(ContainerInterface $container, $entity_typ
);
}
/**
* Implements Drupal\Core\Entity\EntityStorageControllerInterface::resetCache().
*/
public function resetCache(array $ids = NULL) {
// The configuration system is fast enough and/or implements its own
// (advanced) caching mechanism already.
}
/**
* Implements Drupal\Core\Entity\EntityStorageControllerInterface::load().
*/
......@@ -182,15 +140,6 @@ public function load(array $ids = NULL) {
return $entities;
}
/**
* Implements \Drupal\Core\Entity\EntityStorageControllerInterface::loadUnchanged()
*/
public function loadUnchanged($id) {
$this->resetCache(array($id));
$result = $this->load(array($id));
return reset($result);
}
/**
* Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadRevision().
*/
......
......@@ -14,7 +14,6 @@
use Drupal\Component\Uuid\Uuid;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -25,30 +24,7 @@
* This class can be used as-is by most simple entity types. Entity types
* requiring special handling can extend the class.
*/
class DatabaseStorageController implements EntityStorageControllerInterface, EntityControllerInterface {
/**
* Static cache of entities.
*
* @var array
*/
protected $entityCache;
/**
* Entity type for this controller instance.
*
* @var string
*/
protected $entityType;
/**
* Array of information about the entity.
*
* @var array
*
* @see entity_get_info()
*/
protected $entityInfo;
class DatabaseStorageController extends EntityStorageControllerBase {
/**
* An array of field information, i.e. containing definitions.
......@@ -66,31 +42,6 @@ class DatabaseStorageController implements EntityStorageControllerInterface, Ent
*/
protected $fieldDefinitions;
/**
* Additional arguments to pass to hook_TYPE_load().
*
* Set before calling Drupal\Core\Entity\DatabaseStorageController::attachLoad().
*
* @var array
*/
protected $hookLoadArguments;
/**
* Name of the entity's ID field in the entity database table.
*
* @var string
*/
protected $idKey;
/**
* Name of entity's UUID database table field, if it supports UUIDs.
*
* Has the value FALSE if this entity does not use UUIDs.
*
* @var string
*/
protected $uuidKey;
/**
* Name of entity's revision database table field, if it supports revisions.
*
......@@ -145,11 +96,9 @@ public static function createInstance(ContainerInterface $container, $entity_typ
* The database connection to be used.
*/
public function __construct($entity_type, array $entity_info, Connection $database) {
parent::__construct($entity_type, $entity_info);
$this->database = $database;
$this->entityType = $entity_type;
$this->entityInfo = $entity_info;
$this->entityCache = array();
$this->hookLoadArguments = array();
// Check if the entity type supports IDs.
if (isset($this->entityInfo['entity_keys']['id'])) {
......@@ -175,25 +124,6 @@ public function __construct($entity_type, array $entity_info, Connection $databa
else {
$this->revisionKey = FALSE;
}
// Check if the entity type supports static caching of loaded entities.
$this->cache = !empty($this->entityInfo['static_cache']);
}
/**
* Implements \Drupal\Core\Entity\EntityStorageControllerInterface::resetCache().
*/
public function resetCache(array $ids = NULL) {
if (isset($ids)) {
foreach ($ids as $id) {
unset($this->entityCache[$id]);
}
}
else {
$this->entityCache = array();
$this->entityFieldInfo = NULL;
$this->fieldDefinitions = array();
}
}
/**
......@@ -263,15 +193,6 @@ public function load(array $ids = NULL) {
return $entities;
}
/**
* Implements \Drupal\Core\Entity\EntityStorageControllerInterface::loadUnchanged()
*/
public function loadUnchanged($id) {
$this->resetCache(array($id));
$result = $this->load(array($id));
return reset($result);
}
/**
* Implements \Drupal\Core\Entity\EntityStorageControllerInterface::loadRevision().
*/
......@@ -445,34 +366,6 @@ protected function attachLoad(&$queried_entities, $load_revision = FALSE) {
}
}
/**
* Gets entities from the static cache.
*
* @param $ids
* If not empty, return entities that match these IDs.
*
* @return
* Array of entities from the entity cache.
*/
protected function cacheGet($ids) {
$entities = array();
// Load any available entities from the internal cache.
if (!empty($this->entityCache)) {
$entities += array_intersect_key($this->entityCache, array_flip($ids));
}
return $entities;
}
/**
* Stores entities in the static entity cache.
*
* @param $entities
* Entities to store in the cache.
*/
protected function cacheSet($entities) {
$this->entityCache += $entities;
}
/**
* Implements \Drupal\Core\Entity\EntityStorageControllerInterface::create().
*/
......
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityStorageControllerBase.
*/
namespace Drupal\Core\Entity;
/**
* A base entity storage controller class.
*/
abstract class EntityStorageControllerBase implements EntityStorageControllerInterface, EntityControllerInterface {
/**
* Static cache of entities.
*
* @var array
*/
protected $entityCache = array();
/**
* Whether this entity type should use the static cache.
*
* Set by entity info.
*
* @var boolean
*/
protected $cache;
/**
* Entity type for this controller instance.
*
* @var string
*/
protected $entityType;
/**
* Array of information about the entity.
*
* @var array
*
* @see entity_get_info()
*/
protected $entityInfo;
/**
* Additional arguments to pass to hook_TYPE_load().
*
* Set before calling Drupal\Core\Entity\DatabaseStorageController::attachLoad().
*
* @var array
*/
protected $hookLoadArguments = array();
/**
* Name of the entity's ID field in the entity database table.
*
* @var string
*/
protected $idKey;
/**
* Name of entity's UUID database table field, if it supports UUIDs.
*
* Has the value FALSE if this entity does not use UUIDs.
*
* @var string
*/
protected $uuidKey;
/**
* Constructs an EntityStorageControllerBase instance.
*
* @param string $entity_type
* The entity type for which the instance is created.
* @param array $entity_info
* An array of entity info for the entity type.
*/
public function __construct($entity_type, $entity_info) {
$this->entityType = $entity_type;
$this->entityInfo = $entity_info;
// Check if the entity type supports static caching of loaded entities.
$this->cache = !empty($this->entityInfo['static_cache']);
}
/**
* {@inheritdoc}
*/
public function loadUnchanged($id) {
$this->resetCache(array($id));
$result = $this->load(array($id));
return reset($result);
}
/**
* {@inheritdoc}
*/
public function resetCache(array $ids = NULL) {
if ($this->cache && isset($ids)) {
foreach ($ids as $id) {
unset($this->entityCache[$id]);
}
}
else {
$this->entityCache = array();
}
}
/**
* Gets entities from the static cache.
*
* @param $ids
* If not empty, return entities that match these IDs.
*
* @return
* Array of entities from the entity cache.
*/
protected function cacheGet($ids) {
$entities = array();
// Load any available entities from the internal cache.
if ($this->cache && !empty($this->entityCache)) {
$entities += array_intersect_key($this->entityCache, array_flip($ids));
}
return $entities;
}
/**
* Stores entities in the static entity cache.
*
* @param $entities
* Entities to store in the cache.
*/
protected function cacheSet($entities) {
if ($this->cache) {
$this->entityCache += $entities;
}
}
}
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