You are here

public function ClientsideValidationJqueryDrush::addLibrary in Clientside Validation 3.0.x

Download third party libraries required by clientside validation jQuery module.

@command clientside_validation_jquery:library-download @aliases cvjld

File

clientside_validation_jquery/src/Commands/ClientsideValidationJqueryDrush.php, line 52

Class

ClientsideValidationJqueryDrush
Drush command file for clientside_validation_jquery module.

Namespace

Drupal\clientside_validation_jquery\Commands

Code

public function addLibrary() {

  // Check if library is already available.
  if (file_exists(DRUPAL_ROOT . '/libraries/jquery-validation')) {
    throw new \Exception('Library already downloaded, if you want to download again, please remove it first.');
  }

  // Download library.
  $tmp_location = $this->fileSystem
    ->getTempDirectory();
  $download_url = 'https://github.com/jquery-validation/jquery-validation/archive/1.17.0.zip';
  $this
    ->logger()
    ->notice('Downloading {download_url}', [
    'download_url' => $download_url,
  ]);
  file_put_contents($tmp_location . '/jquery-validation.zip', fopen($download_url, 'r'));
  $path = $tmp_location . '/jquery-validation.zip';
  $destination = DRUPAL_ROOT . '/libraries';

  // Unzip the downloaded archive.
  $process = Drush::process([
    'unzip',
    $path,
    '-d',
    $destination,
  ]);
  $process
    ->run();
  $return = $process
    ->isSuccessful();
  if (!$return) {
    throw new \Exception(dt('Unable to extract !filename.' . PHP_EOL . implode(PHP_EOL, $process
      ->getOutput()), [
      '!filename' => $path,
    ]));
  }

  // Name it properly (remove version number from directory).
  $fs = new Filesystem();
  $fs
    ->rename(DRUPAL_ROOT . '/libraries/jquery-validation-1.17.0', DRUPAL_ROOT . '/libraries/jquery-validation', TRUE);

  // Flush all caches.
  drupal_flush_all_caches();
}