ClientsideValidationJqueryDrush.php in Clientside Validation 3.0.x
File
clientside_validation_jquery/src/Commands/ClientsideValidationJqueryDrush.php
View source
<?php
namespace Drupal\clientside_validation_jquery\Commands;
use Drush\Commands\DrushCommands;
use Drupal\Core\File\FileSystemInterface;
use Drush\Drush;
use Symfony\Component\Filesystem\Filesystem;
class ClientsideValidationJqueryDrush extends DrushCommands {
protected $fileSystem;
public function __construct(FileSystemInterface $file_system) {
$this->fileSystem = $file_system;
}
public function libraryStatus() {
$installed = file_exists(DRUPAL_ROOT . '/libraries/jquery-validation/dist/jquery.validate.js');
$message = $installed ? 'installed' : 'not installed';
$this
->logger()
->notice('Library required for clientside validation jQuery is {message}', [
'message' => $message,
]);
}
public function addLibrary() {
if (file_exists(DRUPAL_ROOT . '/libraries/jquery-validation')) {
throw new \Exception('Library already downloaded, if you want to download again, please remove it first.');
}
$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';
$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,
]));
}
$fs = new Filesystem();
$fs
->rename(DRUPAL_ROOT . '/libraries/jquery-validation-1.17.0', DRUPAL_ROOT . '/libraries/jquery-validation', TRUE);
drupal_flush_all_caches();
}
public function removeLibrary() {
$this
->logger()
->notice('Beginning to remove libraries...');
if (file_exists(DRUPAL_ROOT . '/libraries/jquery-validation')) {
$this
->logger()
->notice('jQuery validation library removed.');
$this->fileSystem
->deleteRecursive(DRUPAL_ROOT . '/libraries/jquery-validation');
drupal_flush_all_caches();
return TRUE;
}
return FALSE;
}
}