class ConfigSchemaChecker

Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/Development/ConfigSchemaChecker.php \Drupal\Core\Config\Development\ConfigSchemaChecker
  2. 8.9.x core/lib/Drupal/Core/Config/Testing/ConfigSchemaChecker.php \Drupal\Core\Config\Testing\ConfigSchemaChecker
  3. 8.9.x core/lib/Drupal/Core/Config/Development/ConfigSchemaChecker.php \Drupal\Core\Config\Development\ConfigSchemaChecker
  4. 11.x core/lib/Drupal/Core/Config/Development/ConfigSchemaChecker.php \Drupal\Core\Config\Development\ConfigSchemaChecker

Listens to the config save event and validates schema.

If tests have the $strictConfigSchema property set to TRUE this event listener will be added to the container and throw exceptions if configuration is invalid.

Hierarchy

Expanded class hierarchy of ConfigSchemaChecker

See also

\Drupal\KernelTests\KernelTestBase::register()

\Drupal\Core\Test\FunctionalTestSetupTrait::prepareSettings()

2 files declare their use of ConfigSchemaChecker
FunctionalTestSetupTrait.php in core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
KernelTestBase.php in core/tests/Drupal/KernelTests/KernelTestBase.php

File

core/lib/Drupal/Core/Config/Development/ConfigSchemaChecker.php, line 25

Namespace

Drupal\Core\Config\Development
View source
class ConfigSchemaChecker implements EventSubscriberInterface {
    use SchemaCheckTrait;
    
    /**
     * The typed config manger.
     *
     * @var \Drupal\Core\Config\TypedConfigManagerInterface
     */
    protected $typedManager;
    
    /**
     * An array of config checked already. Keyed by config name and a checksum.
     *
     * @var array
     */
    protected $checked = [];
    
    /**
     * An array of config object names that are excluded from schema checking.
     *
     * @var string[]
     */
    protected $exclude = [];
    
    /**
     * Constructs the ConfigSchemaChecker object.
     *
     * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_manager
     *   The typed config manager.
     * @param string[] $exclude
     *   An array of config object names that are excluded from schema checking.
     * @param bool $validateConstraints
     *   Determines if constraints will be validated. If TRUE, constraint
     *   validation errors will be added to the errors found by
     *   SchemaCheckTrait::checkConfigSchema().
     */
    public function __construct(TypedConfigManagerInterface $typed_manager, array $exclude = [], bool $validateConstraints = FALSE) {
        $this->typedManager = $typed_manager;
        $this->exclude = $exclude;
    }
    
    /**
     * Checks that configuration complies with its schema on config save.
     *
     * @param \Drupal\Core\Config\ConfigCrudEvent $event
     *   The configuration event.
     *
     * @throws \Drupal\Core\Config\Schema\SchemaIncompleteException
     *   Exception thrown when configuration does not match its schema.
     */
    public function onConfigSave(ConfigCrudEvent $event) {
        // Only validate configuration if in the default collection. Other
        // collections may have incomplete configuration (for example language
        // overrides only). These are not valid in themselves.
        $saved_config = $event->getConfig();
        if ($saved_config->getStorage()
            ->getCollectionName() != StorageInterface::DEFAULT_COLLECTION) {
            return;
        }
        $name = $saved_config->getName();
        $data = $saved_config->get();
        $checksum = Crypt::hashBase64(serialize($data));
        if (!in_array($name, $this->exclude) && !isset($this->checked[$name . ':' . $checksum])) {
            $this->checked[$name . ':' . $checksum] = TRUE;
            $errors = $this->checkConfigSchema($this->typedManager, $name, $data, $this->validateConstraints);
            if ($errors === FALSE) {
                throw new SchemaIncompleteException("No schema for {$name}");
            }
            elseif (is_array($errors)) {
                $text_errors = [];
                foreach ($errors as $key => $error) {
                    $text_errors[] = new FormattableMarkup('@key @error', [
                        '@key' => $key,
                        '@error' => $error,
                    ]);
                }
                throw new SchemaIncompleteException("Schema errors for {$name} with the following errors: " . implode(', ', $text_errors));
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[ConfigEvents::SAVE][] = [
            'onConfigSave',
            255,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ConfigSchemaChecker::$checked protected property An array of config checked already. Keyed by config name and a checksum.
ConfigSchemaChecker::$exclude protected property An array of config object names that are excluded from schema checking.
ConfigSchemaChecker::$typedManager protected property The typed config manger.
ConfigSchemaChecker::getSubscribedEvents public static function
ConfigSchemaChecker::onConfigSave public function Checks that configuration complies with its schema on config save.
ConfigSchemaChecker::__construct public function Constructs the ConfigSchemaChecker object.
SchemaCheckTrait::$configName protected property The configuration object name under test.
SchemaCheckTrait::$ignoredPropertyPaths protected static property The ignored property paths.
SchemaCheckTrait::$schema protected property The config schema wrapper object for the configuration object under test.
SchemaCheckTrait::checkConfigSchema public function Checks the TypedConfigManager has a valid schema for the configuration.
SchemaCheckTrait::checkValue protected function Helper method to check data type.
SchemaCheckTrait::isViolationForIgnoredPropertyPath protected static function Determines whether this violation is for an ignored Config property path.

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