Commit 69e38f03 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch
Browse files

Injecting services

parent c683d927
<?php
/**
* @file
* Contains Drupal\typed_entity\Entity\EntityManager.
*/
namespace Drupal\typed_entity\Entity;
class EntityManager implements EntityManagerInterface {
/**
* {@inheritdoc}
*/
public function entityLoad($entity_type, $ids = FALSE, $conditions = array(), $reset = FALSE) {
return entity_load($entity_type, $ids, $conditions, $reset);
}
/**
* {@inheritdoc}
*/
public function entityExtractIds($entity_type, $entity) {
return entity_extract_ids($entity_type, $entity);
}
}
<?php
/**
* @file
* Contains Drupal\typed_entity\Entity\EntityManagerInterface.
*/
namespace Drupal\typed_entity\Entity;
interface EntityManagerInterface {
/**
* Loads a list of entities.
*
* @see entity_load().
*/
public function entityLoad($entity_type, $ids = FALSE, $conditions = array(), $reset = FALSE);
/**
* Extracts entity ids.
*
* @see entity_extract_ids().
*/
public function entityExtractIds($entity_type, $entity);
}
......@@ -16,9 +16,16 @@ class TypedEntityServiceProvider implements ServiceProviderInterface {
*/
public function getContainerDefinition() {
$parameters = $services = array();
$services['entity.manager'] = array(
'class' => '\Drupal\typed_entity\Entity\EntityManager',
);
$services['entity.wrapper'] = array(
'class' => '\Drupal\typed_entity\Entity\EntityWrapperService',
);
$services['system.cache.manager'] = array(
'class' => '\Drupal\typed_entity\System\CacheManager',
);
return array(
'parameters' => $parameters,
'services' => $services,
......
<?php
/**
* @file
* Contains Drupal\typed_entity\System\MemoryCacheController.
*/
namespace Drupal\typed_entity\System;
class ArrayCacheController implements \DrupalCacheInterface {
/**
* The cache bin.
*
* @var string
*/
protected $bin;
/**
* The array of data.
*
* @var array
*/
protected $data;
/**
* Constructs a MemoryCacheController object.
*
* @param $bin
* The cache bin for which the object is created.
*/
public function __construct($bin) {
$this->bin = $bin;
$this->data = array($bin => array());
}
/**
* {@inheritdoc}
*/
function get($cid) {
return isset($this->data[$this->bin][$cid]) ? $this::response(array($this->data[$this->bin][$cid])) : NULL;
}
/**
* {@inheritdoc}
*/
function getMultiple(&$cids) {
$output = array();
foreach ($cids as $index => $cid) {
if (empty($this->data[$this->bin][$cid])) {
unset($cids[$index]);
continue;
}
$output[] = $this->data[$this->bin][$cid];
}
return empty($output) ? NULL : $this::response($output);
}
/**
* {@inheritdoc}
*/
function set($cid, $data, $expire = CACHE_PERMANENT) {
$this->data[$this->bin][$cid] = $data;
}
/**
* {@inheritdoc}
*/
function clear($cid = NULL, $wildcard = FALSE) {
// The wildcard is ignored.
unset($this->data[$this->bin][$cid]);
}
/**
* {@inheritdoc}
*/
function isEmpty() {
return empty($this->data[$this->bin]);
}
/**
* Builds the response object based on a list of values.
*
* @param mixed $values
* The value(s) to return.
*
* @return object
* The cache object.
*/
protected static function response($values) {
return (object) array(
'data' => $values,
);
}
}
<?php
/**
* @file
* Contains Drupal\typed_entity\System\CacheManager.
*/
namespace Drupal\typed_entity\System;
class CacheManager implements CacheManagerInterface {
/**
* {@inheritdoc}
*/
public static function getController($bin = 'cache') {
return _cache_get_object($bin);
}
}
<?php
/**
* @file
* Contains Drupal\typed_entity\System\CacheManager.
*/
namespace Drupal\typed_entity\System;
interface CacheManagerInterface {
/**
* Returns the DrupalCacheInterface object.
*
* @param string $bin
* The cache bin.
*
* @return \DrupalCacheInterface
* The cache controller.
*/
public static function getController($bin = 'cache');
}
......@@ -7,7 +7,9 @@
namespace Drupal\typed_entity\TypedEntity;
use Drupal\service_container\DependencyInjection\Container;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\typed_entity\Entity\EntityWrapperServiceInterface;
use Drupal\typed_entity\Entity\EntityManagerInterface;
use Drupal\typed_entity\Exception\TypedEntityException;
class TypedEntity implements TypedEntityInterface {
......@@ -47,13 +49,36 @@ class TypedEntity implements TypedEntityInterface {
*/
protected $wrapper;
/**
* The entity manager.
*
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* The entity metadata service.
*
* @var EntityWrapperServiceInterface
*/
protected $entityMetadataService;
/**
* The module handler service.
*
* @var ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a TypedEntity object.
*
* @param Container $container
* The service container.
* @param string $entity_type
* The type of the entity.
* @param EntityManagerInterface $entity_manager
* The service to interact with the entity system.
* @param EntityWrapperServiceInterface $entity_metadata_service
* The service that wraps an entity.
* @param ModuleHandlerInterface $module_handler
* The service to interact with modules.
* @param int $entity_id
* The entity ID.
* @param object $entity
......@@ -64,7 +89,7 @@ class TypedEntity implements TypedEntityInterface {
* @throws \Drupal\typed_entity\Exception\TypedEntityException
* @throws \EntityMalformedException
*/
public function __construct($entity_type, $entity_id = NULL, $entity = NULL, $bundle = NULL) {
public function __construct(EntityManagerInterface $entity_manager, EntityWrapperServiceInterface $entity_metadata_service, ModuleHandlerInterface $module_handler, $entity_type, $entity_id = NULL, $entity = NULL, $bundle = NULL) {
if (empty($entity_type)) {
throw new TypedEntityException('You need to provide the entity type for the TypedEntity.');
}
......@@ -75,6 +100,9 @@ class TypedEntity implements TypedEntityInterface {
$this->entityId = $entity_id;
$this->entity = $entity;
$this->bundle = $bundle;
$this->entityManager = $entity_manager;
$this->entityMetadataService = $entity_metadata_service;
$this->moduleHandler = $module_handler;
}
/**
......@@ -88,7 +116,7 @@ class TypedEntity implements TypedEntityInterface {
// This means that somehow we do not have neither entity nor entity ID.
throw new TypedEntityException('You need to provide the fully loaded entity or the entity ID.');
}
list($entity_id, , $bundle) = entity_extract_ids($this->getEntityType(), $this->entity);
list($entity_id, , $bundle) = $this->entityManager->entityExtractIds($this->getEntityType(), $this->entity);
$this->entityId = $entity_id;
$this->bundle = $bundle;
......@@ -107,7 +135,7 @@ class TypedEntity implements TypedEntityInterface {
// We do not have neither entity nor ID. We cannot load.
return NULL;
}
$entities = entity_load($this->getEntityType(), array($this->getEntityId()));
$entities = $this->entityManager->entityLoad($this->getEntityType(), array($this->getEntityId()));
$this->entity = isset($entities[$this->getEntityId()]) ? $entities[$this->getEntityId()] : NULL;
return $this->entity;
}
......@@ -127,7 +155,7 @@ class TypedEntity implements TypedEntityInterface {
return $this->bundle;
}
list($entity_id, , $bundle) = entity_extract_ids($this->getEntityType(), $this->getEntity());
list($entity_id, , $bundle) = $this->entityManager->entityExtractIds($this->getEntityType(), $this->getEntity());
$this->entityId = $entity_id;
$this->bundle = $bundle;
return $this->bundle;
......@@ -140,11 +168,7 @@ class TypedEntity implements TypedEntityInterface {
if (isset($this->wrapper)) {
return $this->wrapper;
}
if (!\ServiceContainer::hasService('entity.wrapper')) {
throw new TypedEntityException('Unable to find the entity wrapper service');
}
$this->wrapper = \ServiceContainer::service('entity.wrapper')
->wrap($this->getEntityType(), $this->getEntity());
$this->wrapper = $this->entityMetadataService->wrap($this->getEntityType(), $this->getEntity());
return $this->wrapper;
}
......
......@@ -7,14 +7,27 @@
namespace Drupal\typed_entity\TypedEntity;
use Drupal\service_container\DependencyInjection\ContainerInterface;
class TypedEntityManager implements TypedEntityManagerInterface {
/**
* The service container.
*
* @var ContainerInterface
*/
protected static $serviceContainer;
/**
* {@inheritdoc}
*/
public static function create($entity_type, $entity) {
// Get the services from the container.
if (!isset(static::$serviceContainer)) {
static::$serviceContainer = \ServiceContainer::service('service_container');
}
$class_name = static::getClass($entity_type, $entity);
return new $class_name($entity_type, NULL, $entity);
return new $class_name(static::$serviceContainer->get('entity.manager'), static::$serviceContainer->get('entity.wrapper'), static::$serviceContainer->get('module_handler'), $entity_type, NULL, $entity);
}
/**
......@@ -29,8 +42,10 @@ class TypedEntityManager implements TypedEntityManagerInterface {
* A valid class name if one exists. It defaults to TypedEntity.
*/
public static function getClass($entity_type, $entity) {
$classes = &drupal_static(__METHOD__);
list(,, $bundle) = entity_extract_ids($entity_type, $entity);
static $classes = array();
list(,, $bundle) = static::$serviceContainer
->get('entity.manager')
->entityExtractIds($entity_type, $entity);
$cid = $entity_type . ':' . $bundle;
if (isset($classes[$cid])) {
......@@ -38,7 +53,12 @@ class TypedEntityManager implements TypedEntityManagerInterface {
}
$cached_classes = array();
if ($cache = cache_get('typed_entity_classes', 'cache_bootstrap')) {
if (
$cache = static::$serviceContainer
->get('system.cache.manager')
->getController('cache_bootstrap')
->get('typed_entity_classes')
) {
$cached_classes = $cache->data;
}
......@@ -79,7 +99,9 @@ class TypedEntityManager implements TypedEntityManagerInterface {
* An array of class name candidates.
*/
protected static function getClassNameCandidates($entity_type, $bundle = NULL) {
$candidates = module_invoke_all('typed_entity_registry_info');
$candidates = static::$serviceContainer
->get('module_handler')
->invokeAll('typed_entity_registry_info');
$candidate_entity_type = $candidate_bundle = '';
foreach ($candidates as $candidate) {
if ($candidate['entity_type'] != $entity_type) {
......@@ -164,4 +186,11 @@ class TypedEntityManager implements TypedEntityManagerInterface {
return implode('', $parts);
}
/**
* {@inheritdoc}
*/
public static function setServiceContainer(ContainerInterface $service_container) {
static::$serviceContainer = $service_container;
}
}
......@@ -7,6 +7,11 @@
namespace Drupal\typed_entity\TypedEntity;
use Drupal\service_container\DependencyInjection\ContainerInterface;
use Drupal\typed_entity\Entity\EntityManagerInterface;
use Drupal\typed_entity\Entity\EntityWrapperServiceInterface;
use Drupal\typed_entity\System\CacheManagerInterface;
interface TypedEntityManagerInterface {
/**
......@@ -33,4 +38,12 @@ interface TypedEntityManagerInterface {
*/
public static function camelize($input);
/**
* Sets the service container.
*
* @param ContainerInterface $service_container
* The container to set for the TypedEntityManager.
*/
public static function setServiceContainer(ContainerInterface $service_container);
}
{
"require-dev": {
"phpunit/phpunit": "4.7.*",
"mockery/mockery": "0.9.*"
"mockery/mockery": "0.9.*",
"mateu-aguilo-bosch/drupal-unit-autoload": "0.1.*"
},
"autoload": {
"psr-0": {
......@@ -13,5 +14,15 @@
"Drupal\\typed_entity\\": "../src/",
"Drupal\\typed_entity\\Tests\\": "src/"
}
},
"class-loader": {
"drupal-path": {
"\\DrupalCacheInterface": "DRUPAL_ROOT/includes/cache.inc",
"\\ServiceContainer": "DRUPAL_CONTRIB<service_container>/lib/ServiceContainer.php",
"\\Drupal": "DRUPAL_CONTRIB<service_container>/lib/Drupal.php"
},
"psr-4": {
"Drupal\\service_container\\": ["DRUPAL_CONTRIB<service_container>/src"]
}
}
}
Supports Markdown
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