You are here

class TestSplitFilter in Config Filter 8

Same name and namespace in other branches
  1. 8.2 tests/modules/config_filter_split_test/src/Plugin/ConfigFilter/TestSplitFilter.php \Drupal\config_filter_split_test\Plugin\ConfigFilter\TestSplitFilter

Provides a TestSplitFilter.

This is a very basic split filter to test that config_filter applies the filters correctly. For more advanced and configurable split filters use the Configuration Split (config_split) module.

Plugin annotation


@ConfigFilter(
  id = "config_filter_split_test",
  label = @Translation("Filter Split test"),
  storages = {"test_storage"},
)

Hierarchy

Expanded class hierarchy of TestSplitFilter

File

tests/modules/config_filter_split_test/src/Plugin/ConfigFilter/TestSplitFilter.php, line 24

Namespace

Drupal\config_filter_split_test\Plugin\ConfigFilter
View source
class TestSplitFilter extends ConfigFilterBase implements ContainerFactoryPluginInterface {

  /**
   * The File storage to read the migrations from.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $storage;

  /**
   * The name prefix to split.
   *
   * @var string
   */
  protected $name;

  /**
   * Constructs a new TestSplitFilter.
   *
   * @param \Drupal\Core\Config\StorageInterface $storage
   *   The migrate storage.
   * @param string $name
   *   The config name prefix to split.
   */
  public function __construct(StorageInterface $storage, $name) {
    parent::__construct([], 'config_filter_split_test', []);
    $this->storage = $storage;
    $this->name = $name;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(new DatabaseStorage($container
      ->get('database'), 'config_filter_split_test'), 'core.');
  }

  /**
   * Decide to split the config off or not.
   *
   * @param string $name
   *   The name of the configuration to check.
   *
   * @return bool
   *   Whether the configuration is supposed to be split.
   */
  protected function isSplitConfig($name) {
    return strpos($name, $this->name) === 0;
  }

  /**
   * {@inheritdoc}
   */
  public function filterRead($name, $data) {
    if ($this
      ->isSplitConfig($name)) {
      if ($this->storage
        ->exists($name)) {
        $data = $this->storage
          ->read($name);
      }
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function filterExists($name, $exists) {
    if ($this
      ->isSplitConfig($name) && !$exists) {
      $exists = $this->storage
        ->exists($name);
    }
    return $exists;
  }

  /**
   * {@inheritdoc}
   */
  public function filterReadMultiple(array $names, array $data) {
    return array_merge($data, $this->storage
      ->readMultiple($names));
  }

  /**
   * {@inheritdoc}
   */
  public function filterListAll($prefix, array $data) {
    return array_unique(array_merge($data, $this->storage
      ->listAll($prefix)));
  }

  /**
   * {@inheritdoc}
   */
  public function filterWrite($name, array $data) {
    if ($this
      ->isSplitConfig($name)) {
      $this->storage
        ->write($name, $data);
      return NULL;
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function filterWriteEmptyIsDelete($name) {
    return $this
      ->isSplitConfig($name) ? TRUE : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function filterDelete($name, $delete) {
    if ($delete && $this->storage
      ->exists($name)) {

      // Call delete on the secondary storage anyway.
      $this->storage
        ->delete($name);
    }
    return $delete;
  }

  /**
   * {@inheritdoc}
   */
  public function filterDeleteAll($prefix, $delete) {
    if ($delete && $this->storage) {
      try {
        $this->storage
          ->deleteAll($prefix);
      } catch (\UnexpectedValueException $exception) {

        // The file storage tries to remove directories of collections. But this
        // fails if the directory doesn't exist. So everything is actually fine.
      }
    }
    return $delete;
  }

  /**
   * {@inheritdoc}
   */
  public function filterCreateCollection($collection) {
    return new static($this->storage
      ->createCollection($collection), $this->name);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TestSplitFilter::$name protected property The name prefix to split.
TestSplitFilter::$storage protected property The File storage to read the migrations from.
TestSplitFilter::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
TestSplitFilter::filterCreateCollection public function Overrides TransparentStorageFilterTrait::filterCreateCollection
TestSplitFilter::filterDelete public function Overrides TransparentStorageFilterTrait::filterDelete
TestSplitFilter::filterDeleteAll public function Overrides TransparentStorageFilterTrait::filterDeleteAll
TestSplitFilter::filterExists public function Overrides TransparentStorageFilterTrait::filterExists
TestSplitFilter::filterListAll public function Overrides TransparentStorageFilterTrait::filterListAll
TestSplitFilter::filterRead public function Overrides TransparentStorageFilterTrait::filterRead
TestSplitFilter::filterReadMultiple public function Overrides TransparentStorageFilterTrait::filterReadMultiple
TestSplitFilter::filterWrite public function Overrides TransparentStorageFilterTrait::filterWrite
TestSplitFilter::filterWriteEmptyIsDelete public function Overrides TransparentStorageFilterTrait::filterWriteEmptyIsDelete
TestSplitFilter::isSplitConfig protected function Decide to split the config off or not.
TestSplitFilter::__construct public function Constructs a new TestSplitFilter. Overrides PluginBase::__construct
TransparentStorageFilterTrait::$filtered protected property The wrapped storage which calls the filter.
TransparentStorageFilterTrait::$source protected property The read-only source storage on which the filter operations are performed.
TransparentStorageFilterTrait::filterGetAllCollectionNames public function
TransparentStorageFilterTrait::filterGetCollectionName public function
TransparentStorageFilterTrait::filterRename public function
TransparentStorageFilterTrait::getFilteredStorage protected function Get the decorator storage which applies the filters.
TransparentStorageFilterTrait::getSourceStorage protected function Get the read-only source Storage.
TransparentStorageFilterTrait::setFilteredStorage public function
TransparentStorageFilterTrait::setSourceStorage public function