CleanFilesCommand.php in Tome 8
File
modules/tome_sync/src/Commands/CleanFilesCommand.php
View source
<?php
namespace Drupal\tome_sync\Commands;
use Drupal\tome_base\PathTrait;
use Drupal\tome_base\CommandBase;
use Drupal\tome_sync\TomeSyncHelper;
use Drupal\tome_sync\FileSyncInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\tome_sync\ContentIndexerTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
class CleanFilesCommand extends CommandBase {
use PathTrait;
use ContentIndexerTrait;
protected $contentStorage;
protected $configStorage;
protected $fileSystem;
protected $fileSync;
public function __construct(StorageInterface $content_storage, StorageInterface $config_storage, FileSyncInterface $file_sync) {
parent::__construct();
$this->contentStorage = $content_storage;
$this->configStorage = $config_storage;
$this->fileSync = $file_sync;
}
protected function configure() {
$this
->setName('tome:clean-files')
->setDescription('Deletes unused files.');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$this
->io()
->writeLn('Searching for unused files...');
$files = $this
->getUnusedFiles();
if (empty($files)) {
$this
->io()
->success('No unused files found.');
return 0;
}
$this
->io()
->listing($files);
if (!$this
->io()
->confirm('The files listed above will be deleted.', FALSE)) {
return 0;
}
foreach ($files as $uuid => $filename) {
$this->contentStorage
->delete("file.{$uuid}");
$this
->unIndexContentByName("file.{$uuid}");
$this->fileSync
->deleteFile($filename);
}
$this
->io()
->success('Deleted all unused files.');
}
protected function getUnusedFiles() {
$files = [];
$names = $this->contentStorage
->listAll('file.');
foreach ($names as $name) {
$data = $this->contentStorage
->read($name);
list(, $uuid) = TomeSyncHelper::getPartsFromContentName($name);
$files[$uuid] = StreamWrapperManager::getTarget($data['uri'][0]['value']);
}
$callback = function ($value) use (&$files) {
if (is_string($value)) {
foreach ($files as $uuid => $filename) {
if (strpos($value, $uuid) !== FALSE || strpos($value, $filename) !== FALSE) {
unset($files[$uuid]);
}
}
}
};
$names = array_diff($this->contentStorage
->listAll(), $names);
foreach ($names as $name) {
if (!$files) {
break;
}
$data = $this->contentStorage
->read($name);
array_walk_recursive($data, $callback);
}
$names = $this->configStorage
->listAll();
foreach ($names as $name) {
if (!$files) {
break;
}
$data = $this->configStorage
->read($name);
array_walk_recursive($data, $callback);
}
return $files;
}
}