function StageBase::require

Adds or updates packages in the stage directory.

Parameters

string[] $runtime: The packages to add as regular top-level dependencies, in the form 'vendor/name' or 'vendor/name:version'.

string[] $dev: (optional) The packages to add as dev dependencies, in the form 'vendor/name' or 'vendor/name:version'. Defaults to an empty array.

int|null $timeout: (optional) How long to allow the Composer operation to run before timing out, in seconds, or NULL to never time out. Defaults to 300 seconds.

Throws

\Drupal\package_manager\Exception\StageException Thrown if the Composer operation cannot be started, or if an error occurs during the operation. In the latter situation, the stage directory will be destroyed.

File

core/modules/package_manager/src/StageBase.php, line 392

Class

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

Namespace

Drupal\package_manager

Code

public function require(array $runtime, array $dev = [], ?int $timeout = 300) : void {
    $this->checkOwnership();
    $this->dispatch(new PreRequireEvent($this, $runtime, $dev));
    // A helper function to execute a command in the stage, destroying it if an
    // exception occurs in the middle of a Composer operation.
    $do_stage = function (array $command) use ($timeout) : void {
        $active_dir = $this->pathFactory
            ->create($this->pathLocator
            ->getProjectRoot());
        $stage_dir = $this->pathFactory
            ->create($this->getStageDirectory());
        try {
            $this->stager
                ->stage($command, $active_dir, $stage_dir, NULL, $timeout);
        } catch (\Throwable $e) {
            // If the caught exception isn't InvalidArgumentException or
            // PreconditionException, a Composer operation was actually attempted,
            // and failed. The stage should therefore be destroyed, because it's in
            // an indeterminate and possibly unrecoverable state.
            if (!$e instanceof InvalidArgumentException && !$e instanceof PreconditionException) {
                $this->destroy();
            }
            $this->rethrowAsStageException($e);
        }
    };
    // Change the runtime and dev requirements as needed, but don't update
    // the installed packages yet.
    if ($runtime) {
        self::validateRequirements($runtime);
        $command = array_merge([
            'require',
            '--no-update',
        ], $runtime);
        $do_stage($command);
    }
    if ($dev) {
        self::validateRequirements($dev);
        $command = array_merge([
            'require',
            '--dev',
            '--no-update',
        ], $dev);
        $do_stage($command);
    }
    // If constraints were changed, update those packages.
    if ($runtime || $dev) {
        $command = array_merge([
            'update',
            '--with-all-dependencies',
            '--optimize-autoloader',
        ], $runtime, $dev);
        $do_stage($command);
    }
    $this->dispatch(new PostRequireEvent($this, $runtime, $dev));
}

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