You are here

class FileCopy in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/process/FileCopy.php \Drupal\migrate\Plugin\migrate\process\FileCopy

Copies or moves a local file from one place into another.

The file can be moved, reused, or set to be automatically renamed if a duplicate exists.

The source value is an indexed array of two values:

  • The source path or URI, e.g. '/path/to/foo.txt' or 'public://bar.txt'.
  • The destination URI, e.g. 'public://foo.txt'.

Available configuration keys:

  • move: (optional) Boolean, if TRUE, move the file, otherwise copy the file. Defaults to FALSE.
  • file_exists: (optional) Replace behavior when the destination file already exists:

    • 'replace' - (default) Replace the existing file.
    • 'rename' - Append _{incrementing number} until the filename is unique.
    • 'use existing' - Do nothing and return FALSE.

Examples:


process:
  path_to_file:
    plugin: file_copy
    source:
      - /path/to/file.png
      - public://new/path/to/file.png

Plugin annotation


@MigrateProcessPlugin(
  id = "file_copy"
)

Hierarchy

Expanded class hierarchy of FileCopy

See also

\Drupal\migrate\Plugin\MigrateProcessInterface

2 files declare their use of FileCopy
FileCopyTest.php in core/modules/migrate/tests/src/Unit/process/FileCopyTest.php
FileCopyTest.php in core/modules/migrate/tests/src/Kernel/process/FileCopyTest.php

File

core/modules/migrate/src/Plugin/migrate/process/FileCopy.php, line 54

Namespace

Drupal\migrate\Plugin\migrate\process
View source
class FileCopy extends FileProcessBase implements ContainerFactoryPluginInterface {

  /**
   * The stream wrapper manager service.
   *
   * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
   */
  protected $streamWrapperManager;

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

  /**
   * An instance of the download process plugin.
   *
   * @var \Drupal\migrate\Plugin\MigrateProcessInterface
   */
  protected $downloadPlugin;

  /**
   * Constructs a file_copy process plugin.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrappers
   *   The stream wrapper manager service.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
   * @param \Drupal\migrate\Plugin\MigrateProcessInterface $download_plugin
   *   An instance of the download plugin for handling remote URIs.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, StreamWrapperManagerInterface $stream_wrappers, FileSystemInterface $file_system, MigrateProcessInterface $download_plugin) {
    $configuration += [
      'move' => FALSE,
    ];
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->streamWrapperManager = $stream_wrappers;
    $this->fileSystem = $file_system;
    $this->downloadPlugin = $download_plugin;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('stream_wrapper_manager'), $container
      ->get('file_system'), $container
      ->get('plugin.manager.migrate.process')
      ->createInstance('download', $configuration));
  }

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {

    // If we're stubbing a file entity, return a URI of NULL so it will get
    // stubbed by the general process.
    if ($row
      ->isStub()) {
      return NULL;
    }
    list($source, $destination) = $value;

    // If the source path or URI represents a remote resource, delegate to the
    // download plugin.
    if (!$this
      ->isLocalUri($source)) {
      return $this->downloadPlugin
        ->transform($value, $migrate_executable, $row, $destination_property);
    }

    // Ensure the source file exists, if it's a local URI or path.
    if (!file_exists($source)) {
      throw new MigrateException("File '{$source}' does not exist");
    }

    // If the start and end file is exactly the same, there is nothing to do.
    if ($this
      ->isLocationUnchanged($source, $destination)) {
      return $destination;
    }

    // Check if a writable directory exists, and if not try to create it.
    $dir = $this
      ->getDirectory($destination);

    // If the directory exists and is writable, avoid
    // \Drupal\Core\File\FileSystemInterface::prepareDirectory() call and write
    // the file to destination.
    if (!is_dir($dir) || !is_writable($dir)) {
      if (!$this->fileSystem
        ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
        throw new MigrateException("Could not create or write to directory '{$dir}'");
      }
    }
    $final_destination = $this
      ->writeFile($source, $destination, $this->configuration['file_exists']);
    if ($final_destination) {
      return $final_destination;
    }
    throw new MigrateException("File {$source} could not be copied to {$destination}");
  }

  /**
   * Tries to move or copy a file.
   *
   * @param string $source
   *   The source path or URI.
   * @param string $destination
   *   The destination path or URI.
   * @param int $replace
   *   (optional) FileSystemInterface::EXISTS_REPLACE (default) or
   *   FileSystemInterface::EXISTS_RENAME.
   *
   * @return string|bool
   *   File destination on success, FALSE on failure.
   */
  protected function writeFile($source, $destination, $replace = FileSystemInterface::EXISTS_REPLACE) {

    // Check if there is a destination available for copying. If there isn't,
    // it already exists at the destination and the replace flag tells us to not
    // replace it. In that case, return the original destination.
    if ($this->fileSystem
      ->getDestinationFilename($destination, $replace) === FALSE) {
      return $destination;
    }
    try {
      if ($this->configuration['move']) {
        return $this->fileSystem
          ->move($source, $destination, $replace);
      }
      else {
        return $this->fileSystem
          ->copy($source, $destination, $replace);
      }
    } catch (FileException $e) {
      return FALSE;
    }
  }

  /**
   * Returns the directory component of a URI or path.
   *
   * For URIs like public://foo.txt, the full physical path of public://
   * will be returned, since a scheme by itself will trip up certain file
   * API functions (such as
   * \Drupal\Core\File\FileSystemInterface::prepareDirectory()).
   *
   * @param string $uri
   *   The URI or path.
   *
   * @return string|false
   *   The directory component of the path or URI, or FALSE if it could not
   *   be determined.
   */
  protected function getDirectory($uri) {
    $dir = $this->fileSystem
      ->dirname($uri);
    if (substr($dir, -3) == '://') {
      return $this->fileSystem
        ->realpath($dir);
    }
    return $dir;
  }

  /**
   * Determines if the source and destination URIs represent identical paths.
   *
   * @param string $source
   *   The source URI.
   * @param string $destination
   *   The destination URI.
   *
   * @return bool
   *   TRUE if the source and destination URIs refer to the same physical path,
   *   otherwise FALSE.
   */
  protected function isLocationUnchanged($source, $destination) {
    return $this->fileSystem
      ->realpath($source) === $this->fileSystem
      ->realpath($destination);
  }

  /**
   * Determines if the given URI or path is considered local.
   *
   * A URI or path is considered local if it either has no scheme component,
   * or the scheme is implemented by a stream wrapper which extends
   * \Drupal\Core\StreamWrapper\LocalStream.
   *
   * @param string $uri
   *   The URI or path to test.
   *
   * @return bool
   */
  protected function isLocalUri($uri) {
    $scheme = StreamWrapperManager::getScheme($uri);

    // The vfs scheme is vfsStream, which is used in testing. vfsStream is a
    // simulated file system that exists only in memory, but should be treated
    // as a local resource.
    if ($scheme == 'vfs') {
      $scheme = FALSE;
    }
    return $scheme === FALSE || $this->streamWrapperManager
      ->getViaScheme($scheme) instanceof LocalStream;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FileCopy::$downloadPlugin protected property An instance of the download process plugin.
FileCopy::$fileSystem protected property The file system service.
FileCopy::$streamWrapperManager protected property The stream wrapper manager service.
FileCopy::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FileCopy::getDirectory protected function Returns the directory component of a URI or path.
FileCopy::isLocalUri protected function Determines if the given URI or path is considered local.
FileCopy::isLocationUnchanged protected function Determines if the source and destination URIs represent identical paths.
FileCopy::transform public function Performs the associated process. Overrides ProcessPluginBase::transform
FileCopy::writeFile protected function Tries to move or copy a file.
FileCopy::__construct public function Constructs a file_copy process plugin. Overrides FileProcessBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.