You are here

private function Update::batch in DRD Agent 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Agent/Action/Update.php \Drupal\drd_agent\Agent\Action\Update::batch()

Callback to determine and execute all required operations.

Parameters

array $start: A list of operations.

Return value

array List of results.

1 call to Update::batch()
Update::execute in src/Agent/Action/Update.php
Execute an action.

File

src/Agent/Action/Update.php, line 52

Class

Update
Provides a 'Update' code.

Namespace

Drupal\drd_agent\Agent\Action

Code

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']) {

      // Set the installed version of each module so updates will start at the
      // correct place. (The updates are already sorted, so we can simply base
      // this on the first one we come across in the above foreach loop.)
      if (isset($start[$update['module']])) {
        drupal_set_installed_schema_version($update['module'], $update['number'] - 1);
        unset($start[$update['module']]);
      }

      // Add this update function to the batch.
      $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']);
    }
  }

  // Apply post update hooks.

  /** @noinspection NullPointerExceptionInspection */
  $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;
}