annotate core/modules/jsonapi/src/Routing/RouteEnhancer.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@18 1 <?php
Chris@18 2
Chris@18 3 namespace Drupal\jsonapi\Routing;
Chris@18 4
Chris@18 5 use Drupal\Core\Routing\EnhancerInterface;
Chris@18 6 use Symfony\Component\HttpFoundation\Request;
Chris@18 7 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Chris@18 8
Chris@18 9 /**
Chris@18 10 * Ensures the loaded entity matches the requested resource type.
Chris@18 11 *
Chris@18 12 * @internal JSON:API maintains no PHP API since its API is the HTTP API. This
Chris@18 13 * class may change at any time and this will break any dependencies on it.
Chris@18 14 *
Chris@18 15 * @see https://www.drupal.org/project/jsonapi/issues/3032787
Chris@18 16 * @see jsonapi.api.php
Chris@18 17 */
Chris@18 18 class RouteEnhancer implements EnhancerInterface {
Chris@18 19
Chris@18 20 /**
Chris@18 21 * {@inheritdoc}
Chris@18 22 */
Chris@18 23 public function enhance(array $defaults, Request $request) {
Chris@18 24 if (!Routes::isJsonApiRequest($defaults)) {
Chris@18 25 return $defaults;
Chris@18 26 }
Chris@18 27
Chris@18 28 $resource_type = Routes::getResourceTypeNameFromParameters($defaults);
Chris@18 29 $entity_type_id = $resource_type->getEntityTypeId();
Chris@18 30 if (!isset($defaults[$entity_type_id]) || !($entity = $defaults[$entity_type_id])) {
Chris@18 31 return $defaults;
Chris@18 32 }
Chris@18 33 $retrieved_bundle = $entity->bundle();
Chris@18 34 $configured_bundle = $resource_type->getBundle();
Chris@18 35 if ($retrieved_bundle != $configured_bundle) {
Chris@18 36 // If the bundle in the loaded entity does not match the bundle in the
Chris@18 37 // route (which is set based on the corresponding ResourceType), then
Chris@18 38 // throw an exception.
Chris@18 39 throw new NotFoundHttpException(sprintf('The loaded entity bundle (%s) does not match the configured resource (%s).', $retrieved_bundle, $configured_bundle));
Chris@18 40 }
Chris@18 41 return $defaults;
Chris@18 42 }
Chris@18 43
Chris@18 44 }