You are here

function drupalgap_download_and_extract_module_from_github in DrupalGap 7

Same name and namespace in other branches
  1. 7.2 drupalgap.module \drupalgap_download_and_extract_module_from_github()

Given a module's details, this will download its zip file from github, extract it, delete the zip file, and move its contents to the app/modules folder.

1 call to drupalgap_download_and_extract_module_from_github()
drupalgap_module_install_form_submit in ./drupalgap.module

File

./drupalgap.module, line 626
A module to provide a bridge between Drupal websites and PhoneGap mobile applications.

Code

function drupalgap_download_and_extract_module_from_github($module_details) {
  $dir = variable_get('drupalgap_sdk_dir', 'mobile-application');
  $file = $module_details['github']['name'] . '.zip';
  $source = $module_details['github']['html_url'] . '/archive/' . $module_details['github']['default_branch'] . '.zip';

  // Download a zip file of the module from github.
  if (file_put_contents($file, file_get_contents($source)) === false) {
    drupal_set_message(t('Failed to download module!'), 'error');
    return;
  }

  // Unzip the module.
  $path = pathinfo(realpath($file), PATHINFO_DIRNAME) . '/' . $dir . '/app/modules';
  if (!file_exists($path)) {
    mkdir($path);
  }
  $zip = new ZipArchive();
  $res = $zip
    ->open($file);
  if ($res === TRUE) {
    $zip
      ->extractTo($path);
    $zip
      ->close();
  }
  else {
    drupal_set_message("Failed to unzip the {$file} file!", 'error');
  }

  // Delete the zip file.
  if (!unlink($file)) {
    drupal_set_message(t("Tried deleting the %file file, but failed", array(
      '%file' => $file,
    )), 'warning');
  }

  // Rename the folder.
  if (!rename($path . '/' . $module_details['github']['name'] . '-' . $module_details['github']['default_branch'], $path . '/' . $module_details['github']['name'])) {
    drupal_set_message("Failed to move unzipped files to the {$path} directory!", 'error');
    return;
  }
  drupal_set_message("Installed module.");
}