View source
<?php
namespace Drupal\jsonapi_boost\Plugin\warmer;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\SubformStateInterface;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
use Drupal\jsonapi_extras\Entity\JsonapiResourceConfig;
use Drupal\jsonapi_extras\EntityToJsonApi;
use Drupal\warmer\Plugin\WarmerPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class ResourceWarmer extends WarmerPluginBase {
private $entityTypeManager;
private $resourceTypeRepository;
private $entityToJsonapi;
private $iids = [];
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$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;
}
public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public function setResourceTypeRepository(ResourceTypeRepositoryInterface $resource_type_repository) {
$this->resourceTypeRepository = $resource_type_repository;
}
public function setEntityToJsonapi(EntityToJsonApi $entitity_to_jsonapi) {
$this->entityToJsonapi = $entitity_to_jsonapi;
}
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;
}
public function warmMultiple(array $items = []) {
$normalizations = array_map([
$this->entityToJsonapi,
'normalize',
], $items);
count($normalizations);
}
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());
}
public function addMoreConfigurationFormElements(array $form, SubformStateInterface $form_state) {
$options = [];
foreach ($this->resourceTypeRepository
->all() as $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;
}
}