ConfigSnapshotSubscriber.php 2.12 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\EventSubscriber;

5
use Drupal\Core\Config\ConfigEvents;
6
use Drupal\Core\Config\ConfigManagerInterface;
7 8 9 10 11 12 13 14 15
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\ConfigImporterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Create a snapshot when config is imported.
 */
class ConfigSnapshotSubscriber implements EventSubscriberInterface {

16 17 18 19 20 21 22
  /**
   * The configuration manager.
   *
   * @var \Drupal\Core\Config\ConfigManagerInterface
   */
  protected $configManager;

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  /**
   * The source storage used to discover configuration changes.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $sourceStorage;

  /**
   * The snapshot storage used to write configuration changes.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $snapshotStorage;

  /**
   * Constructs the ConfigSnapshotSubscriber object.
   *
40 41
   * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
   *   The configuration manager.
42
   * @param \Drupal\Core\Config\StorageInterface $source_storage
43
   *   The source storage used to discover configuration changes.
44
   * @param \Drupal\Core\Config\StorageInterface $snapshot_storage
45 46
   *   The snapshot storage used to write configuration changes.
   */
47 48
  public function __construct(ConfigManagerInterface $config_manager, StorageInterface $source_storage, StorageInterface $snapshot_storage) {
    $this->configManager = $config_manager;
49 50 51 52 53 54 55 56 57 58 59
    $this->sourceStorage = $source_storage;
    $this->snapshotStorage = $snapshot_storage;
  }

  /**
   * Creates a config snapshot.
   *
   * @param \Drupal\Core\Config\ConfigImporterEvent $event
   *   The Event to process.
   */
  public function onConfigImporterImport(ConfigImporterEvent $event) {
60
    $this->configManager->createSnapshot($this->sourceStorage, $this->snapshotStorage);
61 62 63 64 65 66 67 68
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
69
  public static function getSubscribedEvents() {
70
    $events[ConfigEvents::IMPORT][] = ['onConfigImporterImport', 40];
71 72 73 74
    return $events;
  }

}