Commit 85d9a491 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch
Browse files

Avoid global DIC usage

parent 83e24d9f
......@@ -7,8 +7,6 @@
namespace Drupal\typed_entity\TypedEntity\Tests;
use Drupal\typed_entity\Exception\TypedEntityException;
// @TODO: Fix this. If we use EntityDrupalWrapperInterface then we need Drupal's registry, therefore the DB, …
// class MockEntityDrupalWrapper implements \EntityDrupalWrapperInterface {
class MockEntityDrupalWrapper {
......
......@@ -8,6 +8,7 @@
namespace Drupal\typed_entity\TypedEntity;
use Drupal\typed_entity\Exception\TypedEntityException;
use Drupal\xautoload\DIC\ServiceContainer;
class TypedEntity implements TypedEntityInterface {
......@@ -49,6 +50,8 @@ class TypedEntity implements TypedEntityInterface {
/**
* Constructs a TypedEntity object.
*
* @param ServiceContainer $dic
* The dependency injection container from xautoload.
* @param string $entity_type
* The type of the entity.
* @param int $entity_id
......@@ -61,13 +64,14 @@ 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(ServiceContainer $dic, $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.');
}
if (empty($entity_id) && empty($entity)) {
throw new TypedEntityException('You need to provide the fully loaded entity or the entity ID.');
}
$this->dic = $dic;
$this->entityType = $entity_type;
$this->entityId = $entity_id;
$this->entity = $entity;
......@@ -105,8 +109,8 @@ class TypedEntity implements TypedEntityInterface {
return NULL;
}
$this->entity = xautoload()
->getServiceContainer()
$this->entity = $this
->dic
->get('entity_wrapper')
->wrap($this->getEntityType(), $entity_id)
->value();
......@@ -141,8 +145,8 @@ class TypedEntity implements TypedEntityInterface {
if (isset($this->wrapper)) {
return $this->wrapper;
}
$this->wrapper = xautoload()
->getServiceContainer()
$this->wrapper = $this
->dic
->get('entity_wrapper')
->wrap($this->getEntityType(), $this->getEntity());
return $this->wrapper;
......
......@@ -7,6 +7,7 @@
namespace Drupal\typed_entity\TypedEntity;
use Drupal\xautoload\DIC\ServiceContainer;
use Drupal\xautoload\DrupalSystem\DrupalSystemInterface;
class TypedEntityManager implements TypedEntityManagerInterface {
......@@ -18,15 +19,32 @@ class TypedEntityManager implements TypedEntityManagerInterface {
*/
protected static $system;
/**
* Drupal system wrapper.
*
* @var ServiceContainer
*/
protected static $dic;
/**
* Constructor.
*
* Declare a private constructor to make sure this class is never
* instantiated. All method and properties here must be static.
*/
private function __construct() {}
/**
* {@inheritdoc}
*/
public static function create($entity_type, $entity) {
static::$system = xautoload()
->getServiceContainer()
->get('system');
public static function create($entity_type, $entity, ServiceContainer $dic = NULL) {
if (!isset($dic)) {
$dic = xautoload()->getServiceContainer();
}
static::$dic = $dic;
static::$system = $dic->get('system');
$class_name = static::getClass($entity_type, $entity);
return new $class_name($entity_type, NULL, $entity);
return new $class_name($dic, $entity_type, NULL, $entity);
}
/**
......@@ -43,8 +61,7 @@ class TypedEntityManager implements TypedEntityManagerInterface {
public static function getClass($entity_type, $entity) {
$classes = &drupal_static(__METHOD__);
/** @var \EntityDrupalWrapperInterface $wrapper */
$wrapper = xautoload()
->getServiceContainer()
$wrapper = static::$dic
->get('entity_wrapper')
->wrap($entity_type, $entity);
$bundle = $wrapper->getBundle();
......
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