2020-05-20 01:20:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-24 09:26:44 +01:00
|
|
|
namespace Drupal\opdavies_blog\Command;
|
2020-05-20 01:20:22 +01:00
|
|
|
|
|
|
|
use Drupal\Core\Database\Connection;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
final class ExportBodyValuesForThemePurgingCommand {
|
|
|
|
|
|
|
|
private static array $tableNames = [
|
|
|
|
'block_content__body',
|
|
|
|
'node__body',
|
|
|
|
];
|
|
|
|
|
|
|
|
private string $filename = 'body-field-values.txt';
|
|
|
|
|
|
|
|
private Connection $database;
|
|
|
|
|
|
|
|
public function __construct(Connection $database) {
|
|
|
|
$this->database = $database;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drush command to export body field values into a file.
|
|
|
|
*
|
|
|
|
* @command opdavies:export-body-values-for-theme-purging
|
|
|
|
*/
|
2020-05-26 12:01:03 +01:00
|
|
|
public function handle(): void {
|
2020-05-20 01:20:22 +01:00
|
|
|
$values = Collection::make(self::$tableNames)
|
|
|
|
->flatMap(fn(string $tableName) => $this->getValuesFromTable($tableName))
|
|
|
|
->implode(PHP_EOL);
|
|
|
|
|
|
|
|
file_put_contents($this->getFilePath(), $values);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getFilePath(): string {
|
|
|
|
return drupal_get_path('theme', 'opdavies') . DIRECTORY_SEPARATOR . $this->filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getValuesFromTable(string $tableName): array {
|
|
|
|
return $this->database->select($tableName)
|
|
|
|
->fields($tableName, ['body_value'])
|
|
|
|
->execute()
|
|
|
|
->fetchCol();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|