You are here

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

Download and install the Phone International plugin.

@command phone_international:plugin @aliases piplugin,pi-plugin

Parameters

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

Throws

\Exception

File

src/Commands/PhoneInternationalCommands.php, line 40

Class

PhoneInternationalCommands
A Drush commandfile.

Namespace

Drupal\phone_international\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
      ->drushLog(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
    ->drushDownloadFile(PI_DOWNLOAD_URI)) {
    $filename = basename($filepath);
    $dirname = basename($filepath, '.zip');

    // Remove any existing Phone International(intl-tel-input)
    // plugin directory.
    if (is_dir($dirname) || is_dir('chosen')) {
      Filesystem::remove($dirname, TRUE);
      Filesystem::remove('intl-tel-input', TRUE);
      $this
        ->drushLog(dt('A existing Phone International(intl-tel-input) plugin was deleted from @path', [
        '@path' => $path,
      ]), 'notice');
    }

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

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

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