From c3b127382b3626398a3beb5a1ccd69ff80ceba0a Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Sun, 2 Feb 2014 21:09:58 +0000 Subject: [PATCH] Issue #2181625 by damiankloip: Use constructor injection in hal normalizer services. --- core/modules/hal/hal.services.yml | 11 +----- .../hal/Normalizer/EntityNormalizer.php | 19 +++++++++- .../EntityReferenceItemNormalizer.php | 29 +++++++++++++++ .../Drupal/hal/Normalizer/FieldNormalizer.php | 2 - .../Drupal/hal/Normalizer/NormalizerBase.php | 37 ------------------- .../Drupal/hal/Tests/NormalizerTestBase.php | 12 +++--- 6 files changed, 55 insertions(+), 55 deletions(-) diff --git a/core/modules/hal/hal.services.yml b/core/modules/hal/hal.services.yml index 7f3e405c0a6c..ad5c5f6aa205 100644 --- a/core/modules/hal/hal.services.yml +++ b/core/modules/hal/hal.services.yml @@ -1,29 +1,22 @@ 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: diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php index bd0e74191c93..4b5b9270800d 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php @@ -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() */ diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php index 7152070ab9fa..1ffc72faa513 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php @@ -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() */ diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php index c2d160b57a3a..2648998f927f 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php @@ -8,8 +8,6 @@ namespace Drupal\hal\Normalizer; use Drupal\Component\Utility\NestedArray; -use Drupal\Core\Language\Language; - use Symfony\Component\Serializer\Exception\LogicException; /** diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/NormalizerBase.php b/core/modules/hal/lib/Drupal/hal/Normalizer/NormalizerBase.php index 769f4f9427d7..7d762f5df5e2 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/NormalizerBase.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/NormalizerBase.php @@ -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; - } - } diff --git a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php index 3b2372cc3d13..bc07d77b2817 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php @@ -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(), ); -- GitLab