class AllowedScaffoldPackagesValidator

Validates the list of packages that are allowed to scaffold files.

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

File

core/modules/package_manager/src/Validator/AllowedScaffoldPackagesValidator.php, line 25

Namespace

Drupal\package_manager\Validator
View source
final class AllowedScaffoldPackagesValidator implements EventSubscriberInterface {
    use StringTranslationTrait;
    public function __construct(ComposerInspector $composerInspector, PathLocator $pathLocator) {
    }
    
    /**
     * Validates that only the implicitly allowed packages can use scaffolding.
     */
    public function validate(PreOperationStageEvent $event) : void {
        $stage = $event->stage;
        $path = $event instanceof PreApplyEvent ? $stage->getStageDirectory() : $this->pathLocator
            ->getProjectRoot();
        // @see https://www.drupal.org/docs/develop/using-composer/using-drupals-composer-scaffold
        $implicitly_allowed_packages = [
            "drupal/legacy-scaffold-assets",
            "drupal/core",
        ];
        $extra = Json::decode($this->composerInspector
            ->getConfig('extra', $path . '/composer.json'));
        $allowed_packages = $extra['drupal-scaffold']['allowed-packages'] ?? [];
        $extra_packages = array_diff($allowed_packages, $implicitly_allowed_packages);
        if (!empty($extra_packages)) {
            $event->addError(array_map($this->t(...), $extra_packages), $this->t('Any packages other than the implicitly allowed packages are not allowed to scaffold files. See <a href=":url">the scaffold documentation</a> for more information.', [
                ':url' => 'https://www.drupal.org/docs/develop/using-composer/using-drupals-composer-scaffold',
            ]));
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        return [
            StatusCheckEvent::class => 'validate',
            PreCreateEvent::class => 'validate',
            PreApplyEvent::class => 'validate',
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
AllowedScaffoldPackagesValidator::getSubscribedEvents public static function
AllowedScaffoldPackagesValidator::validate public function Validates that only the implicitly allowed packages can use scaffolding.
AllowedScaffoldPackagesValidator::__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.