You are here

public function TypesCommand::types in Purge 8.3

List all supported cache invalidation types.

@usage drush p:types List all supported cache invalidation types. @usage drush p:types --format=list Renders a simple list of supported invalidation types. @usage drush p:types --format=json Export as JSON. @usage drush p:types --format=yaml Export as YAML.

@command p:types @aliases ptyp,p-types @field-labels type: Type supported: Supported

Parameters

array $options: Associative array of options whose values come from Drush.

Return value

array|\Consolidation\OutputFormatters\StructuredData\RowsOfFields Row-based structure of data.

File

modules/purge_drush/src/Commands/TypesCommand.php, line 83

Class

TypesCommand
List all supported cache invalidation types.

Namespace

Drupal\purge_drush\Commands

Code

public function types(array $options = [
  'format' => 'table',
]) {
  $types_by_purger = $this->purgePurgers
    ->getTypesByPurger();
  $types = $this->purgePurgers
    ->getTypes();
  $rows = [];

  // Return a simple listing of supported types.
  if (in_array($options['format'], [
    'list',
    'string',
  ])) {
    foreach ($types as $type) {
      $rows[] = $type;
    }
    return $options['format'] == 'string' ? implode(',', $rows) : $rows;
  }

  // Return a complexer data structure that tells which purger supports what.
  foreach ($this->purgeInvalidationFactory
    ->getPlugins() as $type => $typedef) {
    $rows[$type] = [
      'type' => [
        'id' => $typedef['id'],
        'label' => (string) $typedef['label'],
      ],
      'supported' => in_array($type, $types),
    ];
    foreach ($this->purgePurgers
      ->getPluginsEnabled() as $id => $plugin_id) {
      $rows[$type][$id] = in_array($type, $types_by_purger[$id]);
    }
  }

  // Structurize the data and pass a render function for pretty table output.
  $rows = new RowsOfFields($rows);
  $rows
    ->addRendererFunction(function ($key, $cellData, FormatterOptions $options, $rowData) {
    if ($key == 'type') {
      return $cellData['label'];
    }
    elseif (is_bool($cellData)) {
      return $cellData ? '[X]' : '';
    }
    return $cellData;
  });
  return $rows;
}