function StageBase::apply
Applies staged changes to the active directory.
After the staged changes are applied, the current request should be terminated as soon as possible. This is because the code loaded into the PHP runtime may no longer match the code that is physically present in the active code base, which means that the current request is running in an unreliable, inconsistent environment. In the next request, ::postApply() should be called as early as possible after Drupal is fully bootstrapped, to rebuild the service container, flush caches, and dispatch the post-apply event.
Parameters
int|null $timeout: (optional) How long to allow the file copying operation to run before timing out, in seconds, or NULL to never time out. Defaults to 600 seconds.
Throws
\Drupal\package_manager\Exception\ApplyFailedException Thrown if there is an error calling Composer Stager, which may indicate a failed commit operation.
File
-
core/
modules/ package_manager/ src/ StageBase.php, line 460
Class
- StageBase
- Creates and manages a stage directory in which to install or update code.
Namespace
Drupal\package_managerCode
public function apply(?int $timeout = 600) : void {
$this->checkOwnership();
$active_dir = $this->pathFactory
->create($this->pathLocator
->getProjectRoot());
$stage_dir = $this->pathFactory
->create($this->getStageDirectory());
$excluded_paths = $this->getPathsToExclude();
$event = new PreApplyEvent($this, $excluded_paths);
// If an error occurs while dispatching the events, ensure that ::destroy()
// doesn't think we're in the middle of applying the staged changes to the
// active directory.
$this->tempStore
->set(self::TEMPSTORE_APPLY_TIME_KEY, $this->time
->getRequestTime());
$this->dispatch($event, $this->setNotApplying(...));
// Create a marker file so that we can tell later on if the commit failed.
$this->failureMarker
->write($this, $this->getFailureMarkerMessage());
try {
$this->committer
->commit($stage_dir, $active_dir, $excluded_paths, NULL, $timeout);
} catch (InvalidArgumentException|PreconditionException $e) {
// The commit operation has not started yet, so we can clear the failure
// marker and release the flag that says we're applying.
$this->setNotApplying();
$this->failureMarker
->clear();
$this->rethrowAsStageException($e);
} catch (\Throwable $throwable) {
// The commit operation may have failed midway through, and the site code
// is in an indeterminate state. Release the flag which says we're still
// applying, because in this situation, the site owner should probably
// restore everything from a backup.
$this->setNotApplying();
// Update the marker file with the information from the throwable.
$this->failureMarker
->write($this, $this->getFailureMarkerMessage(), $throwable);
throw new ApplyFailedException($this, $this->failureMarker
->getMessage(), $throwable->getCode(), $throwable);
}
$this->failureMarker
->clear();
$this->setMetadata(self::TEMPSTORE_CHANGES_APPLIED, TRUE);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.