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