Commit c3b12738 authored by alexpott's avatar alexpott

Issue #2181625 by damiankloip: Use constructor injection in hal normalizer services.

parent dd4890bc
services:
serializer.normalizer.entity_reference_item.hal:
class: Drupal\hal\Normalizer\EntityReferenceItemNormalizer
arguments: ['@rest.link_manager', '@serializer.entity_resolver']
tags:
- { name: normalizer, priority: 10 }
calls:
- [setLinkManager, ['@rest.link_manager']]
- [setEntityResolver, ['@serializer.entity_resolver']]
serializer.normalizer.field_item.hal:
class: Drupal\hal\Normalizer\FieldItemNormalizer
tags:
- { name: normalizer, priority: 10 }
calls:
- [setLinkManager, ['@rest.link_manager']]
serializer.normalizer.field.hal:
class: Drupal\hal\Normalizer\FieldNormalizer
tags:
- { name: normalizer, priority: 10 }
calls:
- [setLinkManager, ['@rest.link_manager']]
serializer.normalizer.entity.hal:
class: Drupal\hal\Normalizer\EntityNormalizer
arguments: ['@rest.link_manager']
tags:
- { name: normalizer, priority: 10 }
calls:
- [setLinkManager, ['@rest.link_manager']]
serializer.encoder.hal:
class: Drupal\hal\Encoder\JsonEncoder
tags:
......
......@@ -8,8 +8,8 @@
namespace Drupal\hal\Normalizer;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Language\Language;
use Drupal\rest\LinkManager\LinkManagerInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
......@@ -24,6 +24,23 @@ class EntityNormalizer extends NormalizerBase {
*/
protected $supportedInterfaceOrClass = 'Drupal\Core\Entity\EntityInterface';
/**
* The hypermedia link manager.
*
* @var \Drupal\rest\LinkManager\LinkManagerInterface
*/
protected $linkManager;
/**
* Constructs an EntityNormalizer object.
*
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
*/
public function __construct(LinkManagerInterface $link_manager) {
$this->linkManager = $link_manager;
}
/**
* Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
*/
......
......@@ -7,6 +7,8 @@
namespace Drupal\hal\Normalizer;
use Drupal\rest\LinkManager\LinkManagerInterface;
use Drupal\serialization\EntityResolver\EntityResolverInterface;
use Drupal\serialization\EntityResolver\UuidReferenceInterface;
/**
......@@ -21,6 +23,33 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
*/
protected $supportedInterfaceOrClass = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';
/**
* The hypermedia link manager.
*
* @var \Drupal\rest\LinkManager\LinkManagerInterface
*/
protected $linkManager;
/**
* The entity resolver.
*
* @var \Drupal\serialization\EntityResolver\EntityResolverInterface
*/
protected $entityResolver;
/**
* Constructs an EntityReferenceItemNormalizer object.
*
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
* @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_Resolver
* The entity resolver.
*/
public function __construct(LinkManagerInterface $link_manager, EntityResolverInterface $entity_Resolver) {
$this->linkManager = $link_manager;
$this->entityResolver = $entity_Resolver;
}
/**
* Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
*/
......
......@@ -8,8 +8,6 @@
namespace Drupal\hal\Normalizer;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Language\Language;
use Symfony\Component\Serializer\Exception\LogicException;
/**
......
......@@ -23,20 +23,6 @@ abstract class NormalizerBase extends SerializationNormalizerBase implements Den
*/
protected $formats = array('hal_json');
/**
* The entity resolver.
*
* @var \Drupal\serialization\EntityResolver\EntityResolverInterface
*/
protected $entityResolver;
/**
* The hypermedia link manager.
*
* @var \Drupal\rest\LinkManager\LinkManager
*/
protected $linkManager;
/**
* Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::supportsNormalization().
*/
......@@ -60,27 +46,4 @@ public function supportsDenormalization($data, $type, $format = NULL) {
}
}
/**
* Sets the link manager.
*
* The link manager determines the hypermedia type and relation links which
* correspond to different bundles and fields.
*
* @param \Drupal\rest\LinkManager\LinkManager $link_manager
*/
public function setLinkManager($link_manager) {
$this->linkManager = $link_manager;
}
/**
* Sets the entity resolver.
*
* The entity resolver is used to
*
* @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_resolver
*/
public function setEntityResolver(EntityResolverInterface $entity_resolver) {
$this->entityResolver = $entity_resolver;
}
}
......@@ -17,6 +17,7 @@
use Drupal\rest\LinkManager\LinkManager;
use Drupal\rest\LinkManager\RelationLinkManager;
use Drupal\rest\LinkManager\TypeLinkManager;
use Drupal\serialization\EntityResolver\UuidResolver;
use Drupal\simpletest\DrupalUnitTestBase;
use Symfony\Component\Serializer\Serializer;
......@@ -119,17 +120,16 @@ function setUp() {
'bundle' => 'entity_test',
))->save();
$link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('cache')), new RelationLinkManager(new MemoryBackend('cache')));
// Set up the mock serializer.
$normalizers = array(
new EntityNormalizer(),
new EntityReferenceItemNormalizer(),
new EntityNormalizer($link_manager),
new EntityReferenceItemNormalizer($link_manager, new UuidResolver()),
new FieldItemNormalizer(),
new FieldNormalizer(),
);
$link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('cache')), new RelationLinkManager(new MemoryBackend('cache')));
foreach ($normalizers as $normalizer) {
$normalizer->setLinkManager($link_manager);
}
$encoders = array(
new JsonEncoder(),
);
......
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