function SandboxManagerBase::claim

Attempts to claim the stage.

Once a stage has been created, no operations can be performed on it until it is claimed. This is to ensure that stage operations across multiple requests are being done by the same code, running under the same user or session that created the stage in the first place. To claim a stage, the calling code must provide the unique identifier that was generated when the stage was created.

The stage is claimed when it is created, so external code does NOT need to call this method after calling ::create() in the same request.

Parameters

string $unique_id: The unique ID that was returned by ::create().

Return value

$this

Throws

\Drupal\package_manager\Exception\SandboxOwnershipException If the stage cannot be claimed. This can happen if the current user or session did not originally create the stage, if $unique_id doesn't match the unique ID that was generated when the stage was created, or the current class is not the same one that was used to create the stage.

See also

::create()

1 call to SandboxManagerBase::claim()
SandboxManagerBase::create in core/modules/package_manager/src/SandboxManagerBase.php
Copies the active code base into the stage directory.

File

core/modules/package_manager/src/SandboxManagerBase.php, line 642

Class

SandboxManagerBase
Creates and manages a stage directory in which to install or update code.

Namespace

Drupal\package_manager

Code

public final function claim(string $unique_id) : self {
    $this->failureMarker
        ->assertNotExists();
    if ($this->isAvailable()) {
        // phpcs:disable DrupalPractice.General.ExceptionT.ExceptionT
        // @see https://www.drupal.org/project/auto_updates/issues/3338651
        throw new SandboxException($this, $this->computeDestroyMessage($unique_id, $this->t('Cannot claim the stage because no stage has been created.'))
            ->render());
    }
    $stored_lock = $this->tempStore
        ->getIfOwner(static::TEMPSTORE_LOCK_KEY);
    if (!$stored_lock) {
        throw new SandboxOwnershipException($this, $this->computeDestroyMessage($unique_id, $this->t('Cannot claim the stage because it is not owned by the current user or session.'))
            ->render());
    }
    if ($stored_lock === [
        $unique_id,
        static::class,
        $this->getType(),
    ]) {
        $this->lock = $stored_lock;
        return $this;
    }
    throw new SandboxOwnershipException($this, $this->computeDestroyMessage($unique_id, $this->t('Cannot claim the stage because the current lock does not match the stored lock.'))
        ->render());
    // phpcs:enable DrupalPractice.General.ExceptionT.ExceptionT
}

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