Newer
Older
<?php
namespace Drupal\jsonapi_extras\ResourceType;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\jsonapi\ResourceType\ResourceTypeRepository;
Mateu Aguiló Bosch
committed
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager;
Dan Polant
committed
use Drupal\Core\Config\ConfigFactoryInterface;
Jeremy Dickens
committed
use Drupal\Core\Entity\EntityFieldManagerInterface;
/**
* Provides a repository of JSON API configurable resource types.
*/
class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
Mateu Aguiló Bosch
committed
/**
* Plugin manager for enhancers.
*
* @var \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager
*/
protected $enhancerManager;
Jeremy Dickens
committed
/**
* The bundle manager.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $bundleManager;
Dan Polant
committed
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
Dan Polant
committed
*/
protected $configFactory;
/**
* 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;
/**
* A list of all resource configuration entities.
*
* @var \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig[]
*/
protected $resourceConfigs;
/**
* {@inheritdoc}
*/
Jeremy Dickens
committed
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_manager, EntityFieldManagerInterface $entity_field_manager, EntityRepositoryInterface $entity_repository, ResourceFieldEnhancerManager $enhancer_manager, ConfigFactoryInterface $config_factory) {
parent::__construct($entity_type_manager, $bundle_manager, $entity_field_manager);
$this->entityRepository = $entity_repository;
Mateu Aguiló Bosch
committed
$this->enhancerManager = $enhancer_manager;
Dan Polant
committed
$this->configFactory = $config_factory;
Jeremy Dickens
committed
$this->entityFieldManager = $entity_field_manager;
$this->bundleManager = $bundle_manager;
}
/**
* {@inheritdoc}
*/
public function all() {
if (!$this->all) {
foreach ($this->getEntityTypeBundleTuples() as $tuple) {
list($entity_type_id, $bundle) = $tuple;
$resource_config_id = sprintf('%s--%s', $entity_type_id, $bundle);
Mateu Aguiló Bosch
committed
$this->all[] = new ConfigurableResourceType(
$entity_type_id,
$bundle,
$this->entityTypeManager->getDefinition($entity_type_id)->getClass(),
$this->getResourceConfig($resource_config_id),
Dan Polant
committed
$this->enhancerManager,
$this->configFactory
}
Mateu Aguiló Bosch
committed
foreach ($this->all as $resource_type) {
Gabe Sullice
committed
$relatable_resource_types = $this->calculateRelatableResourceTypes($resource_type);
$resource_type->setRelatableResourceTypes($relatable_resource_types);
}
}
Mateu Aguiló Bosch
committed
return $this->all;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
}
/**
* 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() {
if (!$this->resourceConfigs) {
$resource_config_ids = [];
foreach ($this->getEntityTypeBundleTuples() as $tuple) {
list($entity_type_id, $bundle) = $tuple;
$resource_config_ids[] = sprintf('%s--%s', $entity_type_id, $bundle);
}
$this->resourceConfigs = $this->entityTypeManager
->getStorage('jsonapi_resource_config')
->loadMultiple($resource_config_ids);
Mateu Aguiló Bosch
committed
}
return $this->resourceConfigs;
}
/**
* 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);
}, []);
}
/**
* {@inheritdoc}
*/
public function getPathPrefix() {
return $this->configFactory
->get('jsonapi_extras.settings')
->get('path_prefix');
}