View source
<?php
use Symfony\Component\Filesystem\Filesystem;
define('BXSLIDER_DOWNLOAD_URI', 'https://github.com/stevenwanderski/bxslider-4/archive/v4.2.15.zip');
define('BXSLIDER_DOWNLOAD_PREFIX', 'bxslider-');
function bxslider_drush_command() {
$items = [];
$items['bxslider-plugin'] = [
'callback' => 'bxslider_drush_plugin_setup',
'description' => dt('Download and install the bxSlider plugin.'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'arguments' => [
'path' => dt('Optional. A path where to install the bxSlider plugin. If omitted Drush will use the default Libraries location.'),
],
'aliases' => [
'bxsliderplugin',
],
];
return $items;
}
function bxslider_drush_help($section) {
switch ($section) {
case 'drush:bxslider-plugin':
return dt('Download and install the bxSlider plugin. Default location is the Libraries directory.');
}
}
function bxslider_drush_plugin_setup() {
$args = func_get_args();
if (!empty($args[0])) {
$path = $args[0];
}
else {
$path = 'libraries';
}
if (!is_dir($path)) {
drush_op('mkdir', $path);
\Drupal::logger(dt('Directory @path was created', [
'@path' => $path,
]), 'notice');
}
$old_dir = getcwd();
chdir($path);
if ($file_path = drush_download_file(BXSLIDER_DOWNLOAD_URI)) {
$filename = basename($file_path);
$dir_name = BXSLIDER_DOWNLOAD_PREFIX . basename($file_path, '.zip');
if (is_dir($dir_name) || is_dir('bxslider')) {
Filesystem::remove($dir_name, TRUE);
Filesystem::remove('bxslider', TRUE);
\Drupal::logger(dt('A existing bxSlider plugin was deleted from @path', [
'@path' => $path,
]), 'notice');
}
drush_tarball_extract($filename);
if ($dir_name != 'bxslider') {
drush_move_dir($dir_name, 'bxslider', TRUE);
$dir_name = 'bxslider';
}
}
if (is_dir($dir_name)) {
\Drupal::logger(dt('bxSlider plugin has been installed in @path', [
'@path' => $path,
]), 'success');
}
else {
\Drupal::logger(dt('Drush was unable to install the bxSlider plugin to @path', [
'@path' => $path,
]), 'error');
}
chdir($old_dir);
}