Same name and namespace in other branches
- 8.x-1.x src/Commands/ColorboxCommands.php \Drupal\colorbox\Commands\ColorboxCommands::domPurify() 1 comment
Download and install the DOMPurify plugin.
@command colorbox:dompurify @aliases colorboxdompurify,colorbox-dompurify
Parameters
mixed $path: Optional. A path where to install the DOMPurify plugin. If omitted Drush will use the default location.
File
-
src/
Commands/ ColorboxCommands.php, line 127
Class
- ColorboxCommands
- A Drush commandfile.
Namespace
Drupal\colorbox\CommandsCode
public function domPurify($path = '') {
$fs = new Filesystem();
if (empty($path)) {
$path = DRUPAL_ROOT . '/libraries/dompurify';
}
// Create path if it doesn't exist
// Exit with a message otherwise.
if (!$fs->exists($path)) {
$fs->mkdir($path);
}
else {
$this->logger()
->notice(dt('DOMPurify is already present at @path. No download required.', [
'@path' => $path,
]));
return;
}
// Load the DOMPurify defined library.
if ($dompurify_library = $this->libraryDiscovery
->getLibraryByName('colorbox', 'dompurify')) {
// Download the file.
$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) {
// Remove the directory.
$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;
}
// Move downloaded file.
$fs->rename($destination, $path . '/DOMPurify.zip');
// Unzip the file.
$zip = new \ZipArchive();
$res = $zip->open($path . '/DOMPurify.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 DOMPurify file.', []));
return;
}
// Remove the downloaded zip file.
$fs->remove($path . '/DOMPurify.zip');
// Move the dist directory.
$fs->mirror($path . '/DOMPurify-main/dist', $path . '/dist', NULL, [
'override' => TRUE,
]);
$fs->remove($path . '/DOMPurify-main');
// Success.
$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'));
}
}