class LinearHistory

Same name in other branches
  1. 10 core/lib/Drupal/Core/Config/Checkpoint/LinearHistory.php \Drupal\Core\Config\Checkpoint\LinearHistory

A chronological list of Checkpoint objects.

@internal This API is experimental.

Hierarchy

Expanded class hierarchy of LinearHistory

2 files declare their use of LinearHistory
CheckpointStorageTest.php in core/tests/Drupal/Tests/Core/Config/Checkpoint/CheckpointStorageTest.php
LinearHistoryTest.php in core/tests/Drupal/Tests/Core/Config/Checkpoint/LinearHistoryTest.php

File

core/lib/Drupal/Core/Config/Checkpoint/LinearHistory.php, line 16

Namespace

Drupal\Core\Config\Checkpoint
View source
final class LinearHistory implements CheckpointListInterface {
    
    /**
     * The store of all the checkpoint names in state.
     */
    private const CHECKPOINT_KEY = 'config.checkpoints';
    
    /**
     * The active checkpoint.
     *
     * In our implementation this is always the last in the list.
     *
     * @var \Drupal\Core\Config\Checkpoint\Checkpoint|null
     */
    private ?Checkpoint $activeCheckpoint;
    
    /**
     * The list of checkpoints, keyed by ID.
     *
     * @var \Drupal\Core\Config\Checkpoint\Checkpoint[]
     */
    private array $checkpoints;
    
    /**
     * Constructs a checkpoints object.
     *
     * @param \Drupal\Core\State\StateInterface $state
     *   The state service.
     * @param \Drupal\Component\Datetime\TimeInterface $time
     *   The time service.
     */
    public function __construct(StateInterface $state, TimeInterface $time) {
        $this->checkpoints = $this->state
            ->get(self::CHECKPOINT_KEY, []);
        $this->activeCheckpoint = end($this->checkpoints) ?: NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getActiveCheckpoint() : ?Checkpoint {
        return $this->activeCheckpoint;
    }
    
    /**
     * {@inheritdoc}
     */
    public function get(string $id) : Checkpoint {
        if (!isset($this->checkpoints[$id])) {
            throw new UnknownCheckpointException(sprintf('The checkpoint "%s" does not exist', $id));
        }
        return $this->checkpoints[$id];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getParents(string $id) : \Traversable {
        if (!isset($this->checkpoints[$id])) {
            throw new UnknownCheckpointException(sprintf('The checkpoint "%s" does not exist', $id));
        }
        $checkpoint = $this->checkpoints[$id];
        while ($checkpoint->parent !== NULL) {
            $checkpoint = $this->get($checkpoint->parent);
            (yield $checkpoint->id => $checkpoint);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getIterator() : \Traversable {
        return new \ArrayIterator($this->checkpoints);
    }
    
    /**
     * {@inheritdoc}
     */
    public function count() : int {
        return count($this->checkpoints);
    }
    
    /**
     * {@inheritdoc}
     */
    public function add(string $id, string|\Stringable $label) : Checkpoint {
        if (isset($this->checkpoints[$id])) {
            throw new CheckpointExistsException(sprintf('Cannot create a checkpoint with the ID "%s" as it already exists', $id));
        }
        $checkpoint = new Checkpoint($id, $label, $this->time
            ->getCurrentTime(), $this->activeCheckpoint?->id);
        $this->checkpoints[$checkpoint->id] = $checkpoint;
        $this->activeCheckpoint = $checkpoint;
        $this->state
            ->set(self::CHECKPOINT_KEY, $this->checkpoints);
        return $checkpoint;
    }
    
    /**
     * {@inheritdoc}
     */
    public function delete(string $id) : static {
        if (!isset($this->checkpoints[$id])) {
            throw new UnknownCheckpointException(sprintf('Cannot delete a checkpoint with the ID "%s" as it does not exist', $id));
        }
        foreach ($this->checkpoints as $key => $checkpoint) {
            unset($this->checkpoints[$key]);
            if ($checkpoint->id === $id) {
                break;
            }
        }
        $this->state
            ->set(self::CHECKPOINT_KEY, $this->checkpoints);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteAll() : static {
        $this->checkpoints = [];
        $this->activeCheckpoint = NULL;
        $this->state
            ->delete(self::CHECKPOINT_KEY);
        return $this;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
LinearHistory::$activeCheckpoint private property The active checkpoint.
LinearHistory::$checkpoints private property The list of checkpoints, keyed by ID.
LinearHistory::add public function Adds a new checkpoint. Overrides CheckpointListInterface::add
LinearHistory::CHECKPOINT_KEY private constant The store of all the checkpoint names in state.
LinearHistory::count public function
LinearHistory::delete public function Deletes a checkpoint. Overrides CheckpointListInterface::delete
LinearHistory::deleteAll public function Deletes all checkpoints. Overrides CheckpointListInterface::deleteAll
LinearHistory::get public function Gets a checkpoint. Overrides CheckpointListInterface::get
LinearHistory::getActiveCheckpoint public function Gets the active checkpoint. Overrides CheckpointListInterface::getActiveCheckpoint
LinearHistory::getIterator public function
LinearHistory::getParents public function Gets a checkpoint's parents. Overrides CheckpointListInterface::getParents
LinearHistory::__construct public function Constructs a checkpoints object.

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