Same filename and directory in other branches
- 8.x-1.x src/Commands/ColorboxCommands.php
Namespace
Drupal\colorbox\Commands
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', [
'sink' => $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(),
]));
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.', []));
return;
}
$fs->remove($path . '/colorbox.zip');
$fs->mirror($path . '/colorbox-master', $path, NULL, [
'override' => TRUE,
]);
$fs->remove($path . '/colorbox-master');
$this->logger()
->success(dt('The colorbox library has been successfully downloaded to @path.', [
'@path' => $path,
]));
}
else {
$this->logger()
->error(dt('Drush was unable to load the colorbox library'));
}
}
public function domPurify($path = '') {
$fs = new Filesystem();
if (empty($path)) {
$path = DRUPAL_ROOT . '/libraries/dompurify';
}
if (!$fs->exists($path)) {
$fs->mkdir($path);
}
else {
$this->logger()
->notice(dt('DOMPurify is already present at @path. No download required.', [
'@path' => $path,
]));
return;
}
if ($dompurify_library = $this->libraryDiscovery
->getLibraryByName('colorbox', 'dompurify')) {
$client = new Client();
$destination = tempnam(sys_get_temp_dir(), 'DOMPurify-tmp');
try {
$client->get($dompurify_library['remote'] . '/archive/main.zip', [
'sink' => $destination,
]);
} catch (RequestException $e) {
$fs->remove($path);
$this->logger()
->error(dt('Drush was unable to download the DOMPurify library from @remote. @exception', [
'@remote' => $dompurify_library['remote'] . '/archive/main.zip',
'@exception' => $e->getMessage(),
]));
return;
}
$fs->rename($destination, $path . '/DOMPurify.zip');
$zip = new \ZipArchive();
$res = $zip->open($path . '/DOMPurify.zip');
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
}
else {
$fs->remove($path);
$this->logger()
->error(dt('Error: unable to unzip DOMPurify file.', []));
return;
}
$fs->remove($path . '/DOMPurify.zip');
$fs->mirror($path . '/DOMPurify-main/dist', $path . '/dist', NULL, [
'override' => TRUE,
]);
$fs->remove($path . '/DOMPurify-main');
$this->logger()
->success(dt('The DOMPurify library has been successfully downloaded to @path.', [
'@path' => $path,
]));
}
else {
$this->logger()
->error(dt('Drush was unable to load the DOMPurify library'));
}
}
}
Classes