UpdateRunnerUtils.php in Scheduled Updates 8
File
src/UpdateRunnerUtils.php
View source
<?php
namespace Drupal\scheduled_updates;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\scheduled_updates\Entity\ScheduledUpdateType;
use Drupal\scheduled_updates\Plugin\EntityMonitorUpdateRunnerInterface;
use Drupal\scheduled_updates\Plugin\UpdateRunnerInterface;
use Drupal\scheduled_updates\Plugin\UpdateRunnerManager;
class UpdateRunnerUtils {
protected $entityFieldManager;
protected $entityTypeBundleInfo;
protected $runnerManager;
protected $updateUtils;
protected $config;
public function __construct(EntityFieldManagerInterface $entityFieldManager, EntityTypeBundleInfoInterface $entityTypeBundleInfo, UpdateRunnerManager $runnerManager, UpdateUtils $updateUtils, ConfigFactoryInterface $configFactory) {
$this->entityFieldManager = $entityFieldManager;
$this->entityTypeBundleInfo = $entityTypeBundleInfo;
$this->runnerManager = $runnerManager;
$this->updateUtils = $updateUtils;
$this->config = $configFactory
->get('scheduled_updates.settings');
}
public function getUpdateTypeRunners(array $update_types = []) {
$bundles = $this->entityTypeBundleInfo
->getBundleInfo('scheduled_update');
$runners = [];
foreach ($bundles as $bundle => $bundle_info) {
if (empty($update_types) || in_array($bundle, $update_types)) {
$updater = ScheduledUpdateType::load($bundle);
if ($runner = $this
->getUpdateRunnerInstance($updater)) {
$runners[$bundle] = $runner;
}
else {
}
}
}
return $runners;
}
public function getUpdateRunnerInstance(ScheduledUpdateTypeInterface $scheduledUpdateType) {
$runner_settings = $scheduledUpdateType
->getUpdateRunnerSettings();
$runner_settings['updater_type'] = $scheduledUpdateType
->id();
if ($this->runnerManager
->hasDefinition($runner_settings['id'])) {
return $this->runnerManager
->createInstance($runner_settings['id'], $runner_settings);
}
return NULL;
}
protected function getTimeout() {
return (int) $this->config
->get('timeout') ? $this->config
->get('timeout') : 15;
}
public function runAllUpdates(array $update_types = [], $run_by_cron = FALSE) {
$time_end = time() + $this
->getTimeout();
$runners = $this
->getUpdateTypeRunners($update_types);
foreach ($runners as $runner) {
$runner
->setRunByCron($run_by_cron);
$runner
->addUpdatesToQueue();
$runner
->runUpdatesInQueue($time_end);
if ($time_end < time()) {
break;
}
}
}
public function invokeEntityUpdate(ContentEntityInterface $entity) {
$runners = $this
->getUpdateTypeRunners();
foreach ($runners as $runner) {
if ($runner instanceof EntityMonitorUpdateRunnerInterface && $runner
->updateEntityType() == $entity
->getEntityTypeId()) {
if ($this
->entityUpdatesChanged($entity, $runner)) {
$runner
->onEntityUpdate($entity);
break;
}
}
}
}
public function isEmbeddedUpdater(ScheduledUpdateTypeInterface $scheduledUpdateType) {
return $this
->updateSupportsTypes($scheduledUpdateType, [
'embedded',
]);
}
public function isIndependentUpdater(ScheduledUpdateTypeInterface $scheduledUpdateType) {
return $this
->updateSupportsTypes($scheduledUpdateType, [
'independent',
]);
}
protected function entityUpdatesChanged(ContentEntityInterface $entity, EntityMonitorUpdateRunnerInterface $runner) {
if ($entity
->getEntityType()
->isRevisionable()) {
$previous = $this->updateUtils
->getPreviousRevision($entity);
}
else {
$previous = isset($entity->original) ? $entity->original : NULL;
}
if ($previous) {
$field_ids = $runner
->getReferencingFieldIds();
foreach ($field_ids as $field_id) {
$previous_update_ids = $runner
->getEntityReferenceTargetIds($previous, $field_id, TRUE);
$updated_update_ids = $runner
->getEntityReferenceTargetIds($entity, $field_id, TRUE);
if ($previous_update_ids != $updated_update_ids) {
return TRUE;
}
}
}
return FALSE;
}
public function updateSupportsTypes(ScheduledUpdateTypeInterface $scheduledUpdateType, $types) {
$plugin_id = $scheduledUpdateType
->getUpdateRunnerSettings()['id'];
if ($this->runnerManager
->hasDefinition($plugin_id)) {
$definition = $this->runnerManager
->getDefinition($plugin_id);
$unsupported_types = array_diff($types, $definition['update_types']);
return empty($unsupported_types);
}
return FALSE;
}
}