You are here

public function WebformCommandsBase::drush_download_file in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Commands/WebformCommandsBase.php \Drupal\webform\Commands\WebformCommandsBase::drush_download_file()

File

src/Commands/WebformCommandsBase.php, line 71

Class

WebformCommandsBase
Base class for Webform commands for Drush 9.x.

Namespace

Drupal\webform\Commands

Code

public function drush_download_file($url, $destination) {

  // Copied from: \Drush\Commands\SyncViaHttpCommands::downloadFile
  static $use_wget;
  if ($use_wget === NULL) {
    $use_wget = ExecTrait::programExists('wget');
  }
  $destination_tmp = drush_tempnam('download_file');
  if ($use_wget) {
    $args = [
      'wget',
      '-q',
      '--timeout=30',
      '-O',
      $destination_tmp,
      $url,
    ];
  }
  else {
    $args = [
      'curl',
      '-s',
      '-L',
      '--connect-timeout',
      '30',
      '-o',
      $destination_tmp,
      $url,
    ];
  }
  $process = Drush::process($args);
  $process
    ->mustRun();
  if (!drush_file_not_empty($destination_tmp) && ($file = @file_get_contents($url))) {
    @file_put_contents($destination_tmp, $file);
  }
  if (!drush_file_not_empty($destination_tmp)) {

    // Download failed.
    throw new \Exception(dt("The URL !url could not be downloaded.", [
      '!url' => $url,
    ]));
  }
  if ($destination) {
    $fs = new Filesystem();
    $fs
      ->rename($destination_tmp, $destination, TRUE);
    return $destination;
  }
  return $destination_tmp;
}