public function KeyCommands::save in Key 8
Save a key.
@command key:save
@option label The human-readable label of the key. @option description A short description of the key. @option key-type The key type. To see a list of available key types, use `drush key-type-list`. @option key-type-settings Settings specific to the defined key type, in JSON format. @option key-provider The key provider. To see a list of available key providers, use `drush key-provider-list`. @option key-provider-settings Settings specific to the defined key provider, in JSON format. @option key-input The key input method. @option key-input-settings Settings specific to the defined key input, in JSON format. @usage drush key-save secret_password 'pA$$w0rd' --label="Secret password" --key-type=authentication --key-provider=config --key-input=text_field Define a key for a password to use for authentication using the Configuration key provider. @usage drush key-save encryption_key --label="Encryption key" --key-type=encryption --key-type-settings='{"key_size":256}' --key-provider=file --key-provider-settings='{"file_location":"private://keys/encryption.key", "base64_encoded":true}' --key-input=none Define a key to use for encryption using the File key provider. @aliases key-save
Parameters
string $id: The ID (machine name) of the key to save.
string $key_value: A key value to save. May or may not be allowed or required, depending on the key provider.
array $options: Options array.
File
- src/
Commands/ KeyCommands.php, line 101
Class
- KeyCommands
- Class KeyCommands.
Namespace
Drupal\key\CommandsCode
public function save($id, $key_value = NULL, array $options = [
'label' => NULL,
'description' => NULL,
'key-type' => NULL,
'key-type-settings' => NULL,
'key-provider' => NULL,
'key-provider-settings' => NULL,
'key-input' => NULL,
'key-input-settings' => NULL,
]) {
$values = [];
$values['id'] = $id;
// Look for a key with the specified ID.
$existing_key = $this->repository
->getKey($values['id']);
if ($existing_key) {
// Add a warning about overwriting a key.
$this->logger
->warning('Be extremely careful when overwriting a key! It may result in losing access to a service or making encrypted data unreadable.');
// Confirm that the key should be saved.
$this->output
->writeln(dt('The following key will be overwritten: !id', [
'!id' => $values['id'],
]));
if (!$this
->io()
->confirm(dt('Do you want to continue?'))) {
// Removing drush_user_abort(), no current implementation of that.
return;
}
}
// Set any values defined as options.
foreach (array_keys($this
->keyOptions()) as $option) {
$value = $options[$option];
if (isset($value)) {
if (in_array($option, [
'key-type-settings',
'key-provider-settings',
'key-input-settings',
])) {
$values[str_replace('-', '_', $option)] = Json::decode($value);
}
else {
$values[str_replace('-', '_', $option)] = $value;
}
}
}
// If the label was not defined, use the ID.
if (!isset($values['label'])) {
$values['label'] = $values['id'];
}
// If the key already exists, make a clone and update it.
// Otherwise, create a new key entity.
if ($existing_key) {
$key = clone $existing_key;
foreach ($values as $index => $value) {
if ($index != 'id') {
$key
->set($index, $value);
}
}
}
else {
$storage = $this->entityTypeManager
->getStorage('key');
$key = $storage
->create($values);
}
// If a key value was specified, set it.
if (isset($key_value)) {
$key
->setKeyValue($key_value);
}
// Save the key.
$key
->save();
// Load the key to confirm that it was saved.
$key_check = $this->repository
->getKey($values['id']);
if (!$key_check) {
throw new \Exception(dt('Key !id was not saved.', [
'!id' => $values['id'],
]));
}
$this->logger
->info('Key !id was saved successfully.', [
'!id' => $values['id'],
]);
}