You are here

public function PhoneInternationalCommands::drushDownloadFile in International Phone 3.x

Download file with Drush.

Parameters

string $url: The download url.

mixed $destination: The destination path.

Return value

bool|string The destination file.

Throws

\Exception

1 call to PhoneInternationalCommands::drushDownloadFile()
PhoneInternationalCommands::plugin in src/Commands/PhoneInternationalCommands.php
Download and install the Phone International plugin.

File

src/Commands/PhoneInternationalCommands.php, line 116

Class

PhoneInternationalCommands
A Drush commandfile.

Namespace

Drupal\phone_international\Commands

Code

public function drushDownloadFile($url, $destination = FALSE) {

  // Generate destination if omitted.
  if (!$destination) {
    $file = basename(current(explode('?', $url, 2)));
    $destination = getcwd() . '/' . basename($file);
  }

  // Copied from: \Drush\Commands\SyncViaHttpCommands::downloadFile.
  static $use_wget;
  if ($use_wget === NULL) {
    $process = Drush::process([
      'which',
      'wget',
    ]);
    $process
      ->run();
    $use_wget = $process
      ->isSuccessful();
  }
  $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;
}