You are here

class ConfigFilterEventSubscriber in Config Filter 8.2

Class ConfigFilterEventSubscriber.

Hierarchy

Expanded class hierarchy of ConfigFilterEventSubscriber

1 string reference to 'ConfigFilterEventSubscriber'
config_filter.services.yml in ./config_filter.services.yml
config_filter.services.yml
1 service uses ConfigFilterEventSubscriber
config_filter.event_subscriber in ./config_filter.services.yml
Drupal\config_filter\ConfigFilterEventSubscriber

File

src/ConfigFilterEventSubscriber.php, line 14

Namespace

Drupal\config_filter
View source
class ConfigFilterEventSubscriber implements EventSubscriberInterface {
  use StorageCopyTrait;

  /**
   * The filter storage factory.
   *
   * @var \Drupal\config_filter\ConfigFilterStorageFactory
   */
  protected $filterStorageFactory;

  /**
   * The sync storage.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $sync;

  /**
   * ConfigFilterEventSubscriber constructor.
   *
   * @param \Drupal\config_filter\ConfigFilterStorageFactory $filterStorageFactory
   *   The filter storage factory.
   * @param \Drupal\Core\Config\StorageInterface $sync
   *   The sync storage.
   */
  public function __construct(ConfigFilterStorageFactory $filterStorageFactory, StorageInterface $sync) {
    $this->filterStorageFactory = $filterStorageFactory;
    $this->sync = $sync;
  }

  /**
   * The storage is transformed for importing.
   *
   * @param \Drupal\Core\Config\StorageTransformEvent $event
   *   The event for altering configuration of the storage.
   */
  public function onImportTransform(StorageTransformEvent $event) {
    $storage = $event
      ->getStorage();

    // The temporary storage representing the active storage.
    $temp = new MemoryStorage();

    // Get the filtered storage based on the event storage.
    $filtered = $this->filterStorageFactory
      ->getFilteredStorage($storage, [
      'config.storage.sync',
    ]);

    // Simulate the importing of configuration.
    self::replaceStorageContents($filtered, $temp);

    // Set the event storage to the one of the simulated import.
    self::replaceStorageContents($temp, $storage);
  }

  /**
   * The storage is transformed for exporting.
   *
   * @param \Drupal\Core\Config\StorageTransformEvent $event
   *   The event for altering configuration of the storage.
   */
  public function onExportTransform(StorageTransformEvent $event) {
    $storage = $event
      ->getStorage();

    // The temporary storage representing the sync storage.
    $temp = new MemoryStorage();

    // Copy the contents of the sync storage to the temporary one.
    self::replaceStorageContents($this->sync, $temp);

    // Get the simulated filtered sync storage.
    $filtered = $this->filterStorageFactory
      ->getFilteredStorage($temp, [
      'config.storage.sync',
    ]);

    // Simulate the exporting of the configuration.
    self::replaceStorageContents($storage, $filtered);

    // Set the event storage to the inner storage of the simulated sync storage.
    self::replaceStorageContents($temp, $storage);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // @todo: use class constants when they get added in #2991683
    $events['config.transform.import'][] = [
      'onImportTransform',
    ];
    $events['config.transform.export'][] = [
      'onExportTransform',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFilterEventSubscriber::$filterStorageFactory protected property The filter storage factory.
ConfigFilterEventSubscriber::$sync protected property The sync storage.
ConfigFilterEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ConfigFilterEventSubscriber::onExportTransform public function The storage is transformed for exporting.
ConfigFilterEventSubscriber::onImportTransform public function The storage is transformed for importing.
ConfigFilterEventSubscriber::__construct public function ConfigFilterEventSubscriber constructor.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.