Commit 0ac97d9f authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch
Browse files

Add service container

parent bacdabd0
......@@ -79,4 +79,17 @@ class TypedEntityExampleWebTestCase extends \DrupalWebTestCase {
}
}
/**
* Test the service container integration.
*/
public function testServiceContainer() {
$article = $this->drupalCreateNode(array(
'type' => 'article',
'title' => 'Test article',
));
$typed_article = TypedEntityManager::create('node', $article);
$wrapper = $typed_article->getWrapper();
$this->assertTrue($wrapper instanceof \EntityDrupalWrapperInterface);
}
}
<?php
/**
* @file
* Contains \Drupal\typed_entity\Entity\EntityWrapperService.
*/
namespace Drupal\typed_entity\Entity;
class EntityWrapperService implements EntityWrapperServiceInterface {
/**
* {@inheritdoc}
*/
public static function wrap($entity_type, $entity) {
return new \EntityDrupalWrapper($entity_type, $entity);
}
}
<?php
/**
* @file
* Contains \Drupal\typed_entity\Entity\EntityWrapperServiceInterface.
*/
namespace Drupal\typed_entity\Entity;
interface EntityWrapperServiceInterface {
/**
* Wraps an entity based on its type.
*
* @param string $entity_type
* The entity type.
* @param mixed $entity
* The loaded entity or entity ID.
*
* @return \EntityDrupalWrapperInterface
*/
public static function wrap($entity_type, $entity);
}
<?php
/**
* @file
* Contains Drupal\typed_entity\ServiceContainer\ServiceProvider\TypedEntityServiceProvider.
*/
namespace Drupal\typed_entity\ServiceContainer\ServiceProvider;
use Drupal\service_container\DependencyInjection\ServiceProviderInterface;
class TypedEntityServiceProvider implements ServiceProviderInterface {
/**
* {@inheritdoc}
*/
public function getContainerDefinition() {
$parameters = $services = array();
$services['entity.wrapper'] = array(
'class' => '\Drupal\typed_entity\Entity\EntityWrapperService',
);
return array(
'parameters' => $parameters,
'services' => $services,
);
}
/**
* {@inheritdoc}
*/
public function alterContainerDefinition(&$container_definition) {}
}
<?php
$plugin = array(
'class' => "\\Drupal\\typed_entity\\ServiceContainer\\ServiceProvider\\TypedEntityServiceProvider",
);
......@@ -7,6 +7,7 @@
namespace Drupal\typed_entity\TypedEntity;
use Drupal\service_container\DependencyInjection\Container;
use Drupal\typed_entity\Exception\TypedEntityException;
class TypedEntity implements TypedEntityInterface {
......@@ -42,13 +43,15 @@ class TypedEntity implements TypedEntityInterface {
/**
* The EMW
*
* @var \EntityDrupalWrapper
* @var \EntityDrupalWrapperInterface
*/
protected $wrapper;
/**
* Constructs a TypedEntity object.
*
* @param Container $container
* The service container.
* @param string $entity_type
* The type of the entity.
* @param int $entity_id
......@@ -137,7 +140,11 @@ class TypedEntity implements TypedEntityInterface {
if (isset($this->wrapper)) {
return $this->wrapper;
}
$this->wrapper = entity_metadata_wrapper($this->getEntityType(), $this->getEntity());
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());
return $this->wrapper;
}
......
......@@ -4,3 +4,23 @@
* @file
* Module implementation file.
*/
/**
* Implements hook_ctools_plugin_directory().
*
* We need to implement this so the service container plugins can be discovered.
*
* @todo: Explain what a service container plugin does.
*
* @param string $owner
* @param string $plugin_type
*
* @return null|string
*/
function typed_entity_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'service_container') {
return 'src/ServiceContainer/' . $plugin_type;
}
return NULL;
}
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