ConfigSnapshotSubscriber.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Drupal\Core\Config\ConfigEvents;
  4. use Drupal\Core\Config\ConfigManagerInterface;
  5. use Drupal\Core\Config\StorageInterface;
  6. use Drupal\Core\Config\ConfigImporterEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9. * Create a snapshot when config is imported.
  10. */
  11. class ConfigSnapshotSubscriber implements EventSubscriberInterface {
  12. /**
  13. * The configuration manager.
  14. *
  15. * @var \Drupal\Core\Config\ConfigManagerInterface
  16. */
  17. protected $configManager;
  18. /**
  19. * The source storage used to discover configuration changes.
  20. *
  21. * @var \Drupal\Core\Config\StorageInterface
  22. */
  23. protected $sourceStorage;
  24. /**
  25. * The snapshot storage used to write configuration changes.
  26. *
  27. * @var \Drupal\Core\Config\StorageInterface
  28. */
  29. protected $snapshotStorage;
  30. /**
  31. * Constructs the ConfigSnapshotSubscriber object.
  32. *
  33. * @param \Drupal\Core\Config\StorageInterface $source_storage
  34. * The source storage used to discover configuration changes.
  35. * @param \Drupal\Core\Config\StorageInterface $snapshot_storage
  36. * The snapshot storage used to write configuration changes.
  37. */
  38. public function __construct(ConfigManagerInterface $config_manager, StorageInterface $source_storage, StorageInterface $snapshot_storage) {
  39. $this->configManager = $config_manager;
  40. $this->sourceStorage = $source_storage;
  41. $this->snapshotStorage = $snapshot_storage;
  42. }
  43. /**
  44. * Creates a config snapshot.
  45. *
  46. * @param \Drupal\Core\Config\ConfigImporterEvent $event
  47. * The Event to process.
  48. */
  49. public function onConfigImporterImport(ConfigImporterEvent $event) {
  50. $this->configManager->createSnapshot($this->sourceStorage, $this->snapshotStorage);
  51. }
  52. /**
  53. * Registers the methods in this class that should be listeners.
  54. *
  55. * @return array
  56. * An array of event listener definitions.
  57. */
  58. public static function getSubscribedEvents() {
  59. $events[ConfigEvents::IMPORT][] = ['onConfigImporterImport', 40];
  60. return $events;
  61. }
  62. }