Chris@18: negotiators[$negotiator_name] = $version_negotiator; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets a negotiated entity revision. Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@18: * The entity. Chris@18: * @param string $resource_version_identifier Chris@18: * A value used to derive a revision for the given entity. Chris@18: * Chris@18: * @return \Drupal\Core\Entity\EntityInterface Chris@18: * The loaded revision. Chris@18: * Chris@18: * @throws \Drupal\Core\Http\Exception\CacheableNotFoundHttpException Chris@18: * When the revision does not exist. Chris@18: * @throws \Drupal\Core\Http\Exception\CacheableBadRequestHttpException Chris@18: * When the revision ID cannot be negotiated. Chris@18: */ Chris@18: public function getRevision(EntityInterface $entity, $resource_version_identifier) { Chris@18: try { Chris@18: list($version_negotiator_name, $version_argument) = explode(VersionNegotiator::SEPARATOR, $resource_version_identifier, 2); Chris@18: if (!isset($this->negotiators[$version_negotiator_name])) { Chris@18: static::throwBadRequestHttpException($resource_version_identifier); Chris@18: } Chris@18: return $this->negotiators[$version_negotiator_name]->getRevision($entity, $version_argument); Chris@18: } Chris@18: catch (VersionNotFoundException $exception) { Chris@18: static::throwNotFoundHttpException($entity, $resource_version_identifier); Chris@18: } Chris@18: catch (InvalidVersionIdentifierException $exception) { Chris@18: static::throwBadRequestHttpException($resource_version_identifier); Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * Throws a cacheable error exception. Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@18: * The entity for which a revision was requested. Chris@18: * @param string $resource_version_identifier Chris@18: * The user input for the revision negotiation. Chris@18: * Chris@18: * @throws \Drupal\Core\Http\Exception\CacheableNotFoundHttpException Chris@18: */ Chris@18: protected static function throwNotFoundHttpException(EntityInterface $entity, $resource_version_identifier) { Chris@18: $cacheability = CacheableMetadata::createFromObject($entity)->addCacheContexts(['url.path', 'url.query_args:' . ResourceVersionRouteEnhancer::RESOURCE_VERSION_QUERY_PARAMETER]); Chris@18: $reason = sprintf('The requested version, identified by `%s`, could not be found.', $resource_version_identifier); Chris@18: throw new CacheableNotFoundHttpException($cacheability, $reason); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Throws a cacheable error exception. Chris@18: * Chris@18: * @param string $resource_version_identifier Chris@18: * The user input for the revision negotiation. Chris@18: * Chris@18: * @throws \Drupal\Core\Http\Exception\CacheableBadRequestHttpException Chris@18: */ Chris@18: protected static function throwBadRequestHttpException($resource_version_identifier) { Chris@18: $cacheability = (new CacheableMetadata())->addCacheContexts(['url.query_args:' . ResourceVersionRouteEnhancer::RESOURCE_VERSION_QUERY_PARAMETER]); Chris@18: $message = sprintf('An invalid resource version identifier, `%s`, was provided.', $resource_version_identifier); Chris@18: throw new CacheableBadRequestHttpException($cacheability, $message); Chris@18: } Chris@18: Chris@18: }