src/Configuration/ResourceConfig.php | 25 ++++---------------- src/Configuration/ResourceConfigInterface.php | 27 ++++++++++------------ src/Configuration/ResourceManager.php | 2 +- src/Normalizer/EntityNormalizer.php | 4 ++-- src/Normalizer/EntityReferenceFieldNormalizer.php | 2 +- src/Resource/EntityResource.php | 2 +- src/Routing/Routes.php | 20 ++++++++-------- .../Kernel/Configuration/ResourceManagerTest.php | 8 +------ 8 files changed, 32 insertions(+), 58 deletions(-) diff --git a/src/Configuration/ResourceConfig.php b/src/Configuration/ResourceConfig.php index 8a02d8b..84558f0 100644 --- a/src/Configuration/ResourceConfig.php +++ b/src/Configuration/ResourceConfig.php @@ -27,7 +27,7 @@ class ResourceConfig implements ResourceConfigInterface { * * @var string */ - protected $bundleId; + protected $bundle; /** * The type name. @@ -37,13 +37,6 @@ class ResourceConfig implements ResourceConfigInterface { protected $typeName; /** - * The base resource path. - * - * @var string - */ - protected $path; - - /** * The class to which a payload converts to. * * @var string @@ -67,15 +60,8 @@ class ResourceConfig implements ResourceConfigInterface { /** * {@inheritdoc} */ - public function getPath() { - return $this->path; - } - - /** - * {@inheritdoc} - */ - public function getBundleId() { - return $this->bundleId; + public function getBundle() { + return $this->bundle; } /** @@ -97,11 +83,10 @@ class ResourceConfig implements ResourceConfigInterface { */ public function __construct($entity_type_id, $bundle_id, $deserialization_target_class) { $this->entityTypeId = $entity_type_id; - $this->bundleId = $bundle_id; + $this->bundle = $bundle_id; $this->deserializationTargetClass = $deserialization_target_class; - $this->typeName = sprintf('%s--%s', $this->entityTypeId, $this->bundleId); - $this->path= sprintf('/%s/%s', $this->entityTypeId, $this->bundleId); + $this->typeName = sprintf('%s--%s', $this->entityTypeId, $this->bundle); } } diff --git a/src/Configuration/ResourceConfigInterface.php b/src/Configuration/ResourceConfigInterface.php index e2d816d..27a37cf 100644 --- a/src/Configuration/ResourceConfigInterface.php +++ b/src/Configuration/ResourceConfigInterface.php @@ -9,36 +9,33 @@ namespace Drupal\jsonapi\Configuration; interface ResourceConfigInterface { /** - * Gets the entity type id. + * Gets the entity type ID. * * @return string - * The entity type id. + * The entity type ID. + * + * @see \Drupal\Core\Entity\EntityInterface::getEntityTypeId */ public function getEntityTypeId(); /** - * Gets the type name. + * Gets the bundle. * * @return string - * The type name. - */ - public function getTypeName(); - - /** - * Gets the path. + * The bundle. Is the same as the entity type ID if the entity type does not + * make use of different bundles. * - * @return string - * The path. + * @see \Drupal\Core\Entity\EntityInterface::bundle */ - public function getPath(); + public function getBundle(); /** - * Gets the bundle ID. + * Gets the type name. * * @return string - * The bundleId. + * The type name. */ - public function getBundleId(); + public function getTypeName(); /** * Gets the deserialization target class. diff --git a/src/Configuration/ResourceManager.php b/src/Configuration/ResourceManager.php index 11f7fba..d8c23b2 100644 --- a/src/Configuration/ResourceManager.php +++ b/src/Configuration/ResourceManager.php @@ -78,7 +78,7 @@ class ResourceManager implements ResourceManagerInterface { throw new PreconditionFailedHttpException('Server error. The current route is malformed.'); } foreach ($this->all(TRUE) as $resource) { - if ($resource->getEntityTypeId() == $entity_type_id && $resource->getBundleId() == $bundle_id) { + if ($resource->getEntityTypeId() == $entity_type_id && $resource->getBundle() == $bundle_id) { return $resource; } } diff --git a/src/Normalizer/EntityNormalizer.php b/src/Normalizer/EntityNormalizer.php index af7225b..a40a257 100644 --- a/src/Normalizer/EntityNormalizer.php +++ b/src/Normalizer/EntityNormalizer.php @@ -90,7 +90,7 @@ class EntityNormalizer extends NormalizerBase implements DenormalizerInterface, $resource_type = $context['resource_config']->getTypeName(); // Get the bundle ID of the requested resource. This is used to determine if // this is a bundle level resource or an entity level resource. - $bundle_id = $context['resource_config']->getBundleId(); + $bundle_id = $context['resource_config']->getBundle(); if (!empty($context['sparse_fieldset'][$resource_type])) { $field_names = $context['sparse_fieldset'][$resource_type]; } @@ -143,7 +143,7 @@ class EntityNormalizer extends NormalizerBase implements DenormalizerInterface, /* @var \Drupal\jsonapi\Configuration\ResourceConfigInterface $resource_config */ $resource_config = $context['resource_config']; $entity_type_id = $resource_config->getEntityTypeId(); - $bundle_id = $resource_config->getBundleId(); + $bundle_id = $resource_config->getBundle(); $bundle_key = $this->entityTypeManager->getDefinition($entity_type_id) ->getKey('bundle'); if ($bundle_key && $bundle_id) { diff --git a/src/Normalizer/EntityReferenceFieldNormalizer.php b/src/Normalizer/EntityReferenceFieldNormalizer.php index d786165..c04a76a 100644 --- a/src/Normalizer/EntityReferenceFieldNormalizer.php +++ b/src/Normalizer/EntityReferenceFieldNormalizer.php @@ -103,7 +103,7 @@ class EntityReferenceFieldNormalizer extends FieldNormalizer implements Denormal $entity_type_id = $context['resource_config']->getEntityTypeId(); $field_definitions = $this->fieldManager->getFieldDefinitions( $entity_type_id, - $context['resource_config']->getBundleId() + $context['resource_config']->getBundle() ); if (empty($context['related']) || empty($field_definitions[$context['related']])) { throw new SerializableHttpException(400, 'Invalid or missing related field.'); diff --git a/src/Resource/EntityResource.php b/src/Resource/EntityResource.php index d00a70e..66683ad 100644 --- a/src/Resource/EntityResource.php +++ b/src/Resource/EntityResource.php @@ -433,7 +433,7 @@ class EntityResource implements EntityResourceInterface { $query = $this->queryBuilder->newQuery($entity_type, $params); // Limit this query to the bundle type for this resource. - $bundle_id = $this->resourceConfig->getBundleId(); + $bundle_id = $this->resourceConfig->getBundle(); if ($bundle_id && ($bundle_key = $entity_type->getKey('bundle'))) { $query->condition( $bundle_key, $bundle_id diff --git a/src/Routing/Routes.php b/src/Routing/Routes.php index 715e2a8..aa82a15 100644 --- a/src/Routing/Routes.php +++ b/src/Routing/Routes.php @@ -80,6 +80,7 @@ class Routes implements ContainerInjectionInterface { public function routes() { $collection = new RouteCollection(); foreach ($this->resourceManager->all() as $resource_config) { + $route_base_path = sprintf('/jsonapi/%s/%s', $resource_config->getEntityTypeId(), $resource_config->getBundle()); $build_route_name = function ($key) use ($resource_config) { return sprintf('jsonapi.%s.%s', $resource_config->getTypeName(), $key); }; @@ -94,10 +95,9 @@ class Routes implements ContainerInjectionInterface { ]; // Collection endpoint, like /jsonapi/file/photo. - $route_collection = (new Route('/jsonapi' . $resource_config->getPath())) - ->addDefaults($defaults) + $route_collection = (new Route($route_base_path, $defaults)) ->setRequirement('_entity_type', $resource_config->getEntityTypeId()) - ->setRequirement('_bundle', $resource_config->getBundleId()) + ->setRequirement('_bundle', $resource_config->getBundle()) ->setRequirement('_permission', 'access content') ->setRequirement('_format', 'api_json') ->setRequirement('_custom_parameter_names', 'TRUE') @@ -108,10 +108,10 @@ class Routes implements ContainerInjectionInterface { // Individual endpoint, like /jsonapi/file/photo/123. $parameters = [$resource_config->getEntityTypeId() => ['type' => 'entity:' . $resource_config->getEntityTypeId()]]; - $route_individual = (new Route('/jsonapi' . sprintf('%s/{%s}', $resource_config->getPath(), $resource_config->getEntityTypeId()))) + $route_individual = (new Route(sprintf('%s/{%s}', $route_base_path, $resource_config->getEntityTypeId()))) ->addDefaults($defaults) ->setRequirement('_entity_type', $resource_config->getEntityTypeId()) - ->setRequirement('_bundle', $resource_config->getBundleId()) + ->setRequirement('_bundle', $resource_config->getBundle()) ->setRequirement('_permission', 'access content') ->setRequirement('_format', 'api_json') ->setRequirement('_custom_parameter_names', 'TRUE') @@ -123,10 +123,9 @@ class Routes implements ContainerInjectionInterface { $collection->add($build_route_name('individual'), $route_individual); // Related resource, like /jsonapi/file/photo/123/comments. - $route_related = (new Route('/jsonapi' . sprintf('%s/{%s}/{related}', $resource_config->getPath(), $resource_config->getEntityTypeId()))) - ->addDefaults($defaults) + $route_related = (new Route(sprintf('%s/{%s}/{related}', $route_base_path, $resource_config->getEntityTypeId()), $defaults)) ->setRequirement('_entity_type', $resource_config->getEntityTypeId()) - ->setRequirement('_bundle', $resource_config->getBundleId()) + ->setRequirement('_bundle', $resource_config->getBundle()) ->setRequirement('_permission', 'access content') ->setRequirement('_format', 'api_json') ->setRequirement('_custom_parameter_names', 'TRUE') @@ -137,10 +136,9 @@ class Routes implements ContainerInjectionInterface { $collection->add($build_route_name('related'), $route_related); // Related endpoint, like /jsonapi/file/photo/123/relationships/comments. - $route_relationship = (new Route('/jsonapi' . sprintf('%s/{%s}/relationships/{related}', $resource_config->getPath(), $resource_config->getEntityTypeId()))) - ->addDefaults($defaults + ['_on_relationship' => TRUE]) + $route_relationship = (new Route(sprintf('%s/{%s}/relationships/{related}', $route_base_path, $resource_config->getEntityTypeId()), $defaults + ['_on_relationship' => TRUE])) ->setRequirement('_entity_type', $resource_config->getEntityTypeId()) - ->setRequirement('_bundle', $resource_config->getBundleId()) + ->setRequirement('_bundle', $resource_config->getBundle()) ->setRequirement('_permission', 'access content') ->setRequirement('_format', 'api_json') ->setRequirement('_custom_parameter_names', 'TRUE') diff --git a/tests/src/Kernel/Configuration/ResourceManagerTest.php b/tests/src/Kernel/Configuration/ResourceManagerTest.php index 6ca6095..6c427f4 100644 --- a/tests/src/Kernel/Configuration/ResourceManagerTest.php +++ b/tests/src/Kernel/Configuration/ResourceManagerTest.php @@ -69,11 +69,6 @@ class ResourceManagerTest extends KernelTestBase { $this->assertNotEmpty($resource_config->getDeserializationTargetClass()); $this->assertNotEmpty($resource_config->getEntityTypeId()); $this->assertNotEmpty($resource_config->getTypeName()); - $path = $resource_config->getPath(); - if (!$resource_config->getBundleId()) { - $this->assertCount(1, explode('/', ltrim($path, '/'))); - } - $this->assertNotEmpty($path); }); } @@ -87,8 +82,7 @@ class ResourceManagerTest extends KernelTestBase { $this->assertInstanceOf(ResourceConfigInterface::class, $resource_config); $this->assertSame($entity_class, $resource_config->getDeserializationTargetClass()); $this->assertSame($entity_type_id, $resource_config->getEntityTypeId()); - $this->assertSame($bundle_id, $resource_config->getBundleId()); - $this->assertSame('/' . $entity_type_id . '/' . $bundle_id, $resource_config->getPath()); + $this->assertSame($bundle_id, $resource_config->getBundle()); $this->assertSame($entity_type_id . '--' . $bundle_id, $resource_config->getTypeName()); }