function CheckpointStorage::checkpoint

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Config/Checkpoint/CheckpointStorage.php \Drupal\Core\Config\Checkpoint\CheckpointStorage::checkpoint()

Creates a checkpoint, if required, and returns the active checkpoint.

If the storage determines that the current active checkpoint would contain the same information, it does not have to create a new checkpoint.

Parameters

string|\Stringable $label: The checkpoint label to use if a new checkpoint is created.

Return value

\Drupal\Core\Config\Checkpoint\Checkpoint The currently active checkpoint.

Overrides CheckpointStorageInterface::checkpoint

File

core/lib/Drupal/Core/Config/Checkpoint/CheckpointStorage.php, line 252

Class

CheckpointStorage
Provides a config storage that can make checkpoints.

Namespace

Drupal\Core\Config\Checkpoint

Code

public function checkpoint(string|\Stringable $label) : Checkpoint {
  // Generate a new ID based on the state of the current active checkpoint.
  $active_checkpoint = $this->checkpoints
    ->getActiveCheckpoint();
  if (!$active_checkpoint instanceof Checkpoint) {
    // @todo https://www.drupal.org/i/3408525 Consider options for generating
    //   a real fingerprint.
    $id = hash('sha1', random_bytes(32));
    return $this->checkpoints
      ->add($id, $label);
  }
  // Determine if we need to create a new checkpoint by checking if
  // configuration has changed since the last checkpoint.
  $collections = $this->getAllCollectionNames();
  $collections[] = StorageInterface::DEFAULT_COLLECTION;
  foreach ($collections as $collection) {
    $current_checkpoint_data[$collection] = $this->getKeyValue($active_checkpoint->id, $collection)
      ->getAll();
    // Remove the collections key because it is irrelevant.
    unset($current_checkpoint_data[$collection][static::CONFIG_COLLECTION_KEY]);
    // If there is no data in the collection then there is no need to hash
    // the empty array.
    if (empty($current_checkpoint_data[$collection])) {
      unset($current_checkpoint_data[$collection]);
    }
  }
  if (!empty($current_checkpoint_data)) {
    // Use json_encode() here because it is both quicker and results in
    // smaller output than serialize().
    $id = hash('sha1', ($active_checkpoint->parent ?? '') . json_encode($current_checkpoint_data));
    return $this->checkpoints
      ->add($id, $label);
  }
  $this->logger?->notice('A backup checkpoint was not created because nothing has changed since the "{active}" checkpoint was created.', [
    'active' => $active_checkpoint->label,
  ]);
  return $active_checkpoint;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.