class EnvironmentSupportValidator

Checks that the environment has support for Package Manager.

@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 EnvironmentSupportValidator

1 file declares its use of EnvironmentSupportValidator
EnvironmentSupportValidatorTest.php in core/modules/package_manager/tests/src/Kernel/EnvironmentSupportValidatorTest.php

File

core/modules/package_manager/src/Validator/EnvironmentSupportValidator.php, line 20

Namespace

Drupal\package_manager\Validator
View source
final class EnvironmentSupportValidator implements EventSubscriberInterface {
    use BaseRequirementValidatorTrait {
        getSubscribedEvents as private getSubscribedEventsFromTrait;
    }
    use StringTranslationTrait;
    
    /**
     * The name of the environment variable to check.
     *
     * This environment variable, if defined, should be parseable by
     * \Drupal\Core\Url::fromUri() and link to an explanation of why Package
     * Manager is not supported in the current environment.
     *
     * @var string
     */
    public const VARIABLE_NAME = 'DRUPAL_PACKAGE_MANAGER_NOT_SUPPORTED_HELP_URL';
    
    /**
     * Checks that this environment supports Package Manager.
     */
    public function validate(SandboxValidationEvent $event) : void {
        $message = $this->t('Package Manager is not supported by your environment.');
        $help_url = getenv(static::VARIABLE_NAME);
        if (empty($help_url)) {
            return;
        }
        // If the URL is not parseable, catch the exception that Url::fromUri()
        // would generate.
        try {
            $message = $this->t('<a href=":url">@message</a>', [
                ':url' => Url::fromUri($help_url)->toString(),
                '@message' => $message,
            ]);
        } catch (\InvalidArgumentException) {
            // No need to do anything here. The message just won't be a link.
        }
        $event->addError([
            $message,
        ]);
        // If Package Manager is unsupported, there's no point in doing any more
        // validation.
        $event->stopPropagation();
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        // Set priority to run before BaseRequirementsFulfilledValidator, and even
        // before other base requirement validators.
        // @see \Drupal\package_manager\Validator\BaseRequirementsFulfilledValidator
        return array_map(fn() => [
            'validate',
            BaseRequirementsFulfilledValidator::PRIORITY + 1000,
        ], static::getSubscribedEventsFromTrait());
    }

}

Members

Title Sort descending Modifiers Object type Summary Member alias Overriden Title Overrides
BaseRequirementValidatorTrait::getSubscribedEvents public static function Implements EventSubscriberInterface::getSubscribedEvents(). Aliased as: getSubscribedEventsFromTrait
EnvironmentSupportValidator::getSubscribedEvents public static function
EnvironmentSupportValidator::validate public function Checks that this environment supports Package Manager. Overrides BaseRequirementValidatorTrait::validate
EnvironmentSupportValidator::VARIABLE_NAME public constant The name of the environment variable to check.
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. 1

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