View source
<?php
namespace Drupal\update;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Utility\ProjectInfo;
class UpdateManager implements UpdateManagerInterface {
use DependencySerializationTrait;
use StringTranslationTrait;
protected $updateSettings;
protected $moduleHandler;
protected $updateProcessor;
protected $projects;
protected $keyValueStore;
protected $availableReleasesTempStore;
protected $themeHandler;
protected $moduleExtensionList;
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, UpdateProcessorInterface $update_processor, TranslationInterface $translation, KeyValueFactoryInterface $key_value_expirable_factory, ThemeHandlerInterface $theme_handler, ModuleExtensionList $extension_list_module) {
$this->updateSettings = $config_factory
->get('update.settings');
$this->moduleHandler = $module_handler;
$this->updateProcessor = $update_processor;
$this->stringTranslation = $translation;
$this->keyValueStore = $key_value_expirable_factory
->get('update');
$this->themeHandler = $theme_handler;
$this->availableReleasesTempStore = $key_value_expirable_factory
->get('update_available_releases');
$this->projects = [];
$this->moduleExtensionList = $extension_list_module;
}
public function refreshUpdateData() {
$this->keyValueStore
->delete('update_project_projects');
$this->keyValueStore
->delete('update_project_data');
$projects = $this
->getProjects();
$this->availableReleasesTempStore
->deleteAll();
foreach ($projects as $project) {
$this->updateProcessor
->createFetchTask($project);
}
}
public function getProjects() {
if (empty($this->projects)) {
$this->projects = $this
->projectStorage('update_project_projects');
if (empty($this->projects)) {
$module_data = $this->moduleExtensionList
->reset()
->getList();
$theme_data = $this->themeHandler
->rebuildThemeData();
$project_info = new ProjectInfo();
$project_info
->processInfoList($this->projects, $module_data, 'module', TRUE);
$project_info
->processInfoList($this->projects, $theme_data, 'theme', TRUE);
if ($this->updateSettings
->get('check.disabled_extensions')) {
$project_info
->processInfoList($this->projects, $module_data, 'module', FALSE);
$project_info
->processInfoList($this->projects, $theme_data, 'theme', FALSE);
}
$this->moduleHandler
->alter('update_projects', $this->projects);
$this->keyValueStore
->setWithExpire('update_project_projects', $this->projects, 3600);
}
}
return $this->projects;
}
public function projectStorage($key) {
$projects = [];
$route_names = [
'update.theme_update',
'system.modules_list',
'system.theme_install',
'update.module_update',
'update.module_install',
'update.status',
'update.report_update',
'update.report_install',
'update.settings',
'system.status',
'update.manual_status',
'update.confirmation_page',
'system.themes_page',
];
if (in_array(\Drupal::routeMatch()
->getRouteName(), $route_names)) {
$this->keyValueStore
->delete($key);
}
else {
$projects = $this->keyValueStore
->get($key, []);
}
return $projects;
}
public function fetchDataBatch(&$context) {
if (empty($context['sandbox']['max'])) {
$context['finished'] = 0;
$context['sandbox']['max'] = $this->updateProcessor
->numberOfQueueItems();
$context['sandbox']['progress'] = 0;
$context['message'] = $this
->t('Checking available update data ...');
$context['results']['updated'] = 0;
$context['results']['failures'] = 0;
$context['results']['processed'] = 0;
}
for ($i = 0; $i < 5; $i++) {
if ($item = $this->updateProcessor
->claimQueueItem()) {
if ($this->updateProcessor
->processFetchTask($item->data)) {
$context['results']['updated']++;
$context['message'] = $this
->t('Checked available update data for %title.', [
'%title' => $item->data['info']['name'],
]);
}
else {
$context['message'] = $this
->t('Failed to check available update data for %title.', [
'%title' => $item->data['info']['name'],
]);
$context['results']['failures']++;
}
$context['sandbox']['progress']++;
$context['results']['processed']++;
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
$this->updateProcessor
->deleteQueueItem($item);
}
else {
$context['finished'] = 1;
return;
}
}
}
}