You are here

class FileSync in Tome 8

Handles file import and exports by keeping a file export directory in sync.

@internal

Hierarchy

Expanded class hierarchy of FileSync

1 string reference to 'FileSync'
tome_sync.services.yml in modules/tome_sync/tome_sync.services.yml
modules/tome_sync/tome_sync.services.yml
1 service uses FileSync
tome_sync.file_sync in modules/tome_sync/tome_sync.services.yml
Drupal\tome_sync\FileSync

File

modules/tome_sync/src/FileSync.php, line 18

Namespace

Drupal\tome_sync
View source
class FileSync implements FileSyncInterface {
  use PathTrait;

  /**
   * The file system.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Creates an FileSync object.
   *
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system.
   */
  public function __construct(FileSystemInterface $file_system) {
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public function importFiles() {
    $file_directory = $this
      ->getFileDirectory();
    if (!is_dir($file_directory)) {
      return;
    }

    /** @var \Drupal\file\FileInterface $file */
    foreach ($this->fileSystem
      ->scanDirectory($file_directory, '/.*/') as $file) {
      $destination = 'public://' . ltrim(str_replace($file_directory, '', $file->uri), '/');
      $directory = dirname($destination);
      $this->fileSystem
        ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
      $this->fileSystem
        ->copy($file->uri, $destination, FileSystemInterface::EXISTS_REPLACE);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteExportDirectory() {
    $file_directory = $this
      ->getFileDirectory();
    if (file_exists($file_directory)) {
      if (!$this->fileSystem
        ->deleteRecursive($file_directory)) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function exportFile(FileInterface $file) {
    $this
      ->ensureFileDirectory();
    $file_directory = $this
      ->getFileDirectory();
    if (strpos($file
      ->getFileUri(), 'public://') === 0 && file_exists($file
      ->getFileUri())) {
      $destination = $this
        ->joinPaths($file_directory, StreamWrapperManager::getTarget($file
        ->getFileUri()));
      $directory = dirname($destination);
      $this->fileSystem
        ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
      $this->fileSystem
        ->copy($file
        ->getFileUri(), $destination, FileSystemInterface::EXISTS_REPLACE);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteFileExport(FileInterface $file) {
    $file_directory = $this
      ->getFileDirectory();
    if (strpos($file
      ->getFileUri(), 'public://') === 0) {
      $path = $this
        ->joinPaths($file_directory, StreamWrapperManager::getTarget($file
        ->getFileUri()));
      if (file_exists($path)) {
        $this->fileSystem
          ->delete($path);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteFile($filename) {
    $path = $this
      ->joinPaths($this
      ->getFileDirectory(), $filename);
    if (file_exists($path)) {
      $this->fileSystem
        ->delete($path);
    }
  }

  /**
   * Gets the file directory.
   *
   * @return string
   *   The file directory.
   */
  protected function getFileDirectory() {
    return Settings::get('tome_files_directory', '../files') . '/public';
  }

  /**
   * Ensures that the file directory exists.
   */
  protected function ensureFileDirectory() {
    $file_directory = $this
      ->getFileDirectory();
    $this->fileSystem
      ->prepareDirectory($file_directory, FileSystemInterface::CREATE_DIRECTORY);
    FileSecurity::writeHtaccess($file_directory);
    if (!file_exists($file_directory)) {
      throw new StorageException('Failed to create config directory ' . $file_directory);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileSync::$fileSystem protected property The file system.
FileSync::deleteExportDirectory public function Deletes the file export directory. Overrides FileSyncInterface::deleteExportDirectory
FileSync::deleteFile public function Deletes an exported file by name. Overrides FileSyncInterface::deleteFile
FileSync::deleteFileExport public function Deletes an exported file by entity. Overrides FileSyncInterface::deleteFileExport
FileSync::ensureFileDirectory protected function Ensures that the file directory exists.
FileSync::exportFile public function Exports a file to the export directory. Overrides FileSyncInterface::exportFile
FileSync::getFileDirectory protected function Gets the file directory.
FileSync::importFiles public function Imports all files from the file directory. Overrides FileSyncInterface::importFiles
FileSync::__construct public function Creates an FileSync object.
PathTrait::joinPaths protected function Joins multiple paths.