Skip to content
Snippets Groups Projects
Commit c9544796 authored by Claudiu Cristea's avatar Claudiu Cristea Committed by Rick Hawkins
Browse files

Issue #3364699 by claudiu.cristea: Replace 'storage_method' with 'tags'

parent 2c580e61
No related branches found
No related tags found
1 merge request!14Deprecate storage_method
Showing
with 176 additions and 20 deletions
......@@ -122,8 +122,8 @@ key type group, or storage method. Examples:
This would only display MailChimp keys that use the File key provider.
* `#key_filters = ['type_group' => 'encryption']` This would only display
keys that are of a key type that belongs to the 'encryption' group.
* `#key_filters = ['storage_method' => 'file']` This would only display keys
that are defined to use file as the storage_method.
* `#key_filters = ['tags' => ['file', 'private']]` This would only display
keys whose definitions are tagged with `file` and `private` tags.
* `#key_description` This is a boolean value that determines if information
about keys is added to the element's description. It is TRUE by default
and it prepends the description with the following text (with a link to
......
......@@ -95,8 +95,12 @@ function key_drush_command() {
$items['key-provider-list'] = [
'description' => dt('Display a list of available key providers.'),
'options' => [
'tags' => [
'description' => dt('Optional key provider tags, separated by comma, on which to filter.'),
'example-value' => 'config,file',
],
'storage-method' => [
'description' => dt('An optional key provider storage method on which to filter.'),
'description' => dt('[DEPRECATED] An optional key provider storage method on which to filter.'),
'example-value' => 'config,file',
],
],
......
......@@ -11,11 +11,17 @@
function drush_key_provider_list() {
$result = [];
$storage_method = drush_get_option('storage-method');
$tags = explode(',', (string) drush_get_option('tags'));
if ($storage_method = drush_get_option('storage-method')) {
@trigger_error("The Drush --storage-method option is deprecated in key:1.18.0 and is removed from key:2.0.0. Use the --tags option instead. See https://www.drupal.org/node/3364701", E_USER_DEPRECATED);
\Drupal::logger('key')->log('warning', (dt("The Drush --storage-method option is deprecated in key:1.18.0 and is removed from key:2.0.0. Use the --tags option instead. See https://www.drupal.org/node/3364701", E_USER_DEPRECATED)));
$tags[] = $storage_method;
$tags = array_unique($tags);
}
$plugins = \Drupal::service('plugin.manager.key.key_provider')->getDefinitions();
foreach ($plugins as $id => $plugin) {
if (!isset($storage_method) || $plugin['storage_method'] == $storage_method) {
if (!$tags || array_intersect($plugin['tags'], $tags)) {
$row = [];
$row['id'] = $id;
$row['description'] = $plugin['description'];
......
......@@ -42,9 +42,23 @@ class KeyProvider extends Plugin {
* This is an enumeration of {file, config, database, remote}.
*
* @var string
*
* @deprecated in key:1.18.0 and is removed from key:2.0.0. Use the 'tags'
* definition entry instead.
*
* @see https://www.drupal.org/node/3364701
*/
public $storage_method;
/**
* The key provider tags, used for classification and filtering.
*
* It should be a list of tags as strings.
*
* @var array
*/
public $tags = [];
/**
* The settings for inputting a key value.
*
......
......@@ -300,18 +300,28 @@ class KeyCommands extends DrushCommands {
* Display a list of available key providers.
*
* @command key:provider-list
* @option storage-method An optional key provider storage method on which to filter.
* @option tags Optional key provider tags, separated by comma, on which to filter.
* @option storage-method [DEPRECATED] An optional key provider storage method on which to filter.
* @aliases key-provider-list
* @format table
*/
public function providerList($options = ['storage-method' => NULL]) {
public function providerList($options = [
'tags' => NULL,
'storage-method' => NULL,
]) {
$result = [];
$storage_method = $options['storage-method'];
$tags = StringUtils::csvToArray($options['tags']);
if ($options['storage-method']) {
@trigger_error("The Drush --storage-method option is deprecated in key:1.18.0 and is removed from key:2.0.0. Use the --tags option instead. See https://www.drupal.org/node/3364701", E_USER_DEPRECATED);
\Drupal::logger('key')->log('warning', (dt("The Drush --storage-method option is deprecated in key:1.18.0 and is removed from key:2.0.0. Use the --tags option instead. See https://www.drupal.org/node/3364701", E_USER_DEPRECATED)));
$tags[] = $options['storage-method'];
$tags = array_unique($tags);
}
$plugins = $this->keyProviderPluginManager->getDefinitions();
foreach ($plugins as $id => $plugin) {
if (!isset($storage_method) || $plugin['storage_method'] == $storage_method) {
if (!$tags || array_intersect($plugin['tags'], $tags)) {
$row = [];
$row['id'] = $id;
$row['description'] = $plugin['description'];
......
......@@ -81,9 +81,9 @@ class KeyRepository implements KeyRepositoryInterface {
/**
* {@inheritdoc}
*/
public function getKeysByStorageMethod($storage_method) {
$key_providers = array_filter($this->keyProviderManager->getDefinitions(), function ($definition) use ($storage_method) {
return $definition['storage_method'] == $storage_method;
public function getKeysByTags(array $tags): array {
$key_providers = array_filter($this->keyProviderManager->getDefinitions(), function (array $definition) use ($tags): bool {
return (bool) array_intersect($definition['tags'], $tags);
});
$keys = [];
......@@ -93,6 +93,14 @@ class KeyRepository implements KeyRepositoryInterface {
return $keys;
}
/**
* {@inheritdoc}
*/
public function getKeysByStorageMethod($storage_method) {
@trigger_error(__METHOD__ . '() is deprecated in key:1.18.0 and is removed from key:2.0.0. Use self::getKeysByTags() instead. See https://www.drupal.org/node/3364701', E_USER_DEPRECATED);
return $this->getKeysByTags([$storage_method]);
}
/**
* {@inheritdoc}
*/
......@@ -136,8 +144,13 @@ class KeyRepository implements KeyRepositoryInterface {
$keys = array_intersect_key($this->getKeysByTypeGroup($filter), $keys);
break;
case 'tags':
array_intersect_key($this->getKeysByTags($filter), $keys);
break;
case 'storage_method':
$keys = array_intersect_key($this->getKeysByStorageMethod($filter), $keys);
@trigger_error("Passing 'storage_method' as filter to Drupal\key\KeyRepository::getKeyNamesAsOptions() is deprecated in key:1.18.0 and is removed from key:2.0.0. Use the 'tags' filter instead. See https://www.drupal.org/node/3364701", E_USER_DEPRECATED);
$keys = array_intersect_key($this->getKeysByTags([$filter]), $keys);
break;
}
}
......
......@@ -41,6 +41,19 @@ interface KeyRepositoryInterface {
*/
public function getKeysByType($key_type_id);
/**
* Get keys that tagged with specified tags.
*
* Tags are annotation of a key's key provider plugin.
*
* @param array $tags
* A list of tags of the key provider.
*
* @return \Drupal\key\Entity\Key[]
* An array of key objects indexed by their IDs.
*/
public function getKeysByTags(array $tags): array;
/**
* Get keys that use the specified storage method.
*
......@@ -51,6 +64,11 @@ interface KeyRepositoryInterface {
*
* @return \Drupal\key\Entity\Key[]
* An array of key objects indexed by their ids.
*
* @deprecated in key:1.18.0 and is removed from key:2.0.0. Use
* self::getKeysByTags() instead.
*
* @see https://www.drupal.org/node/3364701
*/
public function getKeysByStorageMethod($storage_method);
......
......@@ -58,6 +58,14 @@ class KeyPluginManager extends DefaultPluginManager {
parent::processDefinition($definition, $plugin_id);
// Add plugin_type to the definition.
$definition['plugin_type'] = $this->pluginType;
if ($this->pluginType === 'key_provider' && !empty($definition['storage_method'])) {
@trigger_error("The key provider 'storage_method' definition entry is deprecated in key:1.18.0 and is removed from key:2.0.0. Use the 'tags' definition entry instead. See https://www.drupal.org/node/3364701", E_USER_DEPRECATED);
$definition['tags'] = $definition['tags'] ?? [];
if (!in_array($definition['storage_method'], $definition['tags'])) {
$definition['tags'][] = $definition['storage_method'];
}
}
}
}
......@@ -16,7 +16,9 @@ use Drupal\key\KeyInterface;
* id = "config",
* label = @Translation("Configuration"),
* description = @Translation("The Configuration key provider stores the key in Drupal's configuration system."),
* storage_method = "config",
* tags = {
* "config",
* },
* key_value = {
* "accepted" = TRUE,
* "required" = FALSE
......
......@@ -14,7 +14,9 @@ use Drupal\key\KeyInterface;
* id = "env",
* label = @Translation("Environment"),
* description = @Translation("The Environment key provider allows a key to be retrieved from an environment variable."),
* storage_method = "env",
* tags = {
* "env",
* },
* key_value = {
* "accepted" = FALSE,
* "required" = FALSE
......
......@@ -14,7 +14,9 @@ use Drupal\key\KeyInterface;
* id = "file",
* label = @Translation("File"),
* description = @Translation("The File key provider allows a key to be stored in a file, preferably outside of the web root."),
* storage_method = "file",
* tags = {
* "file",
* },
* key_value = {
* "accepted" = FALSE,
* "required" = FALSE
......
<?php
namespace Drupal\key_test\Plugin\KeyProvider;
use Drupal\key\KeyInterface;
/**
* Plugin with deprecated definition entries.
*
* @KeyProvider(
* id = "deprecated_defintion_entries",
* label = @Translation("Deprecated"),
* storage_method = "whatever",
* key_value = {
* "accepted" = FALSE,
* "required" = FALSE
* }
* )
*/
class DeprecatedDefinition extends \Drupal\key\Plugin\KeyProviderBase {
/**
* {@inheritdoc}
*/
public function getKeyValue(KeyInterface $key) {
}
}
......@@ -14,7 +14,9 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* id = "key_test_state",
* label = @Translation("State ☠️"),
* description = @Translation("Stores keys in state."),
* storage_method = "state",
* tags = {
* "state",
* },
* key_value = {
* "accepted" = TRUE,
* "required" = FALSE
......
<?php
namespace Drupal\Tests\key\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\key\Entity\Key;
/**
* Tests deprecations for Key module.
*
* @group key
* @group legacy
*/
class KeyDeprecationTest extends KernelTestBase {
/**
* @inheritdoc
*/
protected static $modules = ['key', 'key_test'];
/**
* @covers \Drupal\key\Annotation\KeyProvider
* @covers \Drupal\key\Plugin\KeyPluginManager::processDefinition
* @covers \Drupal\key\KeyRepository::getKeysByStorageMethod
* @covers \Drupal\key\KeyRepository::getKeyNamesAsOptions
*/
public function testStorageMethodDeprecation(): void {
$this->expectDeprecation("The key provider 'storage_method' definition entry is deprecated in key:1.18.0 and is removed from key:2.0.0. Use the 'tags' definition entry instead. See https://www.drupal.org/node/3364701");
$plugin = $this->container->get('plugin.manager.key.key_provider')->createInstance('deprecated_defintion_entries');
$this->assertSame(['whatever'], $plugin->getPluginDefinition()['tags']);
$key_repository = $this->container->get('key.repository');
Key::create(['id' => 'test', 'key_provider' => 'file'])->save();
$this->expectDeprecation('Drupal\key\KeyRepository::getKeysByStorageMethod() is deprecated in key:1.18.0 and is removed from key:2.0.0. Use self::getKeysByTags() instead. See https://www.drupal.org/node/3364701');
$keys = $key_repository->getKeysByStorageMethod('file');
$this->assertCount(1, $keys);
$this->assertSame('file', reset($keys)->getKeyProvider()->getPluginId());
$this->expectDeprecation("Passing 'storage_method' as filter to Drupal\key\KeyRepository::getKeyNamesAsOptions() is deprecated in key:1.18.0 and is removed from key:2.0.0. Use the 'tags' filter instead. See https://www.drupal.org/node/3364701");
$names = $key_repository->getKeyNamesAsOptions(['storage_method' => 'file']);
$this->assertCount(1, $names);
$this->assertArrayHasKey('test', $names);
}
}
......@@ -106,7 +106,7 @@ class KeyEntityTest extends KeyTestBase {
$definition = [
'id' => 'config',
'label' => 'Configuration',
'storage_method' => 'config',
'tags' => ['config'],
];
$this->key_provider_settings = ['key_value' => $this->createToken(), 'base64_encoded' => FALSE];
$plugin = new ConfigKeyProvider($this->key_provider_settings, 'config', $definition);
......@@ -117,11 +117,11 @@ class KeyEntityTest extends KeyTestBase {
$this->keyProviderManager->expects($this->any())
->method('getDefinitions')
->willReturn([
['id' => 'file', 'label' => 'File', 'storage_method' => 'file'],
['id' => 'file', 'label' => 'File', 'tags' => ['file']],
[
'id' => 'config',
'label' => 'Configuration',
'storage_method' => 'config',
'tags' => ['config'],
],
]);
$this->keyProviderManager->expects($this->any())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment