You are here

trait ConfigReadonlyWhitelistTrait in Configuration Read-only mode 8

Trait ConfigReadonlyWhitelistTrait.

@package Drupal\config_readonly

Hierarchy

2 files declare their use of ConfigReadonlyWhitelistTrait
ConfigReadonlyStorage.php in src/Config/ConfigReadonlyStorage.php
ReadOnlyFormSubscriber.php in src/EventSubscriber/ReadOnlyFormSubscriber.php

File

src/ConfigReadonlyWhitelistTrait.php, line 12

Namespace

Drupal\config_readonly
View source
trait ConfigReadonlyWhitelistTrait {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * An array to store the whitelist ignore patterns.
   *
   * @var string[]
   */
  protected $patterns = [];

  /**
   * Set the module handler.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke hooks.
   */
  protected function setModuleHandler(ModuleHandlerInterface $module_handler) {
    $this->moduleHandler = $module_handler;
  }

  /**
   * Get whitelist patterns.
   *
   * @return string[]
   *   The whitelist patterns.
   */
  protected function getWhitelistPatterns() {
    if (!$this->patterns) {
      $this->patterns = $this->moduleHandler
        ->invokeAll('config_readonly_whitelist_patterns');
    }
    return $this->patterns;
  }

  /**
   * Check if the given name matches any whitelist pattern.
   *
   * @param string $name
   *   The config name.
   *
   * @return bool
   *   Whether or not there is a match.
   */
  protected function matchesWhitelistPattern($name) {

    // Check for matches.
    $patterns = $this
      ->getWhitelistPatterns();
    if ($patterns) {
      foreach ($patterns as $pattern) {
        $escaped = str_replace('\\*', '.*', preg_quote($pattern, '/'));
        if (preg_match('/^' . $escaped . '$/', $name)) {
          return TRUE;
        }
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigReadonlyWhitelistTrait::$moduleHandler protected property The module handler.
ConfigReadonlyWhitelistTrait::$patterns protected property An array to store the whitelist ignore patterns.
ConfigReadonlyWhitelistTrait::getWhitelistPatterns protected function Get whitelist patterns.
ConfigReadonlyWhitelistTrait::matchesWhitelistPattern protected function Check if the given name matches any whitelist pattern.
ConfigReadonlyWhitelistTrait::setModuleHandler protected function Set the module handler.