function ComposerInspector::validateExecutable

Validates that the Composer executable exists in a supported version.

Throws

\Exception Thrown if the Composer executable is not available or the detected version of Composer is not supported.

2 calls to ComposerInspector::validateExecutable()
ComposerInspector::getConfig in core/modules/package_manager/src/ComposerInspector.php
Returns a config value from Composer.
ComposerInspector::validate in core/modules/package_manager/src/ComposerInspector.php
Checks that Composer commands can be run.

File

core/modules/package_manager/src/ComposerInspector.php, line 145

Class

ComposerInspector
Defines a class to get information from Composer.

Namespace

Drupal\package_manager

Code

private function validateExecutable() : void {
    $messages = [];
    // Ensure the Composer executable is available. For performance reasons,
    // statically cache the result, since it's unlikely to change during the
    // current request. If $unavailable_message is NULL, it means we haven't
    // done this check yet. If it's FALSE, it means we did the check and there
    // were no errors; and, if it's a string, it's the error message we received
    // the last time we did this check.
    static $unavailable_message;
    if ($unavailable_message === NULL) {
        try {
            // The "Composer is available" precondition requires active and stage
            // directories, but they don't actually matter to it, nor do path
            // exclusions, so dummies can be passed for simplicity.
            $active_dir = $this->pathFactory
                ->create(__DIR__);
            $stage_dir = $active_dir;
            $this->composerIsAvailable
                ->assertIsFulfilled($active_dir, $stage_dir);
            $unavailable_message = FALSE;
        } catch (PreconditionException $e) {
            $unavailable_message = $e->getMessage();
        }
    }
    if ($unavailable_message) {
        $messages[] = $unavailable_message;
    }
    // The detected version of Composer is unlikely to change during the
    // current request, so statically cache it. If $unsupported_message is NULL,
    // it means we haven't done this check yet. If it's FALSE, it means we did
    // the check and there were no errors; and, if it's a string, it's the error
    // message we received the last time we did this check.
    static $unsupported_message;
    if ($unsupported_message === NULL) {
        try {
            $detected_version = $this->getVersion();
            if (Semver::satisfies($detected_version, static::SUPPORTED_VERSION)) {
                // We did the version check, and it did not produce an error message.
                $unsupported_message = FALSE;
            }
            else {
                $unsupported_message = $this->t('The detected Composer version, @version, does not satisfy <code>@constraint</code>.', [
                    '@version' => $detected_version,
                    '@constraint' => static::SUPPORTED_VERSION,
                ]);
            }
        } catch (\UnexpectedValueException $e) {
            $unsupported_message = $e->getMessage();
        }
    }
    if ($unsupported_message) {
        $messages[] = $unsupported_message;
    }
    if ($messages) {
        throw new ComposerNotReadyException(NULL, $messages);
    }
}

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