class PendingUpdatesValidator

Validates that there are no pending database updates.

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

File

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

Namespace

Drupal\package_manager\Validator
View source
final class PendingUpdatesValidator implements EventSubscriberInterface {
    use StringTranslationTrait;
    public function __construct(string $appRoot, UpdateRegistry $updateRegistry) {
    }
    
    /**
     * Validates that there are no pending database updates.
     */
    public function validate(PreOperationStageEvent $event) : void {
        if ($this->updatesExist()) {
            $message = $this->t('Some modules have database updates pending. You should run the <a href=":update">database update script</a> immediately.', [
                ':update' => Url::fromRoute('system.db_update')->toString(),
            ]);
            $event->addError([
                $message,
            ]);
        }
    }
    
    /**
     * Checks if there are any pending update or post-update hooks.
     *
     * @return bool
     *   TRUE if there are any pending update or post-update hooks, FALSE
     *   otherwise.
     */
    public function updatesExist() : bool {
        require_once $this->appRoot . '/core/includes/install.inc';
        require_once $this->appRoot . '/core/includes/update.inc';
        drupal_load_updates();
        $hook_updates = update_get_update_list();
        $post_updates = $this->updateRegistry
            ->getPendingUpdateFunctions();
        return $hook_updates || $post_updates;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        return [
            PreCreateEvent::class => 'validate',
            StatusCheckEvent::class => 'validate',
            PreApplyEvent::class => 'validate',
        ];
    }

}

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