Commit e75b4952 authored by Fabian Bircher's avatar Fabian Bircher
Browse files

Issue #3263968 by bircher: separate state config override services to attempt...

Issue #3263968 by bircher: separate state config override services to attempt to avoid circular references in some cases
parent 8005ffaa
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -43,5 +43,10 @@ services:
    arguments:
      - "@state"
      - "@cache_tags.invalidator"

  config_split.status_config_factory_override:
    class: Drupal\config_split\Config\StatusConfigFactoryOverride
    arguments:
      - "@state"
    tags:
      - { name: config.factory.override, priority: 5 }
+71 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\config_split\Config;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\State\StateInterface;

/**
 * A config factory override for config split based on the drupal state.
 *
 * @see \Drupal\config_split\Config\StatusOverride
 */
class StatusConfigFactoryOverride implements ConfigFactoryOverrideInterface {

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

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

  /**
   * {@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) {
    return new CacheableMetadata();
  }

}
+5 −43
Original line number Diff line number Diff line
@@ -2,16 +2,15 @@

namespace Drupal\config_split\Config;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\State\StateInterface;

/**
 * A config override for config split based on the drupal state.
 * A service for config override for config split based on the drupal state.
 *
 * @see \Drupal\config_split\Config\StatusConfigFactoryOverride
 */
class StatusOverride implements ConfigFactoryOverrideInterface {
class StatusOverride {

  /**
   * The drupal state.
@@ -58,7 +57,7 @@ public function setSplitOverride(string $name, bool $active = NULL) {
      $overrides[$name] = $active;
    }
    $this->state->set('config_split_override_state', $overrides);
    $this->cacheInvalidator->invalidateTags($this->getCacheableMetadata('config_split.config_split.' . $name)->getCacheTags());
    $this->cacheInvalidator->invalidateTags(['config:config_split.config_split.' . $name]);
  }

  /**
@@ -97,43 +96,6 @@ public function getSettingsOverride(string $name) {
    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) {
    return new CacheableMetadata();
  }

  /**
   * Make sure the split name is just the machine name.
   *