Commit ec6fe01d authored by Neslee Canil Pinto's avatar Neslee Canil Pinto Committed by Neslee

Issue #3123758 by switcode, Neslee Canil Pinto: Command "colorbox-plugin" is...

Issue #3123758 by switcode, Neslee Canil Pinto: Command "colorbox-plugin" is not defined - Drush >= 9
parent 07d80d4e
......@@ -5,3 +5,7 @@ services:
colorbox.attachment:
class: Drupal\colorbox\ColorboxAttachment
arguments: ['@colorbox.activation_check', '@module_handler', '@config.factory']
colorbox.commands:
class: \Drupal\colorbox\Commands\ColorboxCommands
tags:
- { name: drush.command }
......@@ -8,5 +8,12 @@
"source": "https://git.drupalcode.org/project/colorbox"
},
"license": "GPL-2.0+",
"minimum-stability": "dev"
"minimum-stability": "dev",
"extra": {
"drush": {
"services": {
"drush.services.yml": "^9"
}
}
}
}
<?php
namespace Drupal\colorbox\Commands;
use Drush\Commands\DrushCommands;
use Symfony\Component\Filesystem\Filesystem;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
/**
* A Drush commandfile.
*
* In addition to this file, you need a drush.services.yml
* in root of your module, and a composer.json file that provides the name
* of the services file to use.
*
* See these files for an example of injecting Drupal services:
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/
class ColorboxCommands extends DrushCommands {
/**
* Download and install the Colorbox plugin.
*
* @param mixed $path
* Optional. A path where to install the Colorbox plugin.
* If omitted Drush will use the default location.
*
* @command colorbox:plugin
* @aliases colorboxplugin,colorbox-plugin
*/
public function download($path = '') {
$fs = new Filesystem();
if (empty($path)) {
$path = DRUPAL_ROOT . '/libraries/colorbox';
}
// Create path if it doesn't exist
// Exit with a message otherwise.
if (!$fs->exists($path)) {
$fs->mkdir($path);
}
else {
$this->logger()->notice(dt('Colorbox is already present at @path. No download required.', ['@path' => $path]));
return;
}
// Load the colorbox defined library.
if ($colorbox_library = \Drupal::service('library.discovery')->getLibraryByName('colorbox', 'colorbox')) {
// Download the file.
$client = new Client();
$destination = tempnam(sys_get_temp_dir(), 'colorbox-tmp');
try {
$client->get($colorbox_library['remote'] . '/archive/master.zip', ['save_to' => $destination]);
}
catch (RequestException $e) {
// Remove the directory.
$fs->remove($path);
$this->logger()->error(dt('Drush was unable to download the colorbox library from @remote. @exception', [
'@remote' => $colorbox_library['remote'] . '/archive/master.zip',
'@exception' => $e->getMessage(),
], 'error'));
return;
}
// Move downloaded file.
$fs->rename($destination, $path . '/colorbox.zip');
// Unzip the file.
$zip = new \ZipArchive();
$res = $zip->open($path . '/colorbox.zip');
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
}
else {
// Remove the directory if unzip fails and exit.
$fs->remove($path);
$this->logger()->error(dt('Error: unable to unzip colorbox file.', [], 'error'));
return;
}
// Remove the downloaded zip file.
$fs->remove($path . '/colorbox.zip');
// Move the file.
$fs->mirror($path . '/colorbox-master', $path, NULL, ['override' => TRUE]);
$fs->remove($path . '/colorbox-master');
// Success.
$this->logger()->notice(dt('The colorbox library has been successfully downloaded to @path.', [
'@path' => $path,
], 'success'));
}
else {
$this->logger()->error(dt('Drush was unable to load the colorbox library'));
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment