diff --git a/core/modules/hal/src/LinkManager/LinkManagerBase.php b/core/modules/hal/src/LinkManager/LinkManagerBase.php index b6af1622d7..410265fa3d 100644 --- a/core/modules/hal/src/LinkManager/LinkManagerBase.php +++ b/core/modules/hal/src/LinkManager/LinkManagerBase.php @@ -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 @@ public function setLinkDomain($domain) { * * @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(); diff --git a/core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php b/core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php index 1387149c4e..95124bb9b6 100644 --- a/core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php +++ b/core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php @@ -8,7 +8,7 @@ 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 @@ public function testGetTypeUri($link_domain, $entity_type, $bundle, array $conte 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 @@ public function providerTestGetTypeUri() { '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 @@ public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_n 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 @@ public function providerTestGetRelationUri() { '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 @@ public function testGetRelationInternalIds() { */ 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 @@ public function testHalLinkManagersSetLinkDomain() { $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]); } } diff --git a/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php index babb238b45..ccd542a15c 100644 --- a/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php +++ b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php @@ -9,6 +9,7 @@ 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 @@ */ 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 @@ protected function renderResponseBody(Request $request, ResourceResponseInterfac 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 @@ protected function renderResponseBody(Request $request, ResourceResponseInterfac @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); diff --git a/core/modules/serialization/src/Normalizer/CacheableNormalizerInterface.php b/core/modules/serialization/src/Normalizer/CacheableNormalizerInterface.php new file mode 100644 index 0000000000..0a4886898d --- /dev/null +++ b/core/modules/serialization/src/Normalizer/CacheableNormalizerInterface.php @@ -0,0 +1,21 @@ +addCacheableDependency($data); + if ($data instanceof CacheableNormalizerInterface && isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) { + $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($data); } }