You are here

public function WebformCliService::drush_webform_libraries_download in Webform 6.x

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

Implements drush_hook_COMMAND().

Overrides WebformCliServiceInterface::drush_webform_libraries_download

File

src/Commands/WebformCliService.php, line 716

Class

WebformCliService
Drush version agnostic commands.

Namespace

Drupal\webform\Commands

Code

public function drush_webform_libraries_download() {

  // Remove all existing libraries (including excluded).
  if ($this
    ->drush_webform_libraries_remove(FALSE)) {
    $this
      ->drush_print($this
      ->dt('Removing existing libraries…'));
  }
  $temp_dir = $this
    ->drush_tempdir();

  /** @var \Drupal\webform\WebformLibrariesManagerInterface $libraries_manager */
  $libraries_manager = \Drupal::service('webform.libraries_manager');
  $libraries = $libraries_manager
    ->getLibraries(TRUE);
  foreach ($libraries as $library_name => $library) {

    // Skip libraries installed by other modules.
    if (!empty($library['module'])) {
      continue;
    }
    $download_location = DRUPAL_ROOT . "/libraries/{$library_name}";
    $download_url = $library['download_url']
      ->toString();
    if (preg_match('/\\.zip$/', $download_url)) {
      $download_type = 'zip';
    }
    elseif (preg_match('/\\.tgz$/', $download_url)) {
      $download_type = 'tar';
    }
    else {
      $download_type = 'file';
    }

    // Download archive to temp directory.
    $this
      ->drush_print("Downloading {$download_url}");
    if ($download_type === 'file') {
      $this
        ->drush_mkdir($download_location);
      $download_filepath = $download_location . '/' . basename($download_url);
      $this
        ->drush_download_file($download_url, $download_filepath);
    }
    else {
      $temp_filepath = $temp_dir . '/' . basename(current(explode('?', $download_url, 2)));
      $this
        ->drush_download_file($download_url, $temp_filepath);

      // Extract ZIP archive.
      $this
        ->drush_print("Extracting to {$download_location}");

      // Extract to temp location.
      $temp_location = $this
        ->drush_tempdir();
      if (!$this
        ->drush_tarball_extract($temp_filepath, $temp_location)) {
        $this
          ->drush_set_error("Unable to extract {$library_name}");
        return;
      }

      // Move files and directories from temp location to download location.
      // using rename.
      $files = scandir($temp_location);

      // Remove directories (. ..)
      unset($files[0], $files[1]);
      if (count($files) === 1 && is_dir($temp_location . '/' . current($files))) {
        $temp_location .= '/' . current($files);
      }
      $this
        ->drush_move_dir($temp_location, $download_location);

      // Remove the tarball.
      if (file_exists($temp_filepath)) {
        $this
          ->drush_delete_dir($temp_filepath, TRUE);
      }
    }
  }
  drupal_flush_all_caches();
}