You are here

class CleanFilesCommand in Tome 8

Contains the tome:clean-files command.

@internal

Hierarchy

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
tome_sync.clean_files_command in modules/tome_sync/tome_sync.services.yml
Drupal\tome_sync\Commands\CleanFilesCommand

File

modules/tome_sync/src/Commands/CleanFilesCommand.php, line 20

Namespace

Drupal\tome_sync\Commands
View 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

Namesort descending Modifiers Type Description Overrides
CleanFilesCommand::$configStorage protected property The config storage.
CleanFilesCommand::$contentStorage protected property The target content storage.
CleanFilesCommand::$fileSync protected property The file sync service.
CleanFilesCommand::$fileSystem protected property The file system.
CleanFilesCommand::configure protected function
CleanFilesCommand::execute protected function
CleanFilesCommand::getUnusedFiles protected function Assembles a list of files that should be unused.
CleanFilesCommand::__construct public function Creates a CleanFilesCommand object.
CommandBase::$executable protected property The current executable path.
CommandBase::$io protected property The IO decorator.
CommandBase::initialize protected function
CommandBase::io protected function Returns the IO decorator, for reporting errors. Overrides ProcessTrait::io
ContentIndexerTrait::acquireContentIndexLock protected function Acquires a lock for writing to the index.
ContentIndexerTrait::deleteContentIndex protected function Deletes the index file.
ContentIndexerTrait::getContentIndex protected function Gets the contents of the index.
ContentIndexerTrait::getContentIndexFilePath protected function Gets the index file path.
ContentIndexerTrait::indexContent protected function Writes content to the index.
ContentIndexerTrait::unIndexContent protected function Removes content from the index.
ContentIndexerTrait::unIndexContentByName protected function Removes content from the index.
ExecutableFinderTrait::findExecutable protected function Finds an executable string for the current process.
PathTrait::joinPaths protected function Joins multiple paths.
ProcessTrait::displayErrors protected function Displays errors using the IO component.
ProcessTrait::runCommand protected function Runs a single command and outputs errors if encountered.
ProcessTrait::runCommands protected function Runs commands with concurrency.