You are here

class StatusOverride in Configuration Split 2.0.x

A config override for config split based on the drupal state.

Hierarchy

Expanded class hierarchy of StatusOverride

6 files declare their use of StatusOverride
ConfigSplitActivateForm.php in src/Form/ConfigSplitActivateForm.php
ConfigSplitCliService.php in src/ConfigSplitCliService.php
ConfigSplitDeactivateForm.php in src/Form/ConfigSplitDeactivateForm.php
ConfigSplitEntityForm.php in src/Form/ConfigSplitEntityForm.php
ConfigSplitEntityListBuilder.php in src/ConfigSplitEntityListBuilder.php

... See full list

1 string reference to 'StatusOverride'
config_split.services.yml in ./config_split.services.yml
config_split.services.yml
1 service uses StatusOverride
config_split.status_override in ./config_split.services.yml
Drupal\config_split\Config\StatusOverride

File

src/Config/StatusOverride.php, line 15

Namespace

Drupal\config_split\Config
View source
class StatusOverride implements ConfigFactoryOverrideInterface {

  /**
   * The drupal state.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The cache invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheInvalidator;

  /**
   * The service constructor.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The drupal state.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cacheInvalidator
   *   The cache invalidator.
   */
  public function __construct(StateInterface $state, CacheTagsInvalidatorInterface $cacheInvalidator) {
    $this->state = $state;
    $this->cacheInvalidator = $cacheInvalidator;
  }

  /**
   * Set a config split state locally.
   *
   * @param string $name
   *   The name of the config split.
   * @param bool|null $active
   *   The state, null to reset.
   */
  public function setSplitOverride(string $name, bool $active = NULL) {
    $name = self::fixName($name);
    $overrides = $this->state
      ->get('config_split_override_state', []);
    if ($active === NULL) {
      unset($overrides[$name]);
    }
    else {
      $overrides[$name] = $active;
    }
    $this->state
      ->set('config_split_override_state', $overrides);
    $this->cacheInvalidator
      ->invalidateTags($this
      ->getCacheableMetadata('config_split.config_split.' . $name)
      ->getCacheTags());
  }

  /**
   * Get the split override setting.
   *
   * @param string $name
   *   The name of the split.
   *
   * @return bool|null
   *   The state, null if not managed.
   */
  public function getSplitOverride(string $name) {
    $name = self::fixName($name);
    $overrides = $this->state
      ->get('config_split_override_state', []);
    if (isset($overrides[$name])) {
      return (bool) $overrides[$name];
    }
    return NULL;
  }

  /**
   * Check settings.php for overrides.
   *
   * @param string $name
   *   The name of the split.
   *
   * @return bool|null
   *   The overridden status from settings.php
   */
  public function getSettingsOverride(string $name) {
    $name = 'config_split.config_split.' . self::fixName($name);

    // This assumes that the config is overwritten as recommended.
    if (isset($GLOBALS['config'][$name], $GLOBALS['config'][$name]['status'])) {
      return (bool) $GLOBALS['config'][$name]['status'];
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function loadOverrides($names) {
    $overrides = [];
    foreach ($this->state
      ->get('config_split_override_state', []) as $name => $status) {
      $name = 'config_split.config_split.' . $name;
      if (in_array($name, $names)) {
        $overrides = $overrides + [
          $name => [
            'status' => (bool) $status,
          ],
        ];
      }
    }
    return $overrides;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheSuffix() {
    return 'config_split_state';
  }

  /**
   * {@inheritdoc}
   */
  public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata($name) {
    $metadata = new CacheableMetadata();
    $metadata
      ->setCacheTags([
      $this
        ->getCacheSuffix() . ':' . $name,
      'config:' . $name,
    ])
      ->setCacheMaxAge(Cache::PERMANENT);
    return $metadata;
  }

  /**
   * Make sure the split name is just the machine name.
   *
   * @param string $name
   *   The split name.
   *
   * @return string
   *   The split name.
   */
  private static function fixName(string $name) : string {
    if (strpos($name, 'config_split.config_split.') === 0) {
      return substr($name, strlen('config_split.config_split.'));
    }
    return $name;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StatusOverride::$cacheInvalidator protected property The cache invalidator.
StatusOverride::$state protected property The drupal state.
StatusOverride::createConfigObject public function Creates a configuration object for use during install and synchronization. Overrides ConfigFactoryOverrideInterface::createConfigObject
StatusOverride::fixName private static function Make sure the split name is just the machine name.
StatusOverride::getCacheableMetadata public function Gets the cacheability metadata associated with the config factory override. Overrides ConfigFactoryOverrideInterface::getCacheableMetadata
StatusOverride::getCacheSuffix public function The string to append to the configuration static cache name. Overrides ConfigFactoryOverrideInterface::getCacheSuffix
StatusOverride::getSettingsOverride public function Check settings.php for overrides.
StatusOverride::getSplitOverride public function Get the split override setting.
StatusOverride::loadOverrides public function Returns config overrides. Overrides ConfigFactoryOverrideInterface::loadOverrides
StatusOverride::setSplitOverride public function Set a config split state locally.
StatusOverride::__construct public function The service constructor.