diff --git a/src/Revisions/ResourceVersionRouteEnhancer.php b/src/Revisions/ResourceVersionRouteEnhancer.php index c04b930..19ea321 100644 --- a/src/Revisions/ResourceVersionRouteEnhancer.php +++ b/src/Revisions/ResourceVersionRouteEnhancer.php @@ -5,6 +5,7 @@ namespace Drupal\jsonapi\Revisions; use Drupal\Core\Routing\EnhancerInterface; use Drupal\jsonapi\Plugin\VersionNegotiation\VersionByRel; use Drupal\jsonapi\Routing\Routes; +use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; /** @@ -93,31 +94,8 @@ class ResourceVersionRouteEnhancer implements EnhancerInterface { /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $defaults[$resource_type->getEntityTypeId()]; - list($negotiator_name, $input_data) = explode(VersionNegotiationManager::SEPARATOR, $resource_version_identifier); - $resolved_revision = NULL; - try { - /** @var \Drupal\jsonapi\Revisions\VersionNegotiationInterface $negotiator */ - $negotiator = $this->revisionNegotiatorManager->createInstance($negotiator_name); - $resolved_revision = $negotiator->getRevision($entity, $input_data); - } - catch (\InvalidArgumentException $exception) { - VersionNegotiationManager::throwBadRequestHttpException($resource_version_identifier); - } - catch (PluginException $exception) { - VersionNegotiationManager::throwBadRequestHttpException($resource_version_identifier); - } - if (is_null($resolved_revision)) { - $cacheability = (new CacheableMetadata()) - ->addCacheContexts(['url.query_args:' . static::RESOURCE_VERSION_QUERY_PARAMETER]) - ->addCacheableDependency($entity); - throw new CacheableNotFoundHttpException( - $cacheability, - sprintf( - 'The requested resource version, identified by `%s`, could not be found.', - $resource_version_identifier - ) - ); - } + /** @var \Drupal\jsonapi\Revisions\VersionNegotiationInterface $negotiator */ + $resolved_revision = $this->revisionNegotiatorManager->getRevision($entity, $resource_version_identifier); return [$resource_type->getEntityTypeId() => $resolved_revision] + $defaults; } diff --git a/src/Revisions/VersionNegotiationManager.php b/src/Revisions/VersionNegotiationManager.php index d2e9fff..0093b0b 100644 --- a/src/Revisions/VersionNegotiationManager.php +++ b/src/Revisions/VersionNegotiationManager.php @@ -2,12 +2,13 @@ namespace Drupal\jsonapi\Revisions; +use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Http\Exception\CacheableBadRequestHttpException; use Drupal\Core\Plugin\DefaultPluginManager; -use Drupal\jsonapi\JsonApiSpec; /** * Provides an Revision ID negotiation plugin manager. @@ -48,6 +49,49 @@ class VersionNegotiationManager extends DefaultPluginManager { $this->setCacheBackend($cache_backend, 'revision_id_negoriation_info_plugins'); } + /** + * Gets a negotiated entity revision. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity. + * @param string $version_identifier + * A value used to derive a revision ID for the given entity. + * + * @return \Drupal\Core\Entity\EntityInterface + * The loaded revision. + * + * @throws \InvalidArgumentException + * When the revision ID cannot be negotiated. + */ + public function getRevision(EntityInterface $entity, $resource_version_identifier) { + $resolved_revision = NULL; + try { + list($negotiator_name, $input_data) = explode(VersionNegotiationManager::SEPARATOR, $resource_version_identifier, 2); + /* @var \Drupal\jsonapi\Revisions\VersionNegotiationInterface $negotiator */ + $negotiator = $this->createInstance($negotiator_name); + return $negotiator->getRevision($entity, $input_data); + } + catch (PluginException $exception) { + static::throwBadRequestHttpException($resource_version_identifier); + } + catch (\InvalidArgumentException $exception) { + static::throwBadRequestHttpException($resource_version_identifier); + } + if (is_null($resolved_revision)) { + $cacheability = (new CacheableMetadata()) + ->addCacheContexts(['url.query_args:' . static::RESOURCE_VERSION_QUERY_PARAMETER]) + ->addCacheableDependency($entity); + throw new CacheableNotFoundHttpException( + $cacheability, + sprintf( + 'The requested resource version, identified by `%s`, could not be found.', + $resource_version_identifier + ) + ); + } + return $resolved_revision; + } + /** * Throws a cacheable error exception. *