summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/ResourceType/ConfigurableResourceTypeRepository.php119
1 files changed, 83 insertions, 36 deletions
diff --git a/src/ResourceType/ConfigurableResourceTypeRepository.php b/src/ResourceType/ConfigurableResourceTypeRepository.php
index e5938bc..61a4388 100644
--- a/src/ResourceType/ConfigurableResourceTypeRepository.php
+++ b/src/ResourceType/ConfigurableResourceTypeRepository.php
@@ -46,13 +46,20 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
protected $configFactory;
/**
- * A list of resource types.
+ * A list of all resource types.
*
* @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType[]
*/
protected $resourceTypes;
/**
+ * A list of only enabled resource types.
+ *
+ * @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType[]
+ */
+ protected $enabledResourceTypes;
+
+ /**
* {@inheritdoc}
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_manager, EntityFieldManagerInterface $entity_field_manager, EntityRepositoryInterface $entity_repository, ResourceFieldEnhancerManager $enhancer_manager, ConfigFactoryInterface $config_factory) {
@@ -103,50 +110,70 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function getResourceTypes($include_disabled = TRUE) {
- if (isset($this->resourceTypes) && $include_disabled) {
- return $this->resourceTypes;
- }
-
- $entity_type_ids = array_keys($this->entityTypeManager->getDefinitions());
-
- $resource_types = [];
-
- $resource_config_ids = [];
- foreach ($entity_type_ids as $entity_type_id) {
- $bundles = array_keys($this->bundleManager->getBundleInfo($entity_type_id));
- $resource_config_ids = array_merge($resource_config_ids, array_map(function ($bundle) use ($entity_type_id) {
- return sprintf('%s--%s', $entity_type_id, $bundle);
- }, $bundles));
- }
-
- $resource_configs = $this->entityTypeManager->getStorage('jsonapi_resource_config')->loadMultiple($resource_config_ids);
-
- if (isset($this->resourceTypes) && !$include_disabled) {
- return $this->filterOutDisabledResourceTypes($this->resourceTypes, $resource_configs);
- }
-
- foreach ($entity_type_ids as $entity_type_id) {
- $bundles = array_keys($this->bundleManager->getBundleInfo($entity_type_id));
- $current_types = array_map(function ($bundle) use ($entity_type_id, $include_disabled, $resource_configs) {
+ if (!isset($this->resourceTypes)) {
+ $this->resourceTypes = [];
+ foreach ($this->getEntityTypeBundleTuples() as $tuple) {
+ list($entity_type_id, $bundle) = $tuple;
$resource_config_id = sprintf('%s--%s', $entity_type_id, $bundle);
- $resource_config = isset($resource_configs[$resource_config_id]) ? $resource_configs[$resource_config_id] : new NullJsonapiResourceConfig([], '');
- return new ConfigurableResourceType(
+ $this->resourceTypes[] = new ConfigurableResourceType(
$entity_type_id,
$bundle,
$this->entityTypeManager->getDefinition($entity_type_id)->getClass(),
- $resource_config,
+ $this->getResourceConfig($resource_config_id),
$this->enhancerManager,
$this->configFactory
);
- }, $bundles);
- $resource_types = array_merge($resource_types, $current_types);
+ }
+ }
+
+ if (!isset($this->enabledResourceTypes) && !$include_disabled) {
+ $this->enabledResourceTypes = $this->filterOutDisabledResourceTypes(
+ $this->resourceTypes,
+ $this->getResourceConfigs()
+ );
}
- $this->resourceTypes = $resource_types;
- if (!$include_disabled) {
- return $this->filterOutDisabledResourceTypes($this->resourceTypes, $resource_configs);
+ return ($include_disabled) ?
+ $this->resourceTypes :
+ $this->enabledResourceTypes;
+ }
+
+ /**
+ * Get a single resource configuration entity by its ID.
+ *
+ * @param string $resource_config_id
+ * The configuration entity ID.
+ *
+ * @return \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ */
+ protected function getResourceConfig($resource_config_id) {
+ $resource_configs = $this->getResourceConfigs();
+ return isset($resource_configs[$resource_config_id]) ?
+ $resource_configs[$resource_config_id] :
+ new NullJsonapiResourceConfig([], '');
+ }
+
+ /**
+ * Load all resource configuration entities.
+ *
+ * @return \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig[]
+ * The resource config entities.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ */
+ function getResourceConfigs() {
+ $resource_config_ids = [];
+ foreach ($this->getEntityTypeBundleTuples() as $tuple) {
+ list($entity_type_id, $bundle) = $tuple;
+ $resource_config_ids[] = sprintf('%s--%s', $entity_type_id, $bundle);
}
- return $this->resourceTypes;
+ /** @var \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig[] $resource_configs */
+ $resource_configs = $this->entityTypeManager
+ ->getStorage('jsonapi_resource_config')
+ ->loadMultiple($resource_config_ids);
+ return $resource_configs;
}
/**
@@ -154,7 +181,7 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
*
* @param \Drupal\jsonapi\ResourceType\ResourceType[] $resource_types
* The list of resource types including disabled ones.
- * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $resource_configs
+ * @param \Drupal\Core\Entity\EntityInterface[] $resource_configs
* The configuration entities that accompany the resource types.
*
* @return \Drupal\jsonapi\ResourceType\ResourceType[]
@@ -172,4 +199,24 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
});
}
+ /**
+ * Entity type ID and bundle iterator.
+ *
+ * @return array
+ * A list of entity type ID and bundle tuples.
+ */
+ protected function getEntityTypeBundleTuples() {
+ $entity_type_ids = array_keys($this->entityTypeManager->getDefinitions());
+ // For each entity type return as many tuples as bundles.
+ return array_reduce($entity_type_ids, function ($carry, $entity_type_id) {
+ $bundles = array_keys($this->bundleManager->getBundleInfo($entity_type_id));
+ // Get all the tuples for the current entity type.
+ $tuples = array_map(function ($bundle) use ($entity_type_id) {
+ return [$entity_type_id, $bundle];
+ }, $bundles);
+ // Append the tuples to the aggregated list.
+ return array_merge($carry, $tuples);
+ }, []);
+ }
+
}