View source
<?php
namespace Drupal\phone_international\Commands;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Symfony\Component\Filesystem\Filesystem;
use Psr\Log\LogLevel;
define('PI_DOWNLOAD_URI', 'https://github.com/jackocnr/intl-tel-input/archive/v17.0.0.zip');
class PhoneInternationalCommands extends DrushCommands {
public function plugin($path = '') {
if (empty($path)) {
$path = 'libraries';
}
if (!is_dir($path)) {
drush_op('mkdir', $path);
$this
->drushLog(dt('Directory @path was created', [
'@path' => $path,
]), 'notice');
}
$olddir = getcwd();
chdir($path);
if ($filepath = $this
->drushDownloadFile(PI_DOWNLOAD_URI)) {
$filename = basename($filepath);
$dirname = basename($filepath, '.zip');
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');
}
$this
->drushTarballExtract($filename, $dirname);
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');
}
chdir($olddir);
}
public function drushLog($message, $type = LogLevel::INFO) {
$this
->logger()
->log($type, $message);
}
public function drushDownloadFile($url, $destination = FALSE) {
if (!$destination) {
$file = basename(current(explode('?', $url, 2)));
$destination = getcwd() . '/' . basename($file);
}
static $use_wget;
if ($use_wget === NULL) {
$process = Drush::process([
'which',
'wget',
]);
$process
->run();
$use_wget = $process
->isSuccessful();
}
$destination_tmp = drush_tempnam('download_file');
if ($use_wget) {
$args = [
'wget',
'-q',
'--timeout=30',
'-O',
$destination_tmp,
$url,
];
}
else {
$args = [
'curl',
'-s',
'-L',
'--connect-timeout',
'30',
'-o',
$destination_tmp,
$url,
];
}
$process = Drush::process($args);
$process
->mustRun();
if (!drush_file_not_empty($destination_tmp) && ($file = @file_get_contents($url))) {
@file_put_contents($destination_tmp, $file);
}
if (!drush_file_not_empty($destination_tmp)) {
throw new \Exception(dt("The URL !url could not be downloaded.", [
'!url' => $url,
]));
}
if ($destination) {
$fs = new Filesystem();
$fs
->rename($destination_tmp, $destination, TRUE);
return $destination;
}
return $destination_tmp;
}
public function drushMoveDir($src, $dest) {
$fs = new Filesystem();
$fs
->rename($src, $dest, TRUE);
}
public function drushMkdir($path) {
$fs = new Filesystem();
$fs
->mkdir($path);
}
public function drushTarballExtract($path, $destination = FALSE) {
$this
->drushMkdir($destination);
$cwd = getcwd();
if (preg_match('/\\.tgz$/', $path)) {
drush_op('chdir', dirname($path));
$process = Drush::process([
'tar',
'-xvzf',
$path,
'-C',
$destination,
]);
$process
->run();
drush_op('chdir', $cwd);
if (!$process
->isSuccessful()) {
throw new \Exception(dt('Unable to extract !filename.' . PHP_EOL . implode(PHP_EOL, $process
->getOutput()), [
'!filename' => $path,
]));
}
}
else {
drush_op('chdir', dirname($path));
$process = Drush::process([
'unzip',
$path,
'-d',
$destination,
]);
$process
->run();
drush_op('chdir', $cwd);
if (!$process
->isSuccessful()) {
throw new \Exception(dt('Unable to extract !filename.' . PHP_EOL . implode(PHP_EOL, $process
->getOutput()), [
'!filename' => $path,
]));
}
}
}
}