Update.php in DRD Agent 4.0.x
File
src/Agent/Action/Update.php
View source
<?php
namespace Drupal\drd_agent\Agent\Action;
class Update extends Base {
public function execute() {
require_once DRUPAL_ROOT . '/core/includes/install.inc';
require_once DRUPAL_ROOT . '/core/includes/update.inc';
drupal_load_updates();
$pending = update_get_update_list();
$post_updates = $this->container
->get('update.post_update_registry')
->getPendingUpdateInformation();
$result = [];
$start = [];
if (count($pending) || count($post_updates)) {
foreach ([
'update',
'post_update',
] as $update_type) {
$updates = $update_type === 'update' ? $pending : $post_updates;
foreach ($updates as $module => $module_updates) {
if (isset($module_updates['start'])) {
$start[$module] = $module_updates['start'];
}
}
}
$result = $this
->batch($start);
}
return $result;
}
private function batch(array $start) : array {
$result = [];
$updates = update_resolve_dependencies($start);
$dependency_map = [];
foreach ($updates as $function => $update) {
$dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : [];
}
$operations = [];
foreach ($updates as $update) {
if ($update['allowed']) {
if (isset($start[$update['module']])) {
drupal_set_installed_schema_version($update['module'], $update['number'] - 1);
unset($start[$update['module']]);
}
$function = $update['module'] . '_update_' . $update['number'];
$operations[] = [
'update_do_one',
[
$update['module'],
$update['number'],
$dependency_map[$function],
],
];
$this->messenger
->addMessage('Updating ' . $update['module'] . ': version ' . $update['number']);
}
}
$post_updates = $this->container
->get('update.post_update_registry')
->getPendingUpdateFunctions();
if ($post_updates) {
$operations[] = [
'drupal_flush_all_caches',
[],
];
foreach ($post_updates as $function) {
$operations[] = [
'update_invoke_post_update',
[
$function,
],
];
}
}
$maintenanceModeOriginalState = $this->state
->get('system.maintenance_mode');
$this->state
->set('system.maintenance_mode', TRUE);
$context = [
'sandbox' => [],
'results' => [],
'message' => '',
];
$this
->batchProcess($operations, $context);
if (!empty($context['results']['#abort'])) {
$result['failed'] = TRUE;
}
$this
->captureUpdateMessages($context['results']);
$this->state
->set('system.maintenance_mode', $maintenanceModeOriginalState);
return $result;
}
private function batchProcess($operations, &$context) {
foreach ($operations as $operation) {
$context['finished'] = FALSE;
$context['sandbox']['#finished'] = TRUE;
$operation[1][] =& $context;
$finished = FALSE;
while (!$finished) {
call_user_func_array($operation[0], $operation[1]);
$finished = !empty($context['finished']) || !empty($context['sandbox']['#finished']);
}
}
}
private function captureUpdateMessages(array $results) {
foreach ($results as $module => $updates) {
if ($module !== '#abort') {
foreach ($updates as $number => $queries) {
foreach ($queries as $query) {
if (empty($query['query'])) {
continue;
}
if ($query['success']) {
$this->messenger
->addMessage($query['query']);
}
else {
$this->messenger
->addMessage($query['query'], 'error');
}
}
}
}
}
}
}