final class ResourceWarmer in JSON:API Boost 8
Same name and namespace in other branches
- 2.x src/Plugin/warmer/ResourceWarmer.php \Drupal\jsonapi_boost\Plugin\warmer\ResourceWarmer
Warms JSON:API resources.
Plugin annotation
@Warmer(
id = "jsonapi",
label = @Translation("JSON:API Resources"),
description = @Translation("Warms normalizations of JSON:API resources."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\warmer\Plugin\WarmerPluginBase implements ConfigurableInterface, DependentPluginInterface, ContainerFactoryPluginInterface, PluginFormInterface, WarmerInterface
- class \Drupal\jsonapi_boost\Plugin\warmer\ResourceWarmer
- class \Drupal\warmer\Plugin\WarmerPluginBase implements ConfigurableInterface, DependentPluginInterface, ContainerFactoryPluginInterface, PluginFormInterface, WarmerInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ResourceWarmer
File
- src/
Plugin/ warmer/ ResourceWarmer.php, line 23
Namespace
Drupal\jsonapi_boost\Plugin\warmerView source
final class ResourceWarmer extends WarmerPluginBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
/**
* The resource type repository.
*
* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
*/
private $resourceTypeRepository;
/**
* The entity to JSON:API service.
*
* @var \Drupal\jsonapi_extras\EntityToJsonApi
*/
private $entityToJsonapi;
/**
* The list of all item IDs for all entities in the system.
*
* Consists of <entity-type-id>:<entity-id>.
*
* @var array
*/
private $iids = [];
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var \Drupal\jsonapi_boost\Plugin\warmer\ResourceWarmer $instance */
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance
->setEntityTypeManager($container
->get('entity_type.manager'));
$instance
->setResourceTypeRepository($container
->get('jsonapi.resource_type.repository'));
$instance
->setEntityToJsonapi($container
->get('jsonapi_extras.entity.to_jsonapi'));
return $instance;
}
/**
* Injects the entity type manager.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* Injects the resource type repository.
*
* @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
* The entity type manager.
*/
public function setResourceTypeRepository(ResourceTypeRepositoryInterface $resource_type_repository) {
$this->resourceTypeRepository = $resource_type_repository;
}
/**
* Injects the entity to JSON:API service.
*
* @param \Drupal\jsonapi_extras\EntityToJsonApi $entitity_to_jsonapi
* The entity type manager.
*/
public function setEntityToJsonapi(EntityToJsonApi $entitity_to_jsonapi) {
$this->entityToJsonapi = $entitity_to_jsonapi;
}
/**
* {@inheritdoc}
*/
public function loadMultiple(array $ids = []) {
$ids_per_type = array_reduce($ids, function ($carry, $id) {
list($entity_type_id, $entity_id, ) = explode(':', $id);
if (empty($carry[$entity_type_id])) {
$carry[$entity_type_id] = [];
}
$carry[$entity_type_id][] = $entity_id;
return $carry;
}, []);
$output = [];
foreach ($ids_per_type as $entity_type_id => $entity_ids) {
$output += $this->entityTypeManager
->getStorage($entity_type_id)
->loadMultiple($entity_ids);
}
return $output;
}
/**
* {@inheritdoc}
*/
public function warmMultiple(array $items = []) {
$normalizations = array_map([
$this->entityToJsonapi,
'normalize',
], $items);
count($normalizations);
}
/**
* {@inheritdoc}
*/
public function buildIdsBatch($cursor) {
$configuration = $this
->getConfiguration();
if (empty($this->iids) && !empty($configuration['resource_types'])) {
$resource_config_ids = array_filter(array_values($configuration['resource_types']));
sort($resource_config_ids);
$this->iids = array_reduce($resource_config_ids, function ($iids, $resource_config_id) {
$resource_config = $this->entityTypeManager
->getStorage('jsonapi_resource_config')
->load($resource_config_id);
$resource_type_name = $resource_config instanceof JsonapiResourceConfig ? $resource_config
->get('resourceType') : $resource_config_id;
$resource_type = $this->resourceTypeRepository
->getByTypeName($resource_type_name);
$entity_type_id = $resource_type
->getEntityTypeId();
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$bundle_key = $entity_type
->getKey('bundle');
$id_key = $entity_type
->getKey('id');
$query = $this->entityTypeManager
->getStorage($entity_type_id)
->getQuery();
if ($id_key) {
$query
->sort($id_key);
}
if ($bundle_key) {
$query
->condition($bundle_key, $resource_type
->getBundle());
}
$results = $query
->execute();
$entity_ids = array_filter((array) array_values($results));
return array_unique(array_merge($iids, array_map(function ($id) use ($resource_config_id, $entity_type_id) {
return sprintf('%s:%s:%s', $entity_type_id, $id, $resource_config_id);
}, $entity_ids)));
}, []);
}
$cursor_position = is_null($cursor) ? -1 : array_search($cursor, $this->iids);
if ($cursor_position === FALSE) {
return [];
}
return array_slice($this->iids, $cursor_position + 1, (int) $this
->getBatchSize());
}
/**
* {@inheritdoc}
*/
public function addMoreConfigurationFormElements(array $form, SubformStateInterface $form_state) {
$options = [];
foreach ($this->resourceTypeRepository
->all() as $resource_type) {
/** @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType $resource_type */
$key = $resource_type
->getJsonapiResourceConfig()
->id();
$options[$key] = $resource_type
->getPath();
}
$configuration = $this
->getConfiguration();
$form['resource_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Resource Types'),
'#description' => $this
->t('Enable the JSON:API resource types to warm asynchronously.'),
'#options' => $options,
'#default_value' => empty($configuration['resource_types']) ? [] : $configuration['resource_types'],
];
return $form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ResourceWarmer:: |
private | property | The entity to JSON:API service. | |
ResourceWarmer:: |
private | property | The entity type manager. | |
ResourceWarmer:: |
private | property | The list of all item IDs for all entities in the system. | |
ResourceWarmer:: |
private | property | The resource type repository. | |
ResourceWarmer:: |
public | function |
Adds additional form elements to the configuration form. Overrides WarmerInterface:: |
|
ResourceWarmer:: |
public | function |
Builds the next batch of IDs based on a position cursor. Overrides WarmerInterface:: |
|
ResourceWarmer:: |
public static | function |
Creates an instance of the plugin. Overrides WarmerPluginBase:: |
|
ResourceWarmer:: |
public | function |
Loads multiple items based on their IDs. Overrides WarmerInterface:: |
|
ResourceWarmer:: |
public | function | Injects the entity to JSON:API service. | |
ResourceWarmer:: |
public | function | Injects the entity type manager. | |
ResourceWarmer:: |
public | function | Injects the resource type repository. | |
ResourceWarmer:: |
public | function |
Warms multiple items. Overrides WarmerInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WarmerPluginBase:: |
protected | property | The state service. | |
WarmerPluginBase:: |
protected | property | The time service. | |
WarmerPluginBase:: |
final public | function |
Form constructor. Overrides PluginFormInterface:: |
|
WarmerPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
WarmerPluginBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
1 |
WarmerPluginBase:: |
public | function |
Returns the batch size for the warming operation. Overrides WarmerInterface:: |
|
WarmerPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
WarmerPluginBase:: |
public | function |
Returns the frequency for the warming operation. Overrides WarmerInterface:: |
|
WarmerPluginBase:: |
public | function |
Checks if the plugin should warm in this particular moment. Overrides WarmerInterface:: |
|
WarmerPluginBase:: |
public | function |
Marks a warmer as enqueued. Overrides WarmerInterface:: |
|
WarmerPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
WarmerPluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
2 |
WarmerPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
WarmerPluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |