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

Hierarchy

Expanded class hierarchy of Download

1 file declares its use of Download
DownloadTest.php in core/modules/migrate/tests/src/Kernel/process/DownloadTest.php
1 string reference to 'Download'
UpdateContribTest::assertCoreCompatibilityMessage in core/modules/update/tests/src/Functional/UpdateContribTest.php
Asserts that a core compatibility message is correct for an update.

File

core/modules/migrate/src/Plugin/migrate/process/Download.php, line 63

Namespace

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

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

  /**
   * The Guzzle HTTP Client service.
   *
   * @var \GuzzleHttp\Client
   */
  protected $httpClient;

  /**
   * Constructs a download process plugin.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param array $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The HTTP client.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, FileSystemInterface $file_system, ClientInterface $http_client) {
    $configuration += [
      'guzzle_options' => [],
    ];
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->fileSystem = $file_system;
    $this->httpClient = $http_client;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('file_system'), $container
      ->get('http_client'));
  }

  /**
   * {@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;
    }
    [
      $source,
      $destination,
    ] = $value;

    // Modify the destination filename if necessary.
    $final_destination = $this->fileSystem
      ->getDestinationFilename($destination, $this->configuration['file_exists']);

    // Reuse if file exists.
    if (!$final_destination) {
      return $destination;
    }

    // Try opening the file first, to avoid calling prepareDirectory()
    // unnecessarily. We're suppressing fopen() errors because we want to try
    // to prepare the directory before we give up and fail.
    $destination_stream = @fopen($final_destination, 'w');
    if (!$destination_stream) {

      // If fopen didn't work, make sure there's a writable directory in place.
      $dir = $this->fileSystem
        ->dirname($final_destination);
      if (!$this->fileSystem
        ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
        throw new MigrateException("Could not create or write to directory '{$dir}'");
      }

      // Let's try that fopen again.
      $destination_stream = @fopen($final_destination, 'w');
      if (!$destination_stream) {
        throw new MigrateException("Could not write to file '{$final_destination}'");
      }
    }

    // Stream the request body directly to the final destination stream.
    $this->configuration['guzzle_options']['sink'] = $destination_stream;
    try {

      // Make the request. Guzzle throws an exception for anything but 200.
      $this->httpClient
        ->get($source, $this->configuration['guzzle_options']);
    } catch (\Exception $e) {
      throw new MigrateException("{$e->getMessage()} ({$source})");
    }
    if (is_resource($destination_stream)) {
      fclose($destination_stream);
    }
    return $final_destination;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Download::$fileSystem protected property The file system service.
Download::$httpClient protected property The Guzzle HTTP Client service.
Download::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Download::transform public function
Download::__construct public function Constructs a download process plugin. Overrides FileProcessBase::__construct