You are here

public function KeyCommands::keyList in Key 8

Display a list of available keys.

@command key:list @option key-type An optional, comma-delimited list of key types. To see a list of available key types, use `drush key-type-list`. @option key-provider An optional, comma-delimited list of key providers. To see a list of available key providers, use `drush key-provider-list`. @aliases key-list @format table

Parameters

array $options: Options array.

File

src/Commands/KeyCommands.php, line 244

Class

KeyCommands
Class KeyCommands.

Namespace

Drupal\key\Commands

Code

public function keyList(array $options = [
  'key-type' => NULL,
  'key-provider' => NULL,
]) {
  $result = [];

  /* @var $key \Drupal\key\Entity\Key */
  $keys = $this->repository
    ->getKeys();

  // Filter by key type, if specified.
  if ($options['key-type']) {
    $key_type_filter = StringUtils::csvToArray($options['key-type']);
    foreach ($keys as $id => $key) {
      if (!in_array($key
        ->getKeyType()
        ->getPluginId(), $key_type_filter)) {
        unset($keys[$id]);
      }
    }
  }

  // Filter by key provider, if specified.
  if ($options['key-provider']) {
    $key_provider_filter = StringUtils::csvToArray($options['key-provider']);
    foreach ($keys as $id => $key) {
      if (!in_array($key
        ->getKeyProvider()
        ->getPluginId(), $key_provider_filter)) {
        unset($keys[$id]);
      }
    }
  }
  foreach ($keys as $id => $key) {
    $row = [];
    $row['id'] = $id;
    $row['label'] = $key
      ->label();
    $row['key_type'] = $key
      ->getKeyType()
      ->getPluginDefinition()['label'];
    $row['key_provider'] = $key
      ->getKeyProvider()
      ->getPluginDefinition()['label'];
    $result[$id] = $row;
  }
  return new RowsOfFields($result);
}