TypesCommand.php in Purge 8.3
File
modules/purge_drush/src/Commands/TypesCommand.php
View source
<?php
namespace Drupal\purge_drush\Commands;
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationsServiceInterface;
use Drupal\purge\Plugin\Purge\Purger\PurgersServiceInterface;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Input\InputInterface;
class TypesCommand extends DrushCommands {
protected $purgePurgers;
protected $purgeInvalidationFactory;
public function __construct(InvalidationsServiceInterface $purge_invalidation_factory, PurgersServiceInterface $purge_purgers) {
parent::__construct();
$this->purgeInvalidationFactory = $purge_invalidation_factory;
$this->purgePurgers = $purge_purgers;
}
public function addFieldLabels(InputInterface $input, AnnotationData $annotationData) {
if (isset($annotationData['field-labels'])) {
foreach ($this->purgePurgers
->getLabels() as $id => $label) {
$annotationData['field-labels'] .= sprintf("\n%s: %s", $id, $label);
}
}
}
public function types(array $options = [
'format' => 'table',
]) {
$types_by_purger = $this->purgePurgers
->getTypesByPurger();
$types = $this->purgePurgers
->getTypes();
$rows = [];
if (in_array($options['format'], [
'list',
'string',
])) {
foreach ($types as $type) {
$rows[] = $type;
}
return $options['format'] == 'string' ? implode(',', $rows) : $rows;
}
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]);
}
}
$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;
}
}