You are here

public function ChosenLibCommands::plugin in Chosen 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/chosen_lib/src/Commands/ChosenLibCommands.php \Drupal\chosen_lib\Commands\ChosenLibCommands::plugin()

Download and install the Chosen plugin.

@command chosen:plugin @aliases chosenplugin,chosen-plugin

Parameters

string $path: Optional. A path where to install the Chosen plugin. If omitted Drush will use the default location.

Throws

\Exception

File

modules/chosen_lib/src/Commands/ChosenLibCommands.php, line 39

Class

ChosenLibCommands
A Drush commandfile.

Namespace

Drupal\chosen_lib\Commands

Code

public function plugin($path = '') {
  if (empty($path)) {
    $path = 'libraries';
  }

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

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

  // Download the zip archive.
  if ($filepath = $this
    ->drush_download_file(CHOSEN_DOWNLOAD_URI)) {
    $filename = basename($filepath);
    $dirname = basename($filepath, '.zip');

    // Remove any existing Chosen plugin directory.
    if (is_dir($dirname) || is_dir('chosen')) {
      $fileservice = \Drupal::service('file_system');
      $fileservice
        ->deleteRecursive($dirname);
      $fileservice
        ->deleteRecursive('chosen');
      $this
        ->drush_log(dt('A existing Chosen plugin was deleted from @path', [
        '@path' => $path,
      ]), 'notice');
    }

    // Decompress the zip archive.
    $this
      ->drush_tarball_extract($filename, $dirname);

    // Change the directory name to "chosen" if needed.
    if ('chosen' !== $dirname) {
      $this
        ->drush_move_dir($dirname, 'chosen');
      $dirname = 'chosen';
    }
    unlink($filename);
  }
  if (is_dir($dirname)) {
    $this
      ->drush_log(dt('Chosen plugin has been installed in @path', [
      '@path' => $path,
    ]), 'success');
  }
  else {
    $this
      ->drush_log(dt('Drush was unable to install the Chosen plugin to @path', [
      '@path' => $path,
    ]), 'error');
  }

  // Set working directory back to the previous working directory.
  chdir($olddir);
}