class ComposerValidator

Validates the project can be used by the Composer Inspector.

@internal This is an internal part of Package Manager and may be changed or removed at any time without warning. External code should not interact with this class.

Hierarchy

Expanded class hierarchy of ComposerValidator

File

core/modules/package_manager/src/Validator/ComposerValidator.php, line 24

Namespace

Drupal\package_manager\Validator
View source
final class ComposerValidator implements EventSubscriberInterface {
    use BaseRequirementValidatorTrait;
    use StringTranslationTrait;
    public function __construct(ComposerInspector $composerInspector, PathLocator $pathLocator, ModuleHandlerInterface $moduleHandler) {
    }
    
    /**
     * Validates that the Composer executable is the correct version.
     */
    public function validate(PreOperationStageEvent $event) : void {
        // If we can't stat processes, there's nothing else we can possibly do here.
        // @see \Symfony\Component\Process\Process::__construct()
        if (!\function_exists('proc_open')) {
            $message = $this->t('Composer cannot be used because the <code>proc_open()</code> function is disabled.');
            if ($this->moduleHandler
                ->moduleExists('help')) {
                $message = $this->t('@message See <a href=":package-manager-help">the help page</a> for information on how to resolve the problem.', [
                    '@message' => $message,
                    ':package-manager-help' => self::getHelpUrl('package-manager-composer-related-faq'),
                ]);
            }
            $event->addError([
                $message,
            ]);
            return;
        }
        $messages = [];
        $dir = $event instanceof PreApplyEvent ? $event->stage
            ->getStageDirectory() : $this->pathLocator
            ->getProjectRoot();
        try {
            $this->composerInspector
                ->validate($dir);
        } catch (\Throwable $e) {
            if ($this->moduleHandler
                ->moduleExists('help')) {
                $message = $this->t('@message See <a href=":package-manager-help">the help page</a> for information on how to resolve the problem.', [
                    '@message' => $e->getMessage(),
                    ':package-manager-help' => self::getHelpUrl('package-manager-composer-related-faq'),
                ]);
                $event->addError([
                    $message,
                ]);
            }
            else {
                $event->addErrorFromThrowable($e);
            }
            return;
        }
        $settings = [];
        foreach ([
            'disable-tls',
            'secure-http',
        ] as $key) {
            try {
                $settings[$key] = json_decode($this->composerInspector
                    ->getConfig($key, $dir));
            } catch (\Throwable $e) {
                $event->addErrorFromThrowable($e, $this->t('Unable to determine Composer <code>@key</code> setting.', [
                    '@key' => $key,
                ]));
                return;
            }
        }
        // If disable-tls is enabled, it overrides secure-http and sets its value to
        // FALSE, even if secure-http is set to TRUE explicitly.
        if ($settings['disable-tls'] === TRUE) {
            $message = $this->t('TLS must be enabled for HTTPS Composer downloads.');
            // If the Help module is installed, link to our help page, which displays
            // the commands for configuring Composer correctly. Otherwise, direct
            // users straight to the Composer documentation, which is a little less
            // helpful.
            if ($this->moduleHandler
                ->moduleExists('help')) {
                $messages[] = $this->t('@message See <a href=":url">the help page</a> for more information on how to configure Composer to download packages securely.', [
                    '@message' => $message,
                    ':url' => self::getHelpUrl('package-manager-requirements'),
                ]);
            }
            else {
                $messages[] = $this->t('@message See <a href=":url">the Composer documentation</a> for more information.', [
                    '@message' => $message,
                    ':url' => 'https://getcomposer.org/doc/06-config.md#disable-tls',
                ]);
            }
            $messages[] = $this->t('You should also check the value of <code>secure-http</code> and make sure that it is set to <code>true</code> or not set at all.');
        }
        elseif ($settings['secure-http'] !== TRUE) {
            $message = $this->t('HTTPS must be enabled for Composer downloads.');
            if ($this->moduleHandler
                ->moduleExists('help')) {
                $messages[] = $this->t('@message See <a href=":url">the help page</a> for more information on how to configure Composer to download packages securely.', [
                    '@message' => $message,
                    ':url' => self::getHelpUrl('package-manager-requirements'),
                ]);
            }
            else {
                $messages[] = $this->t('@message See <a href=":url">the Composer documentation</a> for more information.', [
                    '@message' => $message,
                    ':url' => 'https://getcomposer.org/doc/06-config.md#secure-http',
                ]);
            }
        }
        if ($messages) {
            $event->addError($messages, $this->t("Composer settings don't satisfy Package Manager's requirements."));
        }
    }
    
    /**
     * Returns a URL to a specific fragment of Package Manager's online help.
     *
     * @param string $fragment
     *   The fragment to link to.
     *
     * @return string
     *   A URL to Package Manager's online help.
     */
    private static function getHelpUrl(string $fragment) : string {
        return Url::fromRoute('help.page', [
            'name' => 'package_manager',
        ])->setOption('fragment', $fragment)
            ->toString();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BaseRequirementValidatorTrait::getSubscribedEvents public static function Implements EventSubscriberInterface::getSubscribedEvents().
ComposerValidator::getHelpUrl private static function Returns a URL to a specific fragment of Package Manager&#039;s online help.
ComposerValidator::validate public function Validates that the Composer executable is the correct version. Overrides BaseRequirementValidatorTrait::validate
ComposerValidator::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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