You are here

class ConfigExporter in Update helper 2.x

Same name and namespace in other branches
  1. 8 src/ConfigExporter.php \Drupal\update_helper\ConfigExporter

Configuration exporter service.

TODO:

  • Add support for create new.

@package Drupal\update_helper

Hierarchy

Expanded class hierarchy of ConfigExporter

1 string reference to 'ConfigExporter'
update_helper.services.yml in ./update_helper.services.yml
update_helper.services.yml
1 service uses ConfigExporter
update_helper.config_exporter in ./update_helper.services.yml
Drupal\update_helper\ConfigExporter

File

src/ConfigExporter.php, line 16

Namespace

Drupal\update_helper
View source
class ConfigExporter {

  /**
   * The extension config storage for config/install config items.
   *
   * @var \Drupal\Core\Config\FileStorage
   */
  protected $extensionConfigStorage;

  /**
   * The extension config storage for config/optional config items.
   *
   * @var \Drupal\Core\Config\FileStorage
   */
  protected $extensionOptionalConfigStorage;

  /**
   * Yaml serializer.
   *
   * @var \Drupal\Component\Serialization\SerializationInterface
   */
  protected $serializer;

  /**
   * ConfigExporter constructor.
   *
   * @param \Drupal\Core\Config\StorageInterface $extension_config_storage
   *   The extension config storage.
   * @param \Drupal\Core\Config\StorageInterface $extension_optional_config_storage
   *   The extension config storage for optional config items.
   * @param \Drupal\Component\Serialization\SerializationInterface $serializer
   *   Array serializer service.
   */
  public function __construct(StorageInterface $extension_config_storage, StorageInterface $extension_optional_config_storage, SerializationInterface $serializer) {
    $this->extensionConfigStorage = $extension_config_storage;
    $this->extensionOptionalConfigStorage = $extension_optional_config_storage;
    $this->serializer = $serializer;
  }

  /**
   * Export configuration.
   *
   * TODO: It would be good to log that file has been changed!!!
   *
   * @param \Drupal\update_helper\ConfigName $config_name
   *   Config name.
   * @param array $data
   *   Configuration array.
   *
   * @return bool
   *   Returns if configuration is stored.
   */
  public function exportConfiguration(ConfigName $config_name, array $data) {
    $config_full_name = $config_name
      ->getFullName();

    // Check if file with provided config name exists in install or optional
    // config and update it accordingly.
    foreach ([
      $this->extensionConfigStorage,
      $this->extensionOptionalConfigStorage,
    ] as $config_storage) {
      $file_data = $config_storage
        ->read($config_full_name);
      if ($file_data) {
        $full_file_path = $config_storage
          ->getFilePath($config_full_name);
        return file_put_contents($full_file_path, $this->serializer::encode($data)) !== FALSE;
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigExporter::$extensionConfigStorage protected property The extension config storage for config/install config items.
ConfigExporter::$extensionOptionalConfigStorage protected property The extension config storage for config/optional config items.
ConfigExporter::$serializer protected property Yaml serializer.
ConfigExporter::exportConfiguration public function Export configuration.
ConfigExporter::__construct public function ConfigExporter constructor.