diff --git a/core/modules/dblog/src/Plugin/rest/resource/DBLogResource.php b/core/modules/dblog/src/Plugin/rest/resource/DBLogResource.php index 26ec4f3..56740ca 100644 --- a/core/modules/dblog/src/Plugin/rest/resource/DBLogResource.php +++ b/core/modules/dblog/src/Plugin/rest/resource/DBLogResource.php @@ -8,7 +8,7 @@ namespace Drupal\dblog\Plugin\rest\resource; use Drupal\rest\Plugin\ResourceBase; -use Drupal\rest\CacheableResourceResponse; +use Drupal\rest\ResourceResponse; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -33,7 +33,7 @@ class DBLogResource extends ResourceBase { * @param int $id * The ID of the watchdog log entry. * - * @return \Drupal\rest\CacheableResourceResponse + * @return \Drupal\rest\ResourceResponse * The response containing the log entry. * * @throws \Symfony\Component\HttpKernel\Exception\HttpException @@ -43,7 +43,7 @@ public function get($id = NULL) { $record = db_query("SELECT * FROM {watchdog} WHERE wid = :wid", array(':wid' => $id)) ->fetchAssoc(); if (!empty($record)) { - return new CacheableResourceResponse($record); + return new ResourceResponse($record); } throw new NotFoundHttpException(t('Log entry with ID @id was not found', array('@id' => $id))); diff --git a/core/modules/rest/src/CacheableResourceResponse.php b/core/modules/rest/src/CacheableResourceResponse.php deleted file mode 100644 index 9f58244..0000000 --- a/core/modules/rest/src/CacheableResourceResponse.php +++ /dev/null @@ -1,34 +0,0 @@ -responseData = $data; - parent::__construct('', $status, $headers); - } - -} diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index 3d3090d..34b0bda 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -9,9 +9,8 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageException; -use Drupal\rest\CacheableResourceResponse; use Drupal\rest\Plugin\ResourceBase; -use Drupal\rest\UncacheableResourceResponse; +use Drupal\rest\ResourceResponse; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -40,7 +39,7 @@ class EntityResource extends ResourceBase { * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object. * - * @return \Drupal\rest\UncacheableResourceResponse + * @return \Drupal\rest\ResourceResponse * The response containing the entity with its accessible fields. * * @throws \Symfony\Component\HttpKernel\Exception\HttpException @@ -55,7 +54,7 @@ public function get(EntityInterface $entity) { } } - $response = new CacheableResourceResponse($entity, 200); + $response = new ResourceResponse($entity, 200); // Make the response use the entity's cacheability metadata. // @todo include access cacheability metadata, for the access checks above. $response->addCacheableDependency($entity); @@ -110,7 +109,8 @@ public function post(EntityInterface $entity = NULL) { // 201 Created responses have an empty body. $url = $entity->urlInfo('canonical', ['absolute' => TRUE])->toString(TRUE); - $response = new UncacheableResourceResponse(NULL, 201, ['Location' => $url->getGeneratedUrl()]); + $response = new ResourceResponse(NULL, 201, ['Location' => $url->getGeneratedUrl()]); + $response->addCacheableDependency($url); return $response; } catch (EntityStorageException $e) { @@ -166,7 +166,7 @@ public function patch(EntityInterface $original_entity, EntityInterface $entity $this->logger->notice('Updated entity %type with ID %id.', array('%type' => $original_entity->getEntityTypeId(), '%id' => $original_entity->id())); // Update responses have an empty body. - return new UncacheableResourceResponse(NULL, 204); + return new ResourceResponse(NULL, 204); } catch (EntityStorageException $e) { throw new HttpException(500, 'Internal Server Error', $e); @@ -193,7 +193,7 @@ public function delete(EntityInterface $entity) { $this->logger->notice('Deleted entity %type with ID %id.', array('%type' => $entity->getEntityTypeId(), '%id' => $entity->id())); // Delete responses have an empty body. - return new UncacheableResourceResponse(NULL, 204); + return new ResourceResponse(NULL, 204); } catch (EntityStorageException $e) { throw new HttpException(500, 'Internal Server Error', $e); diff --git a/core/modules/rest/src/RequestHandler.php b/core/modules/rest/src/RequestHandler.php index 6dcb39f..5a04cd8 100644 --- a/core/modules/rest/src/RequestHandler.php +++ b/core/modules/rest/src/RequestHandler.php @@ -7,7 +7,6 @@ namespace Drupal\rest; -use Drupal\Core\Cache\CacheableResponseInterface; use Drupal\Core\Render\RenderContext; use Drupal\Core\Routing\RouteMatchInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; @@ -104,35 +103,26 @@ public function handle(RouteMatchInterface $route_match, Request $request) { } // Serialize the outgoing data for the response, if available. - if ($response instanceof ResourceResponseInterface && $data = $response->getResponseData()) { - // Cacheable Response. - if ($response instanceof CacheableResponseInterface) { - // Serialization can invoke rendering (e.g., generating URLs), but the - // serialization API does not provide a mechanism to collect the - // bubbleable metadata associated with that (e.g., language and other - // contexts), so instead, allow those to "leak" and collect them here in - // a render context. - // @todo Add test coverage for language negotiation contexts in - // https://www.drupal.org/node/2135829. - $context = new RenderContext(); - $output = $this->container->get('renderer')->executeInRenderContext($context, function () use ($serializer, $data, $format) { - return $serializer->serialize($data, $format); - }); - $response->setContent($output); - if (!$context->isEmpty()) { - $response->addCacheableDependency($context->pop()); - } - - $response->headers->set('Content-Type', $request->getMimeType($format)); - // Add rest settings config's cache tags. - $response->addCacheableDependency($this->container->get('config.factory')->get('rest.settings')); - } - else { - // Uncacheable Response. - $output = $serializer->serialize($data, $format); - $response->setContent($output); - $response->headers->set('Content-Type', $request->getMimeType($format)); + if ($response instanceof ResourceResponse && $data = $response->getResponseData()) { + // Serialization can invoke rendering (e.g., generating URLs), but the + // serialization API does not provide a mechanism to collect the + // bubbleable metadata associated with that (e.g., language and other + // contexts), so instead, allow those to "leak" and collect them here in + // a render context. + // @todo Add test coverage for language negotiation contexts in + // https://www.drupal.org/node/2135829. + $context = new RenderContext(); + $output = $this->container->get('renderer')->executeInRenderContext($context, function() use ($serializer, $data, $format) { + return $serializer->serialize($data, $format); + }); + $response->setContent($output); + if (!$context->isEmpty()) { + $response->addCacheableDependency($context->pop()); } + + $response->headers->set('Content-Type', $request->getMimeType($format)); + // Add rest settings config's cache tags. + $response->addCacheableDependency($this->container->get('config.factory')->get('rest.settings')); } return $response; } diff --git a/core/modules/rest/src/ResourceResponse.php b/core/modules/rest/src/ResourceResponse.php new file mode 100644 index 0000000..2919fb1 --- /dev/null +++ b/core/modules/rest/src/ResourceResponse.php @@ -0,0 +1,57 @@ +responseData = $data; + parent::__construct('', $status, $headers); + } + + /** + * Returns response data that should be serialized. + * + * @return mixed + * Response data that should be serialized. + */ + public function getResponseData() { + return $this->responseData; + } +} diff --git a/core/modules/rest/src/ResourceResponseInterface.php b/core/modules/rest/src/ResourceResponseInterface.php deleted file mode 100644 index a889dae..0000000 --- a/core/modules/rest/src/ResourceResponseInterface.php +++ /dev/null @@ -1,23 +0,0 @@ -responseData; - } - -} diff --git a/core/modules/rest/src/UncacheableResourceResponse.php b/core/modules/rest/src/UncacheableResourceResponse.php deleted file mode 100644 index 308275d..0000000 --- a/core/modules/rest/src/UncacheableResourceResponse.php +++ /dev/null @@ -1,30 +0,0 @@ -responseData = $data; - parent::__construct('', $status, $headers); - } -}