summaryrefslogtreecommitdiffstats
path: root/src/ConfigSnapshotStorageTrait.php
blob: 0267059339217de4d6e8a828433c979ed272d952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

namespace Drupal\config_snapshot;

use Drupal\config_snapshot\Entity\ConfigSnapshot;
use Drupal\Core\Config\StorageInterface;

/**
 * Provides a utility method for working with configuration snapshot services.
 */
trait ConfigSnapshotStorageTrait {

  /**
   * Returns a configuration snapshot storage service.
   *
   * Uses a corresponding service if available while falling back to a new
   * storage object. The fallback ensures a storage is provided for a newly-
   * installed extension before the service container is rebuilt.
   *
   * @param string $snapshot_set
   *   The snapshot set.
   * @param string $extension_type
   *   The extension type.
   * @param string $extension_name
   *   The extension name.
   * @param string $collection
   *   (optional) The collection to store configuration in. Defaults to the
   *   default collection.
   * @param \Drupal\config_snapshot\Entity\ConfigSnapshot $config_snapshot
   *   (optional) The configuration snapshot.
   *
   * @return \Drupal\config_snapshot\ConfigSnapshotStorage
   *   A configuration snapshot storage.
   */
  protected function getConfigSnapshotStorage($snapshot_set, $extension_type, $extension_name, $collection = StorageInterface::DEFAULT_COLLECTION, ConfigSnapshot $config_snapshot = NULL) {
    // The service ID is for the default collection.
    $service_id = "config_snapshot.{$snapshot_set}.{$extension_type}.{$extension_name}";

    if ($collection === StorageInterface::DEFAULT_COLLECTION && \Drupal::getContainer() && \Drupal::hasService($service_id)) {
      $storage = \Drupal::service($service_id);
    }
    else {
      $storage = new ConfigSnapshotStorage($snapshot_set, $extension_type, $extension_name, $collection, $config_snapshot);
    }

    return $storage;
  }

}