class CleanFilesCommand in Tome 8
Contains the tome:clean-files command.
@internal
Hierarchy
- class \Drupal\tome_base\CommandBase extends \Symfony\Component\Console\Command\Command uses ExecutableFinderTrait, ProcessTrait
- class \Drupal\tome_sync\Commands\CleanFilesCommand uses PathTrait, ContentIndexerTrait
Expanded class hierarchy of CleanFilesCommand
1 string reference to 'CleanFilesCommand'
- tome_sync.services.yml in modules/
tome_sync/ tome_sync.services.yml - modules/tome_sync/tome_sync.services.yml
1 service uses CleanFilesCommand
File
- modules/
tome_sync/ src/ Commands/ CleanFilesCommand.php, line 20
Namespace
Drupal\tome_sync\CommandsView source
class CleanFilesCommand extends CommandBase {
use PathTrait;
use ContentIndexerTrait;
/**
* The target content storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $contentStorage;
/**
* The config storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $configStorage;
/**
* The file system.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The file sync service.
*
* @var \Drupal\tome_sync\FileSyncInterface
*/
protected $fileSync;
/**
* Creates a CleanFilesCommand object.
*
* @param \Drupal\Core\Config\StorageInterface $content_storage
* The target content storage.
* @param \Drupal\Core\Config\StorageInterface $config_storage
* The target config storage.
* @param \Drupal\tome_sync\FileSyncInterface $file_sync
* The file sync service.
*/
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;
}
/**
* {@inheritdoc}
*/
protected function configure() {
$this
->setName('tome:clean-files')
->setDescription('Deletes unused files.');
}
/**
* {@inheritdoc}
*/
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.');
}
/**
* Assembles a list of files that should be unused.
*
* @return array
* An associative array mapping file UUIDs to their URIs.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CleanFilesCommand:: |
protected | property | The config storage. | |
CleanFilesCommand:: |
protected | property | The target content storage. | |
CleanFilesCommand:: |
protected | property | The file sync service. | |
CleanFilesCommand:: |
protected | property | The file system. | |
CleanFilesCommand:: |
protected | function | ||
CleanFilesCommand:: |
protected | function | ||
CleanFilesCommand:: |
protected | function | Assembles a list of files that should be unused. | |
CleanFilesCommand:: |
public | function | Creates a CleanFilesCommand object. | |
CommandBase:: |
protected | property | The current executable path. | |
CommandBase:: |
protected | property | The IO decorator. | |
CommandBase:: |
protected | function | ||
CommandBase:: |
protected | function |
Returns the IO decorator, for reporting errors. Overrides ProcessTrait:: |
|
ContentIndexerTrait:: |
protected | function | Acquires a lock for writing to the index. | |
ContentIndexerTrait:: |
protected | function | Deletes the index file. | |
ContentIndexerTrait:: |
protected | function | Gets the contents of the index. | |
ContentIndexerTrait:: |
protected | function | Gets the index file path. | |
ContentIndexerTrait:: |
protected | function | Writes content to the index. | |
ContentIndexerTrait:: |
protected | function | Removes content from the index. | |
ContentIndexerTrait:: |
protected | function | Removes content from the index. | |
ExecutableFinderTrait:: |
protected | function | Finds an executable string for the current process. | |
PathTrait:: |
protected | function | Joins multiple paths. | |
ProcessTrait:: |
protected | function | Displays errors using the IO component. | |
ProcessTrait:: |
protected | function | Runs a single command and outputs errors if encountered. | |
ProcessTrait:: |
protected | function | Runs commands with concurrency. |