You are here

function drush_download_elastica in Search API Elasticsearch 7

Command to download the Elastica PHP Library.

1 call to drush_download_elastica()
drush_search_api_elasticsearch_elastica_pre_pm_enable in modules/elastica/search_api_elasticsearch_elastica.drush.inc
Implements drush_MODULE_pre_COMMAND().
1 string reference to 'drush_download_elastica'
search_api_elasticsearch_elastica_drush_command in modules/elastica/search_api_elasticsearch_elastica.drush.inc
Implements hook_drush_command().

File

modules/elastica/search_api_elasticsearch_elastica.drush.inc, line 36
Drush integration for Search API elasticsearch.

Code

function drush_download_elastica() {
  module_load_include('module', 'search_api_elasticsearch_elastica');
  spl_autoload_register('_search_api_elasticsearch_elastica_autoload');

  // Check if the library is already installed.
  if (class_exists('Elastica\\Client')) {
    drush_log(dt('Elastica library already present. No download required.'), 'ok');
    return;
  }
  if (module_exists('composer_manager')) {
    drush_shell_exec('drush composer-manager install');
  }
  else {
    if (!drush_shell_exec('type tar')) {
      return drush_set_error(dt('Missing dependency: tar. Install it before using this command.'));
    }
    $args = func_get_args();
    if (isset($args[0])) {
      $path = $args[0];
    }
    else {
      $path = drush_get_context('DRUSH_DRUPAL_ROOT');

      // If the library exists, get its directory.
      if (module_exists('libraries') && ($library_path = dirname(libraries_get_path('Elastica')))) {
        $path .= '/' . $library_path;
      }
      else {
        $path .= '/sites/all/libraries';
      }
    }

    // Create the path if it does not exist.
    if (!is_dir($path)) {
      drush_op('mkdir', $path);
      drush_log(dt('Directory @path was created', array(
        '@path' => $path,
      )), 'notice');
    }

    // Keep the old directory for later.
    $olddir = getcwd();

    // Set the directory to the download location.
    chdir($path);

    // Download the tar.
    _search_api_elasticsearch_elastica_fetch_tar(SEARCH_API_ELASTICSEARCH_ELASTICA_TARBALL);

    // Set working directory back to the previous working directory.
    chdir($olddir);
    if (is_dir($path . '/Elastica')) {
      drush_log(dt('Elastica has been downloaded to @path', array(
        '@path' => $path,
      )), 'success');
    }
    else {
      drush_log(dt('Drush was unable to download Elastica to @path', array(
        '@path' => $path,
      )), 'error');
    }
  }
}