Issue #2931765 by timmillwood, Wim Leers: Regression: \Drupal\hal\LinkManager\LinkManagerBase implicitly depends on REST module

merge-requests/1654/head
Nathaniel Catchpole 2018-01-24 14:21:04 +00:00
parent 2249c07ee0
commit 05422f2a0b
5 changed files with 45 additions and 32 deletions

View File

@ -2,7 +2,7 @@
namespace Drupal\hal\LinkManager;
use Drupal\rest\EventSubscriber\ResourceResponseSubscriber;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
/**
* Defines an abstract base-class for HAL link manager objects.
@ -49,21 +49,21 @@ abstract class LinkManagerBase {
*
* @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
* @see \Symfony\Component\Serializer\SerializerInterface::serialize()
* @see \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY
* @see \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY
*/
protected function getLinkDomain(array $context = []) {
if (empty($this->linkDomain)) {
if ($domain = $this->configFactory->get('hal.settings')->get('link_domain')) {
// Bubble the appropriate cacheability metadata whenever possible.
if (isset($context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY])) {
$context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($this->configFactory->get('hal.settings'));
if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
$context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($this->configFactory->get('hal.settings'));
}
return rtrim($domain, '/');
}
else {
// Bubble the relevant cacheability metadata whenever possible.
if (isset($context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY])) {
$context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheContexts(['url.site']);
if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
$context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheContexts(['url.site']);
}
$request = $this->requestStack->getCurrentRequest();
return $request->getSchemeAndHttpHost() . $request->getBasePath();

View File

@ -8,7 +8,7 @@ use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\rest\EventSubscriber\ResourceResponseSubscriber;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
/**
* @coversDefaultClass \Drupal\hal\LinkManager\LinkManager
@ -70,10 +70,10 @@ class HalLinkManagerTest extends KernelTestBase {
public function providerTestGetTypeUri() {
$serialization_context_collecting_cacheability = [
ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
];
$expected_serialization_context_cacheability_url_site = [
ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
];
$base_test_case = [
@ -133,7 +133,7 @@ class HalLinkManagerTest extends KernelTestBase {
'context' => $serialization_context_collecting_cacheability,
'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
'expected context' => [
ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
],
],
];
@ -163,10 +163,10 @@ class HalLinkManagerTest extends KernelTestBase {
public function providerTestGetRelationUri() {
$serialization_context_collecting_cacheability = [
ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
];
$expected_serialization_context_cacheability_url_site = [
ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
];
$field_name = $this->randomMachineName();
@ -230,7 +230,7 @@ class HalLinkManagerTest extends KernelTestBase {
'context' => $serialization_context_collecting_cacheability,
'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
'expected context' => [
ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
],
],
];
@ -258,7 +258,7 @@ class HalLinkManagerTest extends KernelTestBase {
*/
public function testHalLinkManagersSetLinkDomain() {
$serialization_context = [
ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
];
/* @var \Drupal\rest\LinkManager\LinkManager $link_manager */
@ -266,10 +266,10 @@ class HalLinkManagerTest extends KernelTestBase {
$link_manager->setLinkDomain('http://example.com/');
$link = $link_manager->getTypeUri('node', 'page', $serialization_context);
$this->assertEqual($link, 'http://example.com/rest/type/node/page');
$this->assertEqual(new CacheableMetadata(), $serialization_context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]);
$this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
$link = $link_manager->getRelationUri('node', 'page', 'field_ref', $serialization_context);
$this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref');
$this->assertEqual(new CacheableMetadata(), $serialization_context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]);
$this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
}
}

View File

@ -9,6 +9,7 @@ use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\rest\ResourceResponseInterface;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
@ -21,15 +22,6 @@ use Symfony\Component\Serializer\SerializerInterface;
*/
class ResourceResponseSubscriber implements EventSubscriberInterface {
/**
* Name of key for bubbling cacheability metadata via serialization context.
*
* @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
* @see \Symfony\Component\Serializer\SerializerInterface::serialize()
* @see \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::renderResponseBody()
*/
const SERIALIZATION_CONTEXT_CACHEABILITY = 'cacheability';
/**
* The serializer.
*
@ -172,7 +164,7 @@ class ResourceResponseSubscriber implements EventSubscriberInterface {
if ($data !== NULL) {
$serialization_context = [
'request' => $request,
static::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
];
// @deprecated In Drupal 8.5.0, will be removed before Drupal 9.0.0. Use
@ -188,7 +180,7 @@ class ResourceResponseSubscriber implements EventSubscriberInterface {
@trigger_error('Implicit cacheability metadata bubbling (onto the global render context) in normalizers is deprecated since Drupal 8.5.0 and will be removed in Drupal 9.0.0. Use the "cacheability" serialization context instead, for explicit cacheability metadata bubbling. See https://www.drupal.org/node/2918937', E_USER_DEPRECATED);
$response->addCacheableDependency($context->pop());
}
$response->addCacheableDependency($serialization_context[static::SERIALIZATION_CONTEXT_CACHEABILITY]);
$response->addCacheableDependency($serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
}
$response->setContent($output);

View File

@ -0,0 +1,23 @@
<?php
namespace Drupal\serialization\Normalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Defines the interface for normalizers producing cacheable normalizations.
*
* @see cache
*/
interface CacheableNormalizerInterface extends NormalizerInterface {
/**
* Name of key for bubbling cacheability metadata via serialization context.
*
* @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
* @see \Symfony\Component\Serializer\SerializerInterface::serialize()
* @see \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::renderResponseBody()
*/
const SERIALIZATION_CONTEXT_CACHEABILITY = 'cacheability';
}

View File

@ -3,14 +3,12 @@
namespace Drupal\serialization\Normalizer;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\rest\EventSubscriber\ResourceResponseSubscriber;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;
/**
* Base class for Normalizers.
*/
abstract class NormalizerBase extends SerializerAwareNormalizer implements NormalizerInterface {
abstract class NormalizerBase extends SerializerAwareNormalizer implements CacheableNormalizerInterface {
/**
* The interface or class that this Normalizer supports.
@ -92,8 +90,8 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
* The data that might have cacheability information.
*/
protected function addCacheableDependency(array $context, $data) {
if ($data instanceof CacheableDependencyInterface && isset($context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY])) {
$context[ResourceResponseSubscriber::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($data);
if ($data instanceof CacheableDependencyInterface && isset($context[static::SERIALIZATION_CONTEXT_CACHEABILITY])) {
$context[static::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($data);
}
}