ColorboxCommands.php in Colorbox 8
File
src/Commands/ColorboxCommands.php
View source
<?php
namespace Drupal\colorbox\Commands;
use Drupal\Core\Asset\libraryDiscovery;
use Drush\Commands\DrushCommands;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\Filesystem\Filesystem;
class ColorboxCommands extends DrushCommands {
protected $libraryDiscovery;
public function __construct(libraryDiscovery $library_discovery) {
$this->libraryDiscovery = $library_discovery;
}
public function download($path = '') {
$fs = new Filesystem();
if (empty($path)) {
$path = DRUPAL_ROOT . '/libraries/colorbox';
}
if (!$fs
->exists($path)) {
$fs
->mkdir($path);
}
else {
$this
->logger()
->notice(dt('Colorbox is already present at @path. No download required.', [
'@path' => $path,
]));
return;
}
if ($colorbox_library = $this->libraryDiscovery
->getLibraryByName('colorbox', 'colorbox')) {
$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) {
$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;
}
$fs
->rename($destination, $path . '/colorbox.zip');
$zip = new \ZipArchive();
$res = $zip
->open($path . '/colorbox.zip');
if ($res === TRUE) {
$zip
->extractTo($path);
$zip
->close();
}
else {
$fs
->remove($path);
$this
->logger()
->error(dt('Error: unable to unzip colorbox file.', [], 'error'));
return;
}
$fs
->remove($path . '/colorbox.zip');
$fs
->mirror($path . '/colorbox-master', $path, NULL, [
'override' => TRUE,
]);
$fs
->remove($path . '/colorbox-master');
$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'));
}
}
}